-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
80 lines (66 loc) · 1.89 KB
/
controller.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Dependencies
const fs = require("fs");
const data = require("./data.json");
class Controller {
// get all
getTodos() {
return Promise.resolve(data);
}
// get by id
getTodoById(id) {
return new Promise((resolve, reject) => {
const todo = data.find((todo) => todo.id === parseInt(id));
if (!todo) {
return reject({ message: `Todo with id ${id} not found` });
}
resolve(todo);
});
}
// store
createTodo(todo) {
return new Promise((resolve, reject) => {
const { title } = todo;
const id = data.reduce((max, todo) => Math.max(max, todo.id), 0) + 1;
const completed = false;
const date = new Date().toISOString().slice(0, 10);
const newTodo = { id, title, completed, date };
if(!title){
return reject({ message: `Todo title is required` });
}
data.push(newTodo);
fs.writeFileSync("./data.json", JSON.stringify(data));
resolve(newTodo);
});
}
// update
updateTodo(todo) {
return new Promise((resolve, reject) => {
const index = data.findIndex((item) => item.id === todo.id);
const date = new Date().toISOString().slice(0, 10);
if (index === -1) {
return reject({ message: `Todo with id ${todo.id} not found` });
}
if(!todo.title) {
return reject({ message: `Todo title is required` });
}
data[index].title = todo.title;
data[index].completed = todo.completed || data[index].completed;
data[index].date = date;
fs.writeFileSync("./data.json", JSON.stringify(data[index]));
resolve(todo);
});
}
// delete
deleteTodoById(id) {
return new Promise((resolve, reject) => {
const index = data.findIndex((todo) => todo.id === parseInt(id));
if (index === -1) {
return reject({ message: `Todo with id ${id} not found` });
}
data.splice(index, 1);
fs.writeFileSync("./data.json", JSON.stringify(data));
resolve({ message: `Successfully deleted` });
});
}
}
module.exports = Controller;