From 3dbbe4b1ba226cd8fa74bee5d747f9c0f9eadfc4 Mon Sep 17 00:00:00 2001 From: Jonasz Bigda Date: Sat, 3 Oct 2020 18:54:59 +0200 Subject: [PATCH] server v1.0.8 --- routes/routeRestaurant.js | 22 +++++++++++++++-- services/dataPrepServices.js | 2 ++ services/databaseServices.js | 47 ++++++++++++++++++++++++++++++++---- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/routes/routeRestaurant.js b/routes/routeRestaurant.js index 63b28c8..1b39f03 100644 --- a/routes/routeRestaurant.js +++ b/routes/routeRestaurant.js @@ -7,6 +7,7 @@ import { removeRestaurant, changeCategory, changeLunchMenu, + changeLunchMenuSet, fetchUser, } from "../services/databaseServices.js"; import { @@ -103,6 +104,23 @@ router.post("/category", async (req, res) => { // CHANGE LUNCH MENU +router.post("/lunchSet", async (req, res) => { + try { + const token = req.headers["x-auth-token"]; + const user = validateUserToken(token); + await validateRestaurant(req.body.restaurantId); + await verifyRestaurantAccess(req.body.restaurantId, user); + await changeLunchMenuSet( + req.body.restaurantId, + req.body.action, + req.body.set + ); + res.sendStatus(200); + } catch (error) { + handleError(error, res); + } +}); + router.post("/lunch", async (req, res) => { try { const token = req.headers["x-auth-token"]; @@ -111,11 +129,11 @@ router.post("/lunch", async (req, res) => { await verifyRestaurantAccess(req.body.restaurantId, user); await changeLunchMenu( req.body.restaurantId, + req.body.setName, req.body.dishId, req.body.action ); - const restaurant = await fetchRestaurant(req.body.restaurantId); - res.send(restaurant); + res.sendStatus(200); } catch (error) { handleError(error, res); } diff --git a/services/dataPrepServices.js b/services/dataPrepServices.js index 08b7df3..56d3af2 100644 --- a/services/dataPrepServices.js +++ b/services/dataPrepServices.js @@ -63,6 +63,7 @@ export async function createRestaurant(request, oldRestaurant) { placesId: request.placesId, imgUrl: img, workingHours: request.workingHours, + lunchHours: request.lunchHours, description: sanitizer.sanitize.keepUnicode(request.description), tags: request.tags, links: request.links, @@ -84,6 +85,7 @@ export async function createRestaurant(request, oldRestaurant) { placesId: request.placesId, imgUrl: img, workingHours: request.workingHours, + lunchHours: request.lunchHours, description: sanitizer.sanitize.keepUnicode(request.description), tags: request.tags, links: request.links, diff --git a/services/databaseServices.js b/services/databaseServices.js index d650951..0ec4621 100644 --- a/services/databaseServices.js +++ b/services/databaseServices.js @@ -120,10 +120,25 @@ async function checkIfCategoryExists(restaurant, category) { } } -async function checkIfAlreadyInLunchMenu(restaurant, dishId) { +async function checkIfSetAlreadyInLunchMenu(restaurant, setName) { const lunchMenu = restaurant.lunchMenu; - if (lunchMenu.includes(dishId)) { - throw newError("Podane danie jest już w lunch menu", 200); + for (lunchSet of lunchMenu) { + if (lunchSet.lunchSetName === setName) { + throw newError("Nazwa zestawu jest zajęta", 409); + } + return; + } +} + +async function checkIfAlreadyInSet(restaurant, setName, dishId) { + const lunchMenu = restaurant.lunchMenu; + for (lunchSet of lunchMenu) { + if (lunchSet.lunchSetName === setName) { + const dishes = lunchSet.lunchSetDishes; + if (dishes.includes(dishId)) { + throw newError("Danie jest już w podanym zestawie", 500); + } + } } } @@ -157,12 +172,34 @@ export async function setDishVisibility(dishId, visible) { ); } -export async function changeLunchMenu(restaurantId, dishId, action) { +export async function changeLunchMenuSet(restaurantId, action, lunchSet) { if (action === "add") { const restaurant = await Restaurant.findById(restaurantId).catch((err) => { throw newError("Nie udało się pobrać restauracji.", 404); }); - await checkIfAlreadyInLunchMenu(restaurant, dishId); + await checkIfSetAlreadyInLunchMenu(restaurant, lunchSet.lunchSetName); + await Restaurant.findByIdAndUpdate(restaurantId, { + $push: { lunchMenu: lunchSet }, + }).catch((e) => { + throw newError("Nie udało się dodać zestawu do lunch menu.", 500); + }); + } else if (action === "delete") { + await Restaurant.findByIdAndUpdate(restaurantId, { + $pull: { lunchMenu: lunchSet }, + }).catch((e) => { + throw newError("Nie udało się usunąć zestawu.", 500); + }); + } else { + throw newError("Nie sprecyzowano akcji", 500); + } +} + +export async function changeLunchMenu(restaurantId, setName, dishId, action) { + if (action === "add") { + const restaurant = await Restaurant.findById(restaurantId).catch((err) => { + throw newError("Nie udało się pobrać restauracji.", 404); + }); + await checkIfAlreadyInSet(restaurant, setName, dishId); await Restaurant.findByIdAndUpdate(restaurantId, { $push: { lunchMenu: dishId }, }).catch((e) => {