From 9823197a480b747b240da810d12b04ec43eeec50 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Thu, 18 Feb 2021 21:06:49 +0100 Subject: [PATCH] Better validation and error resp validating wines. --- api/controllers/lotteryWineController.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/api/controllers/lotteryWineController.js b/api/controllers/lotteryWineController.js index 7274dac..38685ed 100644 --- a/api/controllers/lotteryWineController.js +++ b/api/controllers/lotteryWineController.js @@ -21,7 +21,7 @@ const allWines = (req, res) => { }; const addWines = (req, res) => { - const { wines } = req.body; + let { wines } = req.body; if (!(wines instanceof Array)) { return res.status(400).send({ @@ -36,7 +36,7 @@ const addWines = (req, res) => { return Promise.all( requiredAttributes.map(attr => { - if (typeof wine[attr] === "undefined") { + if (typeof wine[attr] === "undefined" || wine[attr] == "") { return Promise.reject({ message: `Incorrect or missing attribute: ${attr}.`, statusCode: 400, @@ -96,6 +96,13 @@ const updateWineById = (req, res) => { const { id } = req.params; const { wine } = req.body; + if (id == null || id == "undefined") { + return res.status(400).send({ + message: "Unable to update without id.", + success: false + }); + } + return prelotteryWineRepository .updateWineById(id, wine) .then(updatedWine => {