Skip to content

Commit

Permalink
Merge pull request #24 from JackDev21/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
JackDev21 authored Jun 13, 2024
2 parents 6de08a0 + cad451e commit b8e5224
Show file tree
Hide file tree
Showing 13 changed files with 503 additions and 46 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)
113 changes: 113 additions & 0 deletions api/data/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import fs from "fs"


const data = {}

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

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

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

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

const tasks = JSON.parse(tasksJson)

const filtered = tasks.filter(condition)

callback(null, filtered)
})
}


data.insertTask = (task, callback) => {
fs.readFile('./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('./data/tasks.json', jsonTasks, (error) => {
if (error) {
console.log(error)

return
}

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
1 change: 1 addition & 0 deletions api/data/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
80 changes: 80 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import express from 'express'
import cors from 'cors'

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) {
res.status(500).json({ error: error })

return
}

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

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

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'))





47 changes: 47 additions & 0 deletions api/logic/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import logic from "./logic.js"

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

// }

// console.log(tasks)

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

// console.log(error)
// }



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

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

// })

// } catch (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)
}
58 changes: 58 additions & 0 deletions api/logic/logic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

import data from "../data/data.js"

const logic = {}

logic.getAllTasks = (callback) => {

data.findTasks(() => true, (error, tasks) => {

if (error) {
callback(error)
return
}

callback(null, tasks)
})
}

logic.createTask = (text, callback) => {
const task = {
text: text,
}

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)
})
})
}

export default logic
36 changes: 19 additions & 17 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2"
},
"name": "api",
"version": "1.0.0",
"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": ""
}
Loading

0 comments on commit b8e5224

Please sign in to comment.