-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
113 lines (97 loc) · 2.52 KB
/
handler.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const { nanoid } = require("nanoid");
const notes = require("./notes");
const addNodeHandler = (req, res) => {
const { title, tags, body } = req.body;
const id = nanoid(16);
const createdAt = new Date().toISOString();
const updatedAt = createdAt;
const newNode = {
title,
tags,
body,
id,
createdAt,
updatedAt,
};
notes.push(newNode);
const isSuccess = notes.filter((note) => note.id === id).length > 0;
if (isSuccess) {
res.json({
status: "success",
messege: "Catatan berhasil ditambahkan",
data: {
noteId: id,
},
});
} else {
res.json({
status: "fail",
messege: "Catatan gagal ditambahkan",
});
}
};
const getAllNodeHandler = (req, res) => {
res.json({
status: "success",
data: {
notes: notes,
},
});
};
const getNoteHandler = (req, res) => {
const { id } = req.params;
const note = notes.filter((note) => note.id === id);
if (note.length > 0) {
res.json({
status: "success",
data: {
note: note[0],
},
});
} else {
res.json({
status: "error",
message: "note not found",
});
}
};
const updateNodeHandler = (req, res) => {
const { id } = req.params;
const { title, tags, body } = req.body;
const note = notes.filter((note) => note.id === id);
if (note.length > 0) {
note[0].title = title;
note[0].tags = tags;
note[0].body = body;
note[0].updatedAt = new Date().toISOString();
res.json({
status: "success",
messege: "Catatan berhasil diubah",
data: {
note: note[0],
},
});
} else {
res.json({
status: "fail",
messege: "Catatan tidak ditemukan",
});
}
};
const deleteNodeHandler = (req, res) => {
const { id } = req.params;
const note = notes.filter((note) => note.id === id);
if (note.length > 0) {
notes.splice(notes.indexOf(note[0]), 1);
res.json({
status: "success",
messege: "Catatan berhasil dihapus",
});
} else {
res.json({
status: "fail",
messege: "Catatan tidak ditemukan",
});
}
};
module.exports = { addNodeHandler, getAllNodeHandler, getNoteHandler, updateNodeHandler, deleteNodeHandler };