-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from JackDev21/develop
Develop
- Loading branch information
Showing
13 changed files
with
503 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import fs from "fs" | ||
|
||
|
||
const data = {} | ||
|
||
data.findTasks = (condition, callback) => { | ||
|
||
fs.readFile('./data/tasks.json', 'utf-8', (error, tasksJson) => { | ||
if (error) { | ||
|
||
console.log(error) | ||
callback(error.message) | ||
return | ||
} | ||
|
||
if (!tasksJson) { | ||
tasksJson = '[]' | ||
} | ||
|
||
const tasks = JSON.parse(tasksJson) | ||
|
||
const filtered = tasks.filter(condition) | ||
|
||
callback(null, filtered) | ||
}) | ||
} | ||
|
||
|
||
data.insertTask = (task, callback) => { | ||
fs.readFile('./data/tasks.json', 'utf-8', (error, tasksJson) => { | ||
if (error) { | ||
console.log(error) | ||
|
||
return | ||
} | ||
|
||
if (!tasksJson) { | ||
tasksJson = '[]' | ||
} | ||
const tasks = JSON.parse(tasksJson) | ||
|
||
task.id = `${Math.random().toString().slice(2)}-${Date.now()}` | ||
|
||
tasks.push(task) | ||
|
||
const jsonTasks = JSON.stringify(tasks) | ||
|
||
fs.writeFile('./data/tasks.json', jsonTasks, (error) => { | ||
if (error) { | ||
console.log(error) | ||
|
||
return | ||
} | ||
|
||
callback(null) | ||
}) | ||
}) | ||
} | ||
|
||
data.findOneTask = (condition, callback) => { | ||
fs.readFile("./data/tasks.json", "utf-8", (error, tasksJson) => { | ||
if (error) { | ||
callback(new Error(error)) | ||
|
||
return | ||
} | ||
|
||
if (!tasksJson) { | ||
tasksJson = "[]" | ||
} | ||
|
||
const tasks = JSON.parse(tasksJson) | ||
const taskFind = tasks.find(condition) | ||
|
||
callback(null, taskFind) | ||
}) | ||
} | ||
|
||
|
||
data.deleteTask = (condition, callback) => { | ||
|
||
fs.readFile("./data/tasks.json", "utf8", (error, tasksJson) => { | ||
if (error) { | ||
callback(new Error(error.message)) | ||
return | ||
} | ||
|
||
if (!tasksJson) { | ||
tasksJson = "[]" | ||
} | ||
const tasks = JSON.parse(tasksJson) | ||
|
||
const index = tasks.findIndex(condition) | ||
|
||
if (index > -1) { | ||
const deletedTask = tasks.splice(index, 1)[0] | ||
const newJson = JSON.stringify(tasks) | ||
|
||
fs.writeFile("./data/tasks.json", newJson, (error) => { | ||
if (error) { | ||
callback(new Error(error.message)) | ||
return | ||
} | ||
|
||
callback(null, deletedTask) | ||
}) | ||
} else { | ||
callback(null) | ||
} | ||
}) | ||
} | ||
|
||
export default data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import express from 'express' | ||
import cors from 'cors' | ||
|
||
import logic from '../api/logic/logic.js' | ||
|
||
const api = express() | ||
|
||
api.use(cors()) | ||
|
||
|
||
const jsonBodyParser = express.json({ strict: true, type: 'application/json' }) | ||
|
||
api.get('/', (req, res) => { | ||
res.send('Hello World!') | ||
}) | ||
|
||
|
||
api.get("/tasks", (req, res) => { | ||
|
||
try { | ||
logic.getAllTasks((error, tasks) => { | ||
if (error) { | ||
res.status(500).json({ error: error.message }) | ||
return | ||
} | ||
res.json(tasks) | ||
}) | ||
|
||
} catch (error) { | ||
res.status(500).json({ error: error.message }) | ||
} | ||
}) | ||
|
||
|
||
api.post("/tasks", jsonBodyParser, (req, res) => { | ||
|
||
const { text } = req.body | ||
|
||
try { | ||
logic.createTask(text, (error) => { | ||
if (error) { | ||
res.status(500).json({ error: error }) | ||
|
||
return | ||
} | ||
|
||
res.status(201).send({ message: 'task inserted, seguimos Laiiifff!!!' }) | ||
}) | ||
|
||
} catch (error) { | ||
console.log(error) | ||
} | ||
}) | ||
|
||
api.delete("/tasks/:taskId", (req, res) => { | ||
|
||
const { taskId } = req.params | ||
|
||
try { | ||
logic.deleteTask(taskId, (error) => { | ||
if (error) { | ||
res.status(500).json({ error: error }) | ||
return | ||
} | ||
|
||
res.status(204).send() | ||
}) | ||
|
||
} catch (error) { | ||
|
||
res.status(500).json({ error: error }) | ||
} | ||
}) | ||
|
||
api.listen(3001, () => console.log('Tamosss Laifff!!! http://localhost:3001')) | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import logic from "./logic.js" | ||
|
||
// try { | ||
// logic.getAllTasks(() => true, (error, tasks) => { | ||
// if (error) { | ||
// console.log(error) | ||
|
||
// } | ||
|
||
// console.log(tasks) | ||
|
||
// }) | ||
// } catch (error) { | ||
|
||
// console.log(error) | ||
// } | ||
|
||
|
||
|
||
// try { | ||
// logic.createTask('Prueba', (error) => { | ||
// if (error) { | ||
// console.log(error) | ||
|
||
// return | ||
// } | ||
// console.log('task inserted, seguimos Laiiifff!!!') | ||
|
||
// }) | ||
|
||
// } catch (error) { | ||
// console.log(error) | ||
// } | ||
|
||
try { | ||
logic.deleteTask("8322284392957846-1718037846180", (error) => { | ||
if (error) { | ||
console.log("error") | ||
return | ||
} | ||
|
||
console.log("task deleted") | ||
}) | ||
|
||
} catch (error) { | ||
console.log(error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
import data from "../data/data.js" | ||
|
||
const logic = {} | ||
|
||
logic.getAllTasks = (callback) => { | ||
|
||
data.findTasks(() => true, (error, tasks) => { | ||
|
||
if (error) { | ||
callback(error) | ||
return | ||
} | ||
|
||
callback(null, tasks) | ||
}) | ||
} | ||
|
||
logic.createTask = (text, callback) => { | ||
const task = { | ||
text: text, | ||
} | ||
|
||
data.insertTask(task, error => { | ||
|
||
if (error) { | ||
callback(error) | ||
return | ||
} | ||
|
||
callback(null) | ||
}) | ||
} | ||
|
||
logic.deleteTask = (taskId, callback) => { | ||
|
||
data.findOneTask(task => task.id === taskId, (error, task) => { | ||
if (error) { | ||
callback(error) | ||
return | ||
} | ||
|
||
if (!task) { | ||
callback(console.error(error.message)) | ||
} | ||
|
||
data.deleteTask(task => task.id === taskId, (error) => { | ||
if (error) { | ||
callback(error) | ||
return | ||
} | ||
|
||
callback(null) | ||
}) | ||
}) | ||
} | ||
|
||
export default logic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
{ | ||
"dependencies": { | ||
"cors": "^2.8.5", | ||
"express": "^4.19.2" | ||
}, | ||
"name": "api", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"devDependencies": {}, | ||
"scripts": { | ||
"start": "node .", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "" | ||
} | ||
"dependencies": { | ||
"cors": "^2.8.5", | ||
"express": "^4.19.2" | ||
}, | ||
"name": "api", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "index.js", | ||
"devDependencies": {}, | ||
"scripts": { | ||
"debug": "node --inspect-brk .", | ||
"start": "node .", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "" | ||
} |
Oops, something went wrong.