From 2477f36f96dbc0512668e413dc5e68116ff4b795 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Thu, 18 Feb 2021 22:07:54 +0100 Subject: [PATCH] Get all wines with limit parameter. --- api/controllers/historyController.js | 9 +++++++++ api/controllers/wineController.js | 12 +++++++++++- api/wine.js | 8 ++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/api/controllers/historyController.js b/api/controllers/historyController.js index a62634a..2eb070f 100644 --- a/api/controllers/historyController.js +++ b/api/controllers/historyController.js @@ -222,6 +222,15 @@ const orderByWins = (req, res) => { }); } + if (limit && isNaN(limit)) { + return res.status(400).send({ + message: "If limit query parameter is provided it must be a number", + success: false + }); + } else if (!!!isNaN(limit)) { + limit = Number(limit); + } + return historyRepository .orderByWins(includeWines == "true", limit) .then(winners => diff --git a/api/controllers/wineController.js b/api/controllers/wineController.js index d9e8cc6..677bee6 100644 --- a/api/controllers/wineController.js +++ b/api/controllers/wineController.js @@ -3,9 +3,19 @@ const wineRepository = require(path.join(__dirname, "../wine")); const allWines = (req, res) => { // TODO add "includeWinners" + let { limit } = req.query; + + if (limit && isNaN(limit)) { + return res.status(400).send({ + message: "If limit query parameter is provided it must be a number", + success: false + }); + } else if (!!!isNaN(limit)) { + limit = Number(limit); + } return wineRepository - .allWines() + .allWines(limit) .then(wines => res.send({ wines: wines, diff --git a/api/wine.js b/api/wine.js index a15b05d..cd35bb4 100644 --- a/api/wine.js +++ b/api/wine.js @@ -27,8 +27,12 @@ const addWine = async wine => { } }; -const allWines = () => { - return Wine.find(); +const allWines = (limit = undefined) => { + if (limit) { + return Wine.find().limit(limit); + } else { + return Wine.find(); + } }; const wineById = id => {