-
Notifications
You must be signed in to change notification settings - Fork 3
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 #23 from j0sep0z0/feature/server
add functions createTask & findTasks; #20
- Loading branch information
Showing
6 changed files
with
128 additions
and
0 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 @@ | ||
[{"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"}] |
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,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')) | ||
|
||
|
||
|
||
|
||
|
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,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) | ||
} |
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,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 |
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