diff --git a/README.md b/README.md index 7c5284b..32a0e29 100644 --- a/README.md +++ b/README.md @@ -100,3 +100,5 @@ Este proyecto está bajo la Licencia MIT. Consulta el archivo [LICENSE](LICENSE) [![Web](https://img.shields.io/badge/GitHub-Jack-FFA500?style=for-the-badge&logo=github&logoColor=white&labelColor=101010)](https://github.com/JackDev21) [![Web](https://img.shields.io/badge/GitHub-Adrian-FFA500?style=for-the-badge&logo=github&logoColor=white&labelColor=101010)](https://github.com/AdrianGonzalo) + +[![Web](https://img.shields.io/badge/GitHub-Jose-FFA500?style=for-the-badge&logo=github&logoColor=white&labelColor=101010)](https://github.com/j0sep0z0) diff --git a/api/data/tasks.json b/api/data/tasks.json new file mode 100644 index 0000000..e51a78a --- /dev/null +++ b/api/data/tasks.json @@ -0,0 +1 @@ +[{"id":1,"name":"Learn React","completed":false},{"id":2,"name":"Learn Vite","completed":false},{"id":3,"name":"Learn Node","completed":false},{"id":"10","text":"Prueba"},{"id":11,"text":"Prueba"}] \ No newline at end of file diff --git a/api/index.js b/api/index.js new file mode 100644 index 0000000..d75a398 --- /dev/null +++ b/api/index.js @@ -0,0 +1,20 @@ +import express from 'express' + +import cors from 'cors' + +const api = express() + +api.use(cors()) + +const jsonBodyParser = express.json({ strict: true, type: 'application/json' }) + +api.get('/', (req, res) => { + res.send('Hello World!') +}) + +api.listen(3000, () => console.log('Tamosss Laifff!!! http://localhost:3000')) + + + + + diff --git a/api/logic/index.test.js b/api/logic/index.test.js new file mode 100644 index 0000000..eacf998 --- /dev/null +++ b/api/logic/index.test.js @@ -0,0 +1,33 @@ +// import findTasks from './logic.js' + +// try { +// findTasks(() => true, (error, tasks) => { +// if (error) { +// console.log(error) + +// } + +// console.log(tasks) + +// }) +// } catch (error) { + +// console.log(error) +// } + +import createTask from './logic.js' + +try { + createTask(11, 'Prueba', (error) => { + if (error) { + console.log(error) + + return + } + console.log('task inserted, seguimos Laiiifff!!!') + + }) + +} catch (error) { + console.log(error) +} \ No newline at end of file diff --git a/api/logic/logic.js b/api/logic/logic.js index e69de29..27db0f6 100644 --- a/api/logic/logic.js +++ b/api/logic/logic.js @@ -0,0 +1,71 @@ +import fs from 'fs' + +const logic = {} + +const findTasks = (condition, callback) => { + fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => { + if (error) { + console.log(error) + + //alert('Error al leer el archivo') + + return + } + + if (!tasksJson) { + tasksJson = '[]' + } + + const tasks = JSON.parse(tasksJson) + + const taskFind = tasks.filter(condition) + + callback(null, taskFind) + }) + + +} + +const createTask = (id, text, callback) => { + const task = { + id: id, + text: text + } + + const insertTask = (task, callback) => { + fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => { + if (error) { + console.log(error) + + return + } + + if (!tasksJson) { + tasksJson = '[]' + } + const tasks = JSON.parse(tasksJson) + + tasks.push(task) + + const jsonTasks = JSON.stringify(tasks) + + fs.writeFile('../api/data/tasks.json', jsonTasks, (error) => { + if (error) { + console.log(error) + + return + } + + callback(null) + }) + }) + + + + } + insertTask(task, callback); +} + + +//export default findTasks +export default createTask diff --git a/api/package.json b/api/package.json index 8c721ca..3c38bc8 100644 --- a/api/package.json +++ b/api/package.json @@ -5,6 +5,7 @@ }, "name": "api", "version": "1.0.0", + "type": "module", "main": "index.js", "devDependencies": {}, "scripts": {