From 3f77722f4f3268efc8554988ec1b797cc04e258e Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Thu, 18 Feb 2021 23:42:21 +0100 Subject: [PATCH] allLotteries w/ sort & includeWinners query opts. --- api/controllers/lotteryController.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/api/controllers/lotteryController.js b/api/controllers/lotteryController.js index 054821d..ab446cc 100644 --- a/api/controllers/lotteryController.js +++ b/api/controllers/lotteryController.js @@ -103,11 +103,25 @@ const lotteryByDate = (req, res) => { }); }; +const sortOptions = ["desc", "asc"]; const allLotteries = (req, res) => { - const isAdmin = req.isAuthenticated(); + let { includeWinners, year, sort } = req.query; - return lotteryRepository - .allLotteries(isAdmin) + if (sort !== undefined && !sortOptions.includes(sort)) { + return res.status(400).send({ + message: `Sort option must be: '${sortOptions.join(", ")}'`, + success: false + }); + } else if (sort === undefined) { + sort = "asc"; + } + + let allLotteriesFunction = lotteryRepository.allLotteries; + if (includeWinners === "true") { + allLotteriesFunction = lotteryRepository.allLotteriesIncludingWinners; + } + + return allLotteriesFunction(sort, year) .then(lotteries => res.send({ lotteries,