diff --git a/api/router.js b/api/router.js index 5ab7f16..af2546a 100644 --- a/api/router.js +++ b/api/router.js @@ -27,7 +27,8 @@ router.get("/requests", setAdminHeaderIfAuthenticated, requestController.allRequ router.post("/request", requestController.addRequest); router.delete("/request/:id", mustBeAuthenticated, requestController.deleteRequest); -// router.get("/wines", wineController.all); // sort = by-date, by-name, by-occurences +router.get("/wines", wineController.allWines); // sort = by-date, by-name, by-occurences +router.get("/wine/:id", wineController.wineById); // sort = by-date, by-name, by-occurences // router.update("/wine/:id", mustBeAuthenticated, wineController.update); router.get("/history", historyController.all); diff --git a/api/wine.js b/api/wine.js index d4bca3c..a15b05d 100644 --- a/api/wine.js +++ b/api/wine.js @@ -1,6 +1,8 @@ const path = require("path"); const Wine = require(path.join(__dirname, "/schemas/Wine")); +const { WineNotFound } = require(path.join(__dirname, "/vinlottisErrors")); + const addWine = async wine => { let existingWine = await Wine.findOne({ name: wine.name, id: wine.id, year: wine.year }); @@ -29,7 +31,29 @@ const allWines = () => { return Wine.find(); }; +const wineById = id => { + return Wine.findOne({ _id: id }).then(wine => { + if (wine == null) { + throw new WineNotFound(); + } + + return wine; + }); +}; + +const findWine = wine => { + return Wine.findOne({ name: wine.name, id: wine.id, year: wine.year }).then(wine => { + if (wine == null) { + throw new WineNotFound(); + } + + return wine; + }); +}; + module.exports = { addWine, - allWines + allWines, + wineById, + findWine };