Skip to content

Commit b4a5596

Browse files
♻️ refactor: create and update todo process
1 parent 20532fa commit b4a5596

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

app.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,38 @@ const server = http.createServer((request, response) => {
4949

5050
// create - /api/todo : POST
5151
else if (request.url == "/api/todo" && request.method === "POST") {
52-
todo.createTodo().then((todo) => {
53-
response.writeHead(201, CONTENT_TYPE_JSON);
54-
response.end(JSON.stringify(todo));
52+
getRequestBody(request).then((todoData) => {
53+
todo.createTodo(todoData).then((todo) => {
54+
response.writeHead(201, CONTENT_TYPE_JSON);
55+
response.end(JSON.stringify(todo));
56+
})
57+
.catch((err) => {
58+
response.writeHead(400, CONTENT_TYPE_JSON);
59+
response.end(JSON.stringify(err));
60+
});
61+
})
62+
.catch((err) => {
63+
response.writeHead(400, CONTENT_TYPE_JSON);
64+
response.end(JSON.stringify({ message: "Invalid request body", err }));
5565
});
5666
}
5767

5868
// update - /api/todo/ : PUT
5969
else if (request.url == "/api/todo" && request.method === "PUT") {
60-
let body = "";
61-
request
62-
.on("data", (chunk) => {
63-
console.log(chunk.toString());
64-
body += chunk.toString();
70+
getRequestBody(request).then((todoData) => {
71+
todo.updateTodo(todoData).then((todo) => {
72+
response.writeHead(200, CONTENT_TYPE_JSON);
73+
response.end(JSON.stringify(todo));
6574
})
66-
.on("end", () => {
67-
const { id, title, completed, date } = JSON.parse(body);
68-
const todo = { id: parseInt(id), title, completed, date };
69-
new Todo()
70-
.updateTodo(todo)
71-
.then((todo) => {
72-
response.writeHead(200, CONTENT_TYPE_JSON);
73-
response.end(JSON.stringify(todo));
74-
})
75-
.catch((err) => {
76-
response.writeHead(404, CONTENT_TYPE_JSON);
77-
response.end(JSON.stringify(err));
78-
});
75+
.catch((err) => {
76+
response.writeHead(404, CONTENT_TYPE_JSON);
77+
response.end(JSON.stringify(err));
7978
});
79+
})
80+
.catch((err) => {
81+
response.writeHead(400, CONTENT_TYPE_JSON);
82+
response.end(JSON.stringify({ message: "Invalid request body", err }));
83+
});
8084
}
8185

8286
// delete - /api/todo/:id : DELETE

controller.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Dependencies
12
const fs = require("fs");
23

34
const data = require("./data.json");
@@ -21,11 +22,20 @@ class Controller {
2122

2223
// store
2324
createTodo(todo) {
24-
return new Promise((resolve) => {
25+
return new Promise((resolve, reject) => {
26+
const { title } = todo;
2527
const id = data.reduce((max, todo) => Math.max(max, todo.id), 0) + 1;
26-
const newTodo = { id, ...todo };
28+
const completed = false;
29+
const date = new Date().toISOString().slice(0, 10);
30+
const newTodo = { id, title, completed, date };
31+
32+
if(!title){
33+
return reject({ message: `Todo title is required` });
34+
}
35+
2736
data.push(newTodo);
2837
fs.writeFileSync("./data.json", JSON.stringify(data));
38+
2939
resolve(newTodo);
3040
});
3141
}
@@ -34,12 +44,20 @@ class Controller {
3444
updateTodo(todo) {
3545
return new Promise((resolve, reject) => {
3646
const index = data.findIndex((item) => item.id === todo.id);
47+
const date = new Date().toISOString().slice(0, 10);
48+
3749
if (index === -1) {
3850
return reject({ message: `Todo with id ${todo.id} not found` });
3951
}
40-
data[index] = todo;
4152

42-
fs.writeFileSync("./data.json", JSON.stringify(data));
53+
if(!todo.title) {
54+
return reject({ message: `Todo title is required` });
55+
}
56+
data[index].title = todo.title;
57+
data[index].completed = todo.completed || data[index].completed;
58+
data[index].date = date;
59+
60+
fs.writeFileSync("./data.json", JSON.stringify(data[index]));
4361

4462
resolve(todo);
4563
});

data.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
"id": 1,
44
"title": "Learn JavaScript",
55
"completed": true,
6-
"date": "28/11/2022"
6+
"date": "2022-11-28"
77
},
88
{
99
"id": 2,
1010
"title": "Learn NodeJS",
1111
"completed": true,
12-
"date": "04/12/2022"
12+
"date": "2022-12-04"
13+
},
14+
{
15+
"id": 6,
16+
"title": "Learn React",
17+
"completed": false,
18+
"date": "2022-12-05"
19+
},
20+
{
21+
"id": 7,
22+
"title": "Learn Unit Testing",
23+
"completed": false,
24+
"date": "2022-12-05"
1325
}
1426
]

0 commit comments

Comments
 (0)