From 1434918f8ee56e03176d3c32f27b877b16ec4324 Mon Sep 17 00:00:00 2001 From: Jonasz Bigda Date: Sat, 6 Feb 2021 15:25:07 +0100 Subject: [PATCH] fixes --- models/dish.js | 8 ++++++ models/restaurant.js | 7 ++++++ routes/routeRestaurant.js | 2 ++ services/dataPrepServices.js | 1 + services/databaseServices.js | 47 ++++++++++++++++++++++++++++-------- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/models/dish.js b/models/dish.js index 3a4d9bc..e42b41a 100644 --- a/models/dish.js +++ b/models/dish.js @@ -70,6 +70,14 @@ const dishSchema = mongoose.Schema({ kCal: String, vegan: Boolean, vegetarian: Boolean, + photos: [mongoose.Types.ObjectId], + ratings: [ + { + id: mongoose.Types.ObjectId, + rate: Number, + owner: mongoose.Types.ObjectId + } + ] }); module.exports = mongoose.model("Dish", dishSchema); diff --git a/models/restaurant.js b/models/restaurant.js index 25206ec..175fcf0 100644 --- a/models/restaurant.js +++ b/models/restaurant.js @@ -127,6 +127,13 @@ const restaurantSchema = mongoose.Schema({ }, ], dishes: [mongoose.Types.ObjectId], + ratings: [ + { + rate: Number, + owner: mongoose.Types.ObjectId, + id: mongoose.Types.ObjectId + } + ] }); module.exports = mongoose.model("Restaurant", restaurantSchema); diff --git a/routes/routeRestaurant.js b/routes/routeRestaurant.js index d43a82a..ad888e9 100644 --- a/routes/routeRestaurant.js +++ b/routes/routeRestaurant.js @@ -128,6 +128,8 @@ router.post("/lunchSet", async (req, res) => { } }); +// CHANGE LUNCH SET ITEM + router.post("/lunch", async (req, res) => { try { const token = req.headers["x-auth-token"]; diff --git a/services/dataPrepServices.js b/services/dataPrepServices.js index 14fb1f1..b552511 100644 --- a/services/dataPrepServices.js +++ b/services/dataPrepServices.js @@ -169,6 +169,7 @@ async function createDish(dish, restaurantId, oldDish) { } function appendDishToLunchSet(lunchMenu, setName, dishId, quantity) { + console.log("append called") const result = lunchMenu.map((lunchSet) => { if (lunchSet.lunchSetName === setName) { let updatedSet = lunchSet; diff --git a/services/databaseServices.js b/services/databaseServices.js index ec49007..dc6b02d 100644 --- a/services/databaseServices.js +++ b/services/databaseServices.js @@ -3,10 +3,6 @@ const Dish = require("../models/dish.js"); const User = require("../models/users.js"); const Payments = require("../models/payments.js"); const { deleteImage } = require("./oceanServices.js"); -const { - appendDishToLunchSet, - removeDishFromLunchSet, -} = require("./dataPrepServices.js"); const { newError } = require("./services.js"); const mongoose = require("mongoose"); const axios = require("axios"); @@ -229,6 +225,42 @@ async function changeLunchMenuSet(restaurantId, action, lunchSet) { } } +function appendDishToLunchSet(lunchMenu, setName, dishId, quantity) { + const result = lunchMenu.map((lunchSet) => { + if (lunchSet.lunchSetName === setName) { + let updatedSet = lunchSet; + let dishToAdd = { + dishId: dishId, + quantity: quantity + } + updatedSet.lunchSetDishes.push(dishToAdd); + return updatedSet; + } else { + return lunchSet; + } + }); + return result; +} + +function removeDishFromLunchSet(lunchMenu, setName, dishId) { + console.log("remove called") + const result = lunchMenu.map((lunchSet) => { + if (lunchSet.lunchSetName === setName) { + let updatedSet = lunchSet; + const index = updatedSet.lunchSetDishes.findIndex((dish) => { + return dish.dishId.toString() === dishId.toString(); + }); + if (index > -1) { + updatedSet.lunchSetDishes.splice(index, 1); + } + return updatedSet; + } else { + return lunchSet; + } + }); + return result; +} + async function changeLunchMenu(restaurantId, setName, dishId, quantity, action) { if (action === "add") { const restaurant = await Restaurant.findById(restaurantId).catch((err) => { @@ -269,16 +301,11 @@ async function fetchRestaurant(id) { const data = await Restaurant.findById(id).catch((e) => { throw newError("Nie udało się pobrać restauracji.", 500); }); - console.log("fetched restaurant"); return data; } async function fetchMultipleRestaurants(idArray) { - let data = []; - for (const id of idArray) { - let response = await fetchRestaurant(id); - data.push(response); - } + let data = await Restaurant.find().where('_id').in(idArray).exec(); return data; }