File tree Expand file tree Collapse file tree 2 files changed +50
-2
lines changed Expand file tree Collapse file tree 2 files changed +50
-2
lines changed Original file line number Diff line number Diff line change @@ -65,4 +65,43 @@ const getNoteByIdHandler = (request, h) => {
65
65
return response ;
66
66
}
67
67
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
+ } ;
Original file line number Diff line number Diff line change 1
- const { addNoteHandler, getAllNotesHandler, getNoteByIdHandler } = require ( './handler' ) ;
1
+ const { addNoteHandler,
2
+ getAllNotesHandler,
3
+ getNoteByIdHandler,
4
+ editNoteByIdHandler,
5
+ } = require ( './handler' ) ;
2
6
3
7
const routes = [
4
8
{
@@ -15,6 +19,11 @@ const routes = [
15
19
method : 'GET' ,
16
20
path : '/notes/{id}' ,
17
21
handler : getNoteByIdHandler ,
22
+ } ,
23
+ {
24
+ method : 'PUT' ,
25
+ path : '/notes/{id}' ,
26
+ handler : editNoteByIdHandler ,
18
27
}
19
28
] ;
20
29
You can’t perform that action at this time.
0 commit comments