Advertisement
yahyaaa

part of handler.js

Apr 19th, 2021
1,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getAllBookHandler = (request, h) => {
  2.   const { name, reading, finished } = request.query;
  3.   let response;
  4.   let ket;
  5.   if (reading !== undefined || finished !== undefined || name !== undefined) {
  6.     if (reading === '1' || finished === '1') {
  7.       ket = true;
  8.     } else {
  9.       ket = false;
  10.     }
  11.     if (reading) {
  12.       response = h.response({
  13.         status: 'success',
  14.         data: {
  15.           books: books.filter((x) => x.reading === ket).map((book) => ({
  16.             id: book.id,
  17.             name: book.name,
  18.             publisher: book.publisher,
  19.           })),
  20.         },
  21.       });
  22.     }
  23.     if (finished) {
  24.       response = h.response({
  25.         status: 'success',
  26.         data: {
  27.           books: books.filter((x) => x.finished === ket).map((book) => ({
  28.             id: book.id,
  29.             name: book.name,
  30.             publisher: book.publisher,
  31.           })),
  32.         },
  33.       });
  34.     }
  35.     if (name) {
  36.       response = h.response({
  37.         status: 'success',
  38.         data: {
  39.           books: books.filter((x) => x.name.toLowerCase().includes(name.toLowerCase()))
  40.             .map((book) => ({
  41.               id: book.id,
  42.               name: book.name,
  43.               publisher: book.publisher,
  44.             })),
  45.         },
  46.       });
  47.     }
  48.   } else {
  49.     response = h.response({
  50.       status: 'success',
  51.       data: {
  52.         books: books.map((book) => ({
  53.           id: book.id,
  54.           name: book.name,
  55.           publisher: book.publisher,
  56.         })),
  57.       },
  58.     });
  59.   }
  60.   return response;
  61. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement