From 5073e63e7180c58db49b1ca4cb79a79c1de876ef Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 12 Nov 2018 16:37:01 -0500 Subject: [PATCH] filter and paginate list api --- api/controllers/BooksController.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/api/controllers/BooksController.js b/api/controllers/BooksController.js index 9275ee0..1a34b9b 100644 --- a/api/controllers/BooksController.js +++ b/api/controllers/BooksController.js @@ -11,12 +11,6 @@ const HttpError = require('../errors/HttpError') module.exports = { publish: async function (req, res) { try { - /* - title, - author, - version, - opds - */ const body = req.body const host = req.hostname let result @@ -41,11 +35,10 @@ module.exports = { req.file('opds').upload(sails.config.skipperConfig, async function (err, uploaded) { if (err) { - await result.destroy() + await Book.destroy({ id: result.id }) throw new HttpError(500, err.message) } - result.storage = uploaded.fd - await result.save() + await Book.update({ id: result.id }, { storage: uploaded[0].fd }) return res.json({ ...result }) @@ -61,10 +54,11 @@ module.exports = { list: async function (req, res) { try { const body = req.allParams() - if (!body) { throw new HttpError(400, 'Missing parameters') } - - const books = await Book.find(body) + const numPerPage = 50 + if (!body) throw new HttpError(400, 'Missing parameters') + const page = body.page || 1 + const books = await Book.find(body).sort('createdAt DESC').skip(page * numPerPage - numPerPage).limit(numPerPage) if (!books.length) throw new HttpError(404, 'No books matching those parameters were found.') return res.json(books)