Advertisement
dijualrumahjakarta

handles.js

Dec 5th, 2022
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. terpenuhi :
  2.  
  3. API dapat menyimpan buku
  4.  
  5. API dapat menampilkan seluruh buku
  6.  
  7. API dapat menampilkan detail buku
  8.  
  9. API dapat menghapus buku
  10.  
  11.  
  12. Perlu diperbaiki
  13. API dapat mengubah data buku
  14.  
  15.  
  16. File :handles.js
  17.  
  18. const editBookByIdHandler = (request, h) => {
  19.     const { id } = request.params;
  20.  
  21.     const { name, year, author, summary, publisher, pageCount, readPage, reading } = request.payload;
  22.     const index = books.findIndex((book) => book.id === id);   //id buku gk ditemukan
  23.     if(index !== -1){
  24.         if(!name){
  25.             const response = h.response({
  26.                 status: 'fail',
  27.                 message: 'Gagal Mengupdate buku. Mohon isi nama buku'
  28.             });
  29.             response.code(400);
  30.             return response;
  31.         }
  32.    
  33.         if(readPage > pageCount){
  34.             const response = h.response({
  35.                 status: 'fail',
  36.                 message: 'Gagal memperbarui buku. readPage tidak boleh lebih besar dari pageCount'
  37.             });
  38.             response.code(400);
  39.             return response;
  40.         }
  41.  
  42.       //  uodate ke dlm base
  43.         const finished = pageCount == readPage ? true : false;
  44.         const updatedAt = new Date().toISOString();
  45.         books[index] = {
  46.             ...books[index],
  47.             name,
  48.             year,
  49.             author,
  50.             summary,
  51.             publisher,
  52.             pageCount,
  53.             readPage,
  54.             finished,
  55.             reading,
  56.             updatedAt
  57.         }
  58.         const response = h.response({
  59.             status: 'success',
  60.             message: 'Buku berhasil di update'
  61.         });
  62.         response.code(200);
  63.         return response;
  64.     }
  65.     const response = h.response({
  66.         status: 'fail',
  67.         message: 'Tidak berhasil mengupdate buku ini. Id tidak ditemukan'
  68.     });
  69.  
  70.     response.code(404);
  71.     return response;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement