Skip to content

Commit

Permalink
add handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yasiramri committed Feb 20, 2025
1 parent 9fc88c9 commit 3d92c96
Showing 1 changed file with 87 additions and 120 deletions.
207 changes: 87 additions & 120 deletions src/api/notes/handler.js
Original file line number Diff line number Diff line change
@@ -1,136 +1,103 @@
/* eslint-disable linebreak-style */
const { nanoid } = require('nanoid');
const notes = require('../../notes');

const addNoteHandler = (request, h) => {
const { title = 'untitled', tags, body } = request.payload;

const id = nanoid(16);
const createdAt = new Date().toISOString();
const updatedAt = createdAt;

const newNote = {
title,
tags,
body,
id,
createdAt,
updatedAt,
};

notes.push(newNote);

const isSuccess = notes.filter((note) => note.id === id).length > 0;

if (isSuccess) {
const response = h.response({
status: 'success',
message: 'Catatan berhasil ditambahkan',
data: {
noteId: id,
},
});
response.code(201);
return response;
class NotesHandler {
constructor(service) {
this._service = service;
}

const response = h.response({
status: 'fail',
message: 'Catatan gagal ditambahkan',
});
response.code(500);
return response;
};

const getAllNotesHandler = () => ({
status: 'success',
data: {
notes,
},
});

const getNoteByIdHandler = (request, h) => {
const { id } = request.params;

const note = notes.filter((n) => n.id === id)[0];
postNoteHandler(request, h) {
try {
const { title = 'untitled', body, tags } = request.payload;

this._service.addNote({ title, body, tags });

const noteId = this._service.addNote({ title, body, tags });

const response = h.response({
status: 'success',
message: 'Catatan Berhasil ditambahkan',
data: {
noteId,
},
});
response.code(201);
return response;
} catch (error) {
const response = h.response({
status: 'fail',
message: error.message,
});
response.code(400);
return response;
}
}

if (note !== undefined) {
getNotesHandler() {
const notes = this._service.getNotes();
return {
status: 'success',
data: {
note,
notes,
},
};
}

const response = h.response({
status: 'fail',
message: 'Catatan tidak ditemukan',
});
response.code(404);
return response;
};

const editNoteByIdHandler = (request, h) => {
const { id } = request.params;

const { title, tags, body } = request.payload;
const updatedAt = new Date().toISOString();

const index = notes.findIndex((note) => note.id === id);

if (index !== -1) {
notes[index] = {
...notes[index],
title,
tags,
body,
updatedAt,
};

const response = h.response({
status: 'success',
message: 'Catatan berhasil diperbarui',
});
response.code(200);
return response;
getNoteByIdHandler(request, h) {
try {
const { id } = request.params;
const note = this.service.getNoteByIdHandler(id);
return {
status: 'success',
data: {
note,
},
};
} catch (error) {
const response = h.response({
status: 'Fail',
message: error.message,
});
response.code(404);
return response;
}
}

const response = h.response({
status: 'fail',
message: 'Gagal memperbarui catatan. Id tidak ditemukan',
});
response.code(404);
return response;
};

const deleteNoteByIdHandler = (request, h) => {
const { id } = request.params;

const index = notes.findIndex((note) => note.id === id);

if (index !== -1) {
notes.splice(index, 1);
const response = h.response({
status: 'success',
message: 'Catatan berhasil dihapus',
});
response.code(200);
return response;
putNoteByIdHandler(request, h) {
try {
const { id } = request.params;
this._service.editNoteById(id, request.payload);

return {
status: 'success',
message: 'Catatan berhasil ditambahkan',
};
} catch (error) {
const response = h.response({
status: 'fail',
message: error.message,
});
response.code(404);
return response;
}
}

const response = h.response({
status: 'fail',
message: 'Catatan gagal dihapus. Id tidak ditemukan',
});
response.code(404);
return response;
};
deleteNoteByIdHandler(request, h) {
try {
const { id } = request.params;
this._service.deleteNoteById(id);

return {
status: 'success',
message: 'Catatan berhasil dihapus',
};
} catch (error) {
const response = h.response({
status: 'fail',
message: error.message,
});
response.code(404);
return response;
}
}
}

module.exports = {
addNoteHandler,
getAllNotesHandler,
getNoteByIdHandler,
editNoteByIdHandler,
deleteNoteByIdHandler,
};
module.exports = NotesHandler;

0 comments on commit 3d92c96

Please sign in to comment.