-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.js
65 lines (49 loc) · 1.74 KB
/
crud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { v4: randomUuid } = require('uuid');
const datas = [];
const getTasks = (req, res) => {
res.json(datas);
}
const getOneTask = (req, res) => {
const id = req.params.id;
console.log(id)
const tasks = datas.find(task => task.id == id);
console.log(tasks)
if (!tasks) {
return res.status(404).json({ error: "No task found with that ID" });
}
res.json(tasks);
}
const createOneTask = (req, res) => {
const { taskTitle, taskDescription } = req.body;
const id = randomUuid();
if (!id || !taskTitle || !taskDescription) {
return res.status(400).json({ error: "Invalid input: Id/Title/Description is absent" });
}
const existingTask = datas.find(task => task.id === id);
if (existingTask) {
return res.status(409).json({ error: "Task with this ID already exists" });
}
datas.push({id, taskTitle, taskDescription});
res.json(datas);
}
const updateTask = (req, res) => {
const id = req.params.id;
const { taskTitle, taskDescription } = req.body;
const tasks = datas.find(task => task.id == id);
if (!tasks) {
return res.status(404).json({ error: "No task found with that ID" });
}
tasks.taskTitle = taskTitle;
tasks.taskDescription = taskDescription;
res.json(tasks);
}
const deleteTask = (req, res) => {
const id = req.params.id;
const taskIndex = datas.findIndex(task => task.id == id);
if(taskIndex === -1) {
return res.status(404).json({ error: "No task found with that ID" });
}
const tasks = datas.filter(task => task.id != id);
res.json(tasks);
}
module.exports={getTasks, getOneTask, createOneTask, updateTask, deleteTask}