This commit is contained in:
2021-02-06 15:25:07 +01:00
parent 047f2660f2
commit 1434918f8e
5 changed files with 55 additions and 10 deletions

View File

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

View File

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

View File

@@ -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"];

View File

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

View File

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