Skip to content

Commit

Permalink
add functions createTask & findTasks; JackDev21#20
Browse files Browse the repository at this point in the history
  • Loading branch information
j0sep0z0 committed Jun 8, 2024
1 parent 3a70a7c commit ba73ada
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions api/data/tasks.json
Original file line number Diff line number Diff line change
@@ -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"}]
20 changes: 20 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -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'))





33 changes: 33 additions & 0 deletions api/logic/index.test.js
Original file line number Diff line number Diff line change
@@ -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)
}
71 changes: 71 additions & 0 deletions api/logic/logic.js
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"name": "api",
"version": "1.0.0",
"type": "module",
"main": "index.js",
"devDependencies": {},
"scripts": {
Expand Down

0 comments on commit ba73ada

Please sign in to comment.