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:
@@ -27,7 +27,8 @@ router.get("/requests", setAdminHeaderIfAuthenticated, requestController.allRequ
|
|||||||
router.post("/request", requestController.addRequest);
|
router.post("/request", requestController.addRequest);
|
||||||
router.delete("/request/:id", mustBeAuthenticated, requestController.deleteRequest);
|
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.update("/wine/:id", mustBeAuthenticated, wineController.update);
|
||||||
|
|
||||||
router.get("/history", historyController.all);
|
router.get("/history", historyController.all);
|
||||||
|
|||||||
26
api/wine.js
26
api/wine.js
@@ -1,6 +1,8 @@
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
const Wine = require(path.join(__dirname, "/schemas/Wine"));
|
const Wine = require(path.join(__dirname, "/schemas/Wine"));
|
||||||
|
|
||||||
|
const { WineNotFound } = require(path.join(__dirname, "/vinlottisErrors"));
|
||||||
|
|
||||||
const addWine = async wine => {
|
const addWine = async wine => {
|
||||||
let existingWine = await Wine.findOne({ name: wine.name, id: wine.id, year: wine.year });
|
let existingWine = await Wine.findOne({ name: wine.name, id: wine.id, year: wine.year });
|
||||||
|
|
||||||
@@ -29,7 +31,29 @@ const allWines = () => {
|
|||||||
return Wine.find();
|
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 = {
|
module.exports = {
|
||||||
addWine,
|
addWine,
|
||||||
allWines
|
allWines,
|
||||||
|
wineById,
|
||||||
|
findWine
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user