-
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.
- Loading branch information
Showing
4 changed files
with
86 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import fs from "fs" | ||
|
||
const data = {} | ||
|
||
data.findTasks = (condition, callback) => { | ||
|
||
fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => { | ||
if (error) { | ||
console.log(error) | ||
callback(error) | ||
return | ||
} | ||
|
||
if (!tasksJson) { | ||
tasksJson = '[]' | ||
} | ||
|
||
const tasks = JSON.parse(tasksJson) | ||
|
||
const filtered = tasks.filter(condition) | ||
|
||
callback(null, filtered) | ||
}) | ||
} | ||
|
||
|
||
data.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) | ||
|
||
task.id = `${Math.random().toString().slice(2)}-${Date.now()}` | ||
|
||
tasks.push(task) | ||
|
||
const jsonTasks = JSON.stringify(tasks) | ||
|
||
fs.writeFile('../api/data/tasks.json', jsonTasks, (error) => { | ||
if (error) { | ||
console.log(error) | ||
|
||
return | ||
} | ||
|
||
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
[ | ||
{ "text": "Tarea Prueba", "id": "9691749823042075-1717951418737" }, | ||
{ "text": "Tarea 2", "id": "8822678026350037-1717951589854" }, | ||
{ "text": "Tarea 3", "id": "49480507403849905-1717951598149" }, | ||
{ "text": "Lorena es tonta del to", "id": "9158222028142664-1717951606748" } | ||
{ "text": "Test prueba", "id": "8322284392957846-1718037846180" }, | ||
{ "text": "Test 2", "id": "9554433903962911-1718038107817" } | ||
] |
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 |
---|---|---|
@@ -1,67 +1,36 @@ | ||
import fs from 'fs' | ||
|
||
import data from "../data/data.js" | ||
|
||
const logic = {} | ||
|
||
logic.findTasks = (condition, callback) => { | ||
fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => { | ||
if (error) { | ||
console.log(error) | ||
logic.getAllTasks = (callback) => { | ||
|
||
return | ||
} | ||
data.findTasks(() => true, (error, tasks) => { | ||
|
||
if (!tasksJson) { | ||
tasksJson = '[]' | ||
if (error) { | ||
callback(error) | ||
return | ||
} | ||
|
||
const tasks = JSON.parse(tasksJson) | ||
|
||
const taskFind = tasks.filter(condition) | ||
|
||
callback(null, taskFind) | ||
callback(null, tasks) | ||
}) | ||
|
||
|
||
} | ||
|
||
logic.createTask = (text, callback) => { | ||
const task = { | ||
|
||
text: text, | ||
} | ||
|
||
const insertTask = (task, callback) => { | ||
fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => { | ||
if (error) { | ||
console.log(error) | ||
data.insertTask(task, (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('../api/data/tasks.json', jsonTasks, (error) => { | ||
if (error) { | ||
console.log(error) | ||
if (error) { | ||
callback(error) | ||
return | ||
} | ||
|
||
return | ||
} | ||
callback(null) | ||
|
||
callback(null) | ||
}) | ||
}) | ||
} | ||
insertTask(task, callback); | ||
}) | ||
} | ||
|
||
|
||
export default logic |