Aliendreamer

book controler

Jan 15th, 2021
605
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const responses = require('../responses');
  2. const CrudController = require('./crud.controller');
  3.  
  4. const Book = require('../models').book;
  5.  
  6. module.exports = new CrudController(Book, {
  7.  
  8.   updateById: (req, res) => {
  9.     return Book
  10.       .findById(req.params.id)
  11.       .then((acct) => {
  12.         return acct
  13.           .update(req.body, {
  14.             where: { id: req.params.id },
  15.             fields: ['title', 'author', 'publicationDate', 'isbn']
  16.           })
  17.           .then(responses.ok(res));
  18.       });
  19.   },
  20.  
  21.   search: (req, res) => {
  22.     let where = {};
  23.     if (req.query.title) {
  24.       where.title = {
  25.         $ilike: `%${req.query.title}%`
  26.       };
  27.     }
  28.  
  29.     if (req.query.author) {
  30.       where.author = {
  31.         $ilike: `%${req.query.author}%`
  32.       };
  33.     }
  34.  
  35.     let query = {
  36.       attributes: ['id', 'title', 'author']
  37.     };
  38.  
  39.     if (Object.keys(where).length !== 0) {
  40.       query.where = where;
  41.     }
  42.  
  43.     return Book
  44.       .findAll(query)
  45.       .then(responses.ok(res))
  46.       .catch(responses.serverError(res));
  47.   },
  48.   list:(req,res)=>{
  49.       return res.status(200).send( [{
  50.         "title": "\"Satchel: The Life and Times of an American Legend\"",
  51.         "author": "\"Larry Tye\"",
  52.         "publicationDate": "\"2010-05-04\"",
  53.         "isbn": "\"0812977971\""
  54.       },{
  55.         "title": "\"Writing To Learn\"",
  56.         "author": "\"William Zinsser\"",
  57.         "publicationDate": "\"1993-06-04\"",
  58.         "isbn": "\"0062720406\""
  59.       },{
  60.         "title": "\"John Calvin: A Pilgrim's Life\"",
  61.         "author": "\"Herman J. Selderhuis\"",
  62.         "publicationDate": "\"2009-02-21\"",
  63.         "isbn": "\"0830829210\""
  64.       }, {
  65.         "title": "\"The Brazilian Jiu Jitsu Globe Trotter\"",
  66.         "author": "\"Christian Gaugart\"",
  67.         "publicationDate": "\"2012-11-13\"",
  68.         "isbn": "\"1479104523\""
  69.       }]
  70.       )
  71.     }
  72. });
RAW Paste Data