From 80e6c91045b20d5f725f84fafbff1d7ef6b7474a Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Sat, 20 Feb 2021 13:29:41 +0100 Subject: [PATCH] API endpoint for getting latest lottery. --- api/controllers/lotteryController.js | 23 ++++++++++++++++++++++- api/lottery.js | 7 ++++++- api/router.js | 1 + 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/api/controllers/lotteryController.js b/api/controllers/lotteryController.js index ab446cc..199548c 100644 --- a/api/controllers/lotteryController.js +++ b/api/controllers/lotteryController.js @@ -139,6 +139,26 @@ const allLotteries = (req, res) => { }); }; +const latestLottery = (req, res) => { + return lotteryRepository + .latestLottery() + .then(lottery => + res.send({ + lottery, + message: "Latest lottery.", + success: true + }) + ) + .catch(error => { + const { statusCode, message } = error; + + return res.status(statusCode || 500).send({ + message: message || "Unexpected error occured while fetching all lotteries.", + success: false + }); + }); +}; + function verifyLotteryPayload(raffles, stolen, wines) { return new Promise((resolve, reject) => { if (raffles == undefined || !raffles instanceof Array) { @@ -188,5 +208,6 @@ module.exports = { drawWinner, archiveLottery, lotteryByDate, - allLotteries + allLotteries, + latestLottery }; diff --git a/api/lottery.js b/api/lottery.js index 6b1bf90..956e09f 100644 --- a/api/lottery.js +++ b/api/lottery.js @@ -130,6 +130,10 @@ const allLotteriesIncludingWinners = async (sort = "asc", yearFilter = undefined }); }; +const latestLottery = async () => { + return Lottery.findOne().sort({ date: -1 }); +}; + const drawWinner = async () => { let allContestants = await Attendee.find({ winner: false }); @@ -259,5 +263,6 @@ module.exports = { archive, lotteryByDate, allLotteries, - allLotteriesIncludingWinners + allLotteriesIncludingWinners, + latestLottery }; diff --git a/api/router.js b/api/router.js index 77e8661..a9cac58 100644 --- a/api/router.js +++ b/api/router.js @@ -71,6 +71,7 @@ router.delete("/lottery/winner/:id", mustBeAuthenticated, winnerController.delet router.get("/lottery/draw", mustBeAuthenticated, lotteryController.drawWinner); router.post("/lottery/archive", mustBeAuthenticated, lotteryController.archiveLottery); +router.get("/lottery/latest", lotteryController.latestLottery); router.get("/lottery/:epoch", lotteryController.lotteryByDate); router.get("/lotteries/", lotteryController.allLotteries);