Get wines by id or search with wine object.

Search with wine object tries to find wines matching name, year and id.
This is used for finding a wine from a prelottery wine where their _id
do not match.
This commit is contained in:
2021-02-18 20:44:54 +01:00
parent 930c458d9c
commit 4bd3b688e9
2 changed files with 27 additions and 2 deletions

View File

@@ -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);

View File

@@ -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
};