Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- terpenuhi :
- API dapat menyimpan buku
- API dapat menampilkan seluruh buku
- API dapat menampilkan detail buku
- API dapat menghapus buku
- Perlu diperbaiki
- API dapat mengubah data buku
- File :handles.js
- const editBookByIdHandler = (request, h) => {
- const { id } = request.params;
- const { name, year, author, summary, publisher, pageCount, readPage, reading } = request.payload;
- const index = books.findIndex((book) => book.id === id); //id buku gk ditemukan
- if(index !== -1){
- if(!name){
- const response = h.response({
- status: 'fail',
- message: 'Gagal Mengupdate buku. Mohon isi nama buku'
- });
- response.code(400);
- return response;
- }
- if(readPage > pageCount){
- const response = h.response({
- status: 'fail',
- message: 'Gagal memperbarui buku. readPage tidak boleh lebih besar dari pageCount'
- });
- response.code(400);
- return response;
- }
- // uodate ke dlm base
- const finished = pageCount == readPage ? true : false;
- const updatedAt = new Date().toISOString();
- books[index] = {
- ...books[index],
- name,
- year,
- author,
- summary,
- publisher,
- pageCount,
- readPage,
- finished,
- reading,
- updatedAt
- }
- const response = h.response({
- status: 'success',
- message: 'Buku berhasil di update'
- });
- response.code(200);
- return response;
- }
- const response = h.response({
- status: 'fail',
- message: 'Tidak berhasil mengupdate buku ini. Id tidak ditemukan'
- });
- response.code(404);
- return response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement