Skip to content

Commit

Permalink
fix findtasks, add taskdelete functions #20
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDev21 committed Jun 11, 2024
1 parent 6d53917 commit 18e54e0
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 63 deletions.
62 changes: 58 additions & 4 deletions api/data/data.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import fs from "fs"


const data = {}

data.findTasks = (condition, callback) => {

fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => {
fs.readFile('./data/tasks.json', 'utf-8', (error, tasksJson) => {
if (error) {

console.log(error)
callback(error)
callback(error.message)
return
}

Expand All @@ -25,7 +27,7 @@ data.findTasks = (condition, callback) => {


data.insertTask = (task, callback) => {
fs.readFile('../api/data/tasks.json', 'utf-8', (error, tasksJson) => {
fs.readFile('./data/tasks.json', 'utf-8', (error, tasksJson) => {
if (error) {
console.log(error)

Expand All @@ -43,7 +45,7 @@ data.insertTask = (task, callback) => {

const jsonTasks = JSON.stringify(tasks)

fs.writeFile('../api/data/tasks.json', jsonTasks, (error) => {
fs.writeFile('./data/tasks.json', jsonTasks, (error) => {
if (error) {
console.log(error)

Expand All @@ -53,7 +55,59 @@ data.insertTask = (task, callback) => {
callback(null)
})
})
}

data.findOneTask = (condition, callback) => {
fs.readFile("./data/tasks.json", "utf-8", (error, tasksJson) => {
if (error) {
callback(new Error(error))

return
}

if (!tasksJson) {
tasksJson = "[]"
}

const tasks = JSON.parse(tasksJson)
const taskFind = tasks.find(condition)

callback(null, taskFind)
})
}


data.deleteTask = (condition, callback) => {

fs.readFile("./data/tasks.json", "utf8", (error, tasksJson) => {
if (error) {
callback(new Error(error.message))
return
}

if (!tasksJson) {
tasksJson = "[]"
}
const tasks = JSON.parse(tasksJson)

const index = tasks.findIndex(condition)

if (index > -1) {
const deletedTask = tasks.splice(index, 1)[0]
const newJson = JSON.stringify(tasks)

fs.writeFile("./data/tasks.json", newJson, (error) => {
if (error) {
callback(new Error(error.message))
return
}

callback(null, deletedTask)
})
} else {
callback(null)
}
})
}

export default data
4 changes: 2 additions & 2 deletions api/data/tasks.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
{ "text": "Test prueba", "id": "8322284392957846-1718037846180" },
{ "text": "Test 2", "id": "9554433903962911-1718038107817" }
{ "text": "test", "id": "8315203615181515-1718125009567" },
{ "text": "test 2", "id": "03419535378202143-1718125011402" }
]
44 changes: 40 additions & 4 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
import express from 'express'
import cors from 'cors'

import logic from './logic/logic.js'
import logic from '../api/logic/logic.js'

const api = express()

api.use(cors())


const jsonBodyParser = express.json({ strict: true, type: 'application/json' })

api.get('/', (req, res) => {
res.send('Hello World!')
})


api.get("/tasks", (req, res) => {

try {
logic.getAllTasks((error, tasks) => {
if (error) {
res.status(500).json({ error: error.message })
return
}
res.json(tasks)
})

} catch (error) {
res.status(500).json({ error: error.message })
}
})


api.post("/tasks", jsonBodyParser, (req, res) => {

const { text } = req.body


try {
logic.createTask(text, (error) => {
if (error) {
Expand All @@ -28,15 +45,34 @@ api.post("/tasks", jsonBodyParser, (req, res) => {
}

res.status(201).send({ message: 'task inserted, seguimos Laiiifff!!!' })

})

} catch (error) {
console.log(error)
}
})

api.listen(3000, () => console.log('Tamosss Laifff!!! http://localhost:3000'))
api.delete("/tasks/:taskId", (req, res) => {

const { taskId } = req.params

try {
logic.deleteTask(taskId, (error) => {
if (error) {
res.status(500).json({ error: error })
return
}

res.status(204).send()
})

} catch (error) {

res.status(500).json({ error: error })
}
})

api.listen(3001, () => console.log('Tamosss Laifff!!! http://localhost:3001'))



Expand Down
53 changes: 34 additions & 19 deletions api/logic/index.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
import logic from "./logic.js"
try {
logic.findTasks(() => true, (error, tasks) => {
if (error) {
console.log(error)

}
// try {
// logic.getAllTasks(() => true, (error, tasks) => {
// if (error) {
// console.log(error)

console.log(tasks)
// }

})
} catch (error) {
// console.log(tasks)

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

// console.log(error)
// }



// try {
// logic.createTask(11, 'Prueba', (error) => {
// if (error) {
// console.log(error)
// logic.createTask('Prueba', (error) => {
// if (error) {
// console.log(error)

// return
// }
// console.log('task inserted, seguimos Laiiifff!!!')
// return
// }
// console.log('task inserted, seguimos Laiiifff!!!')

// })
// })

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

try {
logic.deleteTask("8322284392957846-1718037846180", (error) => {
if (error) {
console.log("error")
return
}

console.log("task deleted")
})

} catch (error) {
console.log(error)
}
24 changes: 23 additions & 1 deletion api/logic/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,37 @@ logic.createTask = (text, callback) => {
text: text,
}

data.insertTask(task, (error) => {
data.insertTask(task, error => {

if (error) {
callback(error)
return
}

callback(null)
})
}

logic.deleteTask = (taskId, callback) => {

data.findOneTask(task => task.id === taskId, (error, task) => {
if (error) {
callback(error)
return
}

if (!task) {
callback(console.error(error.message))
}

data.deleteTask(task => task.id === taskId, (error) => {
if (error) {
callback(error)
return
}

callback(null)
})
})
}

Expand Down
37 changes: 19 additions & 18 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2"
},
"name": "api",
"version": "1.0.0",
"type": "module",
"main": "index.js",
"devDependencies": {},
"scripts": {
"start": "node .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
"dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2"
},
"name": "api",
"version": "1.0.0",
"type": "module",
"main": "index.js",
"devDependencies": {},
"scripts": {
"debug": "node --inspect-brk .",
"start": "node .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
42 changes: 36 additions & 6 deletions app/src/component/TaskList.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react"
import { useState, useEffect } from "react"

import logic from "../logic"

Expand Down Expand Up @@ -37,10 +37,40 @@ function TaskList() {
setTask("")
}

const handleDeleteTask = (indexToDelete) => {
const taskUpdated = [...tasks]
taskUpdated.splice(indexToDelete, 1)
setTasks(taskUpdated)
useEffect(() => {
try {
logic.getAllTasks((error, tasks) => {
if (error) {
console.error(error)
alert(error.message)
return
}

setTasks(tasks)
})
} catch (error) {
if (error) {
console.error(error)
alert(error.message)
}
}
}, [])

const handleDeleteTask = (indexToDelete, taskId) => {
try {
logic.deleteTask(taskId, (error) => {
if (error) {
console.error(error)
alert(error.message)
}
const taskUpdated = [...tasks]
taskUpdated.splice(indexToDelete, 1)
setTasks(taskUpdated)
})
} catch (error) {
console.error(error)
alert(error.message)
}
}

const handleTaskCompleted = (indexToComplete) => {
Expand All @@ -67,7 +97,7 @@ function TaskList() {
<p onClick={() => handleTaskCompleted(index)} className={task.completed ? "Completed" : ""}>
{task.text}
</p>
<span onClick={() => handleDeleteTask(index)}></span>
<span onClick={() => handleDeleteTask(index, task.id)}></span>
</li>
))}
</ul>
Expand Down
Loading

0 comments on commit 18e54e0

Please sign in to comment.