Skip to content

Commit

Permalink
separate logic and data#20
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDev21 committed Jun 10, 2024
1 parent 09dfa45 commit 6d53917
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 60 deletions.
59 changes: 59 additions & 0 deletions api/data/data.js
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
6 changes: 2 additions & 4 deletions api/data/tasks.json
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" }
]
20 changes: 10 additions & 10 deletions api/logic/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import logic from "./logic.js"
// try {
// logic.findTasks(() => true, (error, tasks) => {
// if (error) {
// console.log(error)
try {
logic.findTasks(() => true, (error, tasks) => {
if (error) {
console.log(error)

// }
}

// console.log(tasks)
console.log(tasks)

// })
// } catch (error) {
})
} catch (error) {

// console.log(error)
// }
console.log(error)
}



Expand Down
61 changes: 15 additions & 46 deletions api/logic/logic.js
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

0 comments on commit 6d53917

Please sign in to comment.