Skip to content

Commit 858e283

Browse files
committed
membuat handler untuk edit note
1 parent 2ee7276 commit 858e283

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/handler.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,43 @@ const getNoteByIdHandler = (request, h) => {
6565
return response;
6666
}
6767

68-
module.exports = { addNoteHandler, getAllNotesHandler, getNoteByIdHandler };
68+
const editNoteByIdHandler = (request, h) => {
69+
const { id } = request.params;
70+
71+
const { title, tags, body } = request.payload;
72+
const updatedAt = new Date().toISOString();
73+
74+
// Merubah notes menggunakan indexing array
75+
const index = notes.findIndex((note) => note.id === id);
76+
77+
if (index !== -1) {
78+
notes[index] = {
79+
...notes[index],
80+
title,
81+
tags,
82+
body,
83+
updatedAt,
84+
}
85+
86+
const response = h.response({
87+
status: 'success',
88+
message: 'Catatan berhasil diperbaharui',
89+
});
90+
response.code(200);
91+
return response;
92+
}
93+
94+
const response = h.response({
95+
status: 'fail',
96+
message: 'Catatan gagal diperbaharui. Id tidak ditemukan!',
97+
});
98+
response.code(404);
99+
return response;
100+
}
101+
102+
module.exports = {
103+
addNoteHandler,
104+
getAllNotesHandler,
105+
getNoteByIdHandler,
106+
editNoteByIdHandler
107+
};

src/routes.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const { addNoteHandler, getAllNotesHandler, getNoteByIdHandler } = require('./handler');
1+
const { addNoteHandler,
2+
getAllNotesHandler,
3+
getNoteByIdHandler,
4+
editNoteByIdHandler,
5+
} = require('./handler');
26

37
const routes = [
48
{
@@ -15,6 +19,11 @@ const routes = [
1519
method: 'GET',
1620
path: '/notes/{id}',
1721
handler: getNoteByIdHandler,
22+
},
23+
{
24+
method: 'PUT',
25+
path: '/notes/{id}',
26+
handler: editNoteByIdHandler,
1827
}
1928
];
2029

0 commit comments

Comments
 (0)