Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const responses = require('../responses');
- const CrudController = require('./crud.controller');
- const Book = require('../models').book;
- module.exports = new CrudController(Book, {
- updateById: (req, res) => {
- return Book
- .findById(req.params.id)
- .then((acct) => {
- return acct
- .update(req.body, {
- where: { id: req.params.id },
- fields: ['title', 'author', 'publicationDate', 'isbn']
- })
- .then(responses.ok(res));
- });
- },
- search: (req, res) => {
- let where = {};
- if (req.query.title) {
- where.title = {
- $ilike: `%${req.query.title}%`
- };
- }
- if (req.query.author) {
- where.author = {
- $ilike: `%${req.query.author}%`
- };
- }
- let query = {
- attributes: ['id', 'title', 'author']
- };
- if (Object.keys(where).length !== 0) {
- query.where = where;
- }
- return Book
- .findAll(query)
- .then(responses.ok(res))
- .catch(responses.serverError(res));
- },
- list:(req,res)=>{
- return res.status(200).send( [{
- "title": "\"Satchel: The Life and Times of an American Legend\"",
- "author": "\"Larry Tye\"",
- "publicationDate": "\"2010-05-04\"",
- "isbn": "\"0812977971\""
- },{
- "title": "\"Writing To Learn\"",
- "author": "\"William Zinsser\"",
- "publicationDate": "\"1993-06-04\"",
- "isbn": "\"0062720406\""
- },{
- "title": "\"John Calvin: A Pilgrim's Life\"",
- "author": "\"Herman J. Selderhuis\"",
- "publicationDate": "\"2009-02-21\"",
- "isbn": "\"0830829210\""
- }, {
- "title": "\"The Brazilian Jiu Jitsu Globe Trotter\"",
- "author": "\"Christian Gaugart\"",
- "publicationDate": "\"2012-11-13\"",
- "isbn": "\"1479104523\""
- }]
- )
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement