split services / delete dish / mail init
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import express from "express";
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
import { createDish } from "../services/dataPrepServices.js";
|
||||
import {
|
||||
removeDish,
|
||||
addDishToRestaurant,
|
||||
} from "../services/databaseServices.js";
|
||||
import * as services from "../services/services.js";
|
||||
import Dish from "../models/dish.js";
|
||||
|
||||
@@ -22,9 +26,25 @@ router.post("/", async (req, res) => {
|
||||
await services.validateRestaurant(req.body.restaurantId);
|
||||
const token = req.headers["x-auth-token"];
|
||||
services.validateUserToken(token);
|
||||
const dish = services.createDish(req.body.dish, true);
|
||||
const dish = createDish(req.body.dish, req.body.restaurantId, true);
|
||||
await dish.save();
|
||||
await services.addDishToRestaurant(req.body.restaurantId, dish._id);
|
||||
await addDishToRestaurant(req.body.restaurantId, dish._id);
|
||||
res.status(201).send(dish._id);
|
||||
} catch (error) {
|
||||
services.handleError(error, res);
|
||||
}
|
||||
});
|
||||
|
||||
// REMOVE DISH
|
||||
|
||||
router.delete("/", async (req, res) => {
|
||||
try {
|
||||
await services.validateDishId(req.body.dishId);
|
||||
const token = req.headers["x-auth-token"];
|
||||
const decodedToken = services.validateUserToken(token);
|
||||
await services.verifyDishAccess(req.body.dishId, decodedToken);
|
||||
await removeDish(req.body.dishId);
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
services.handleError(error, res);
|
||||
}
|
||||
@@ -34,10 +54,10 @@ router.post("/", async (req, res) => {
|
||||
|
||||
router.put("/", async (req, res) => {
|
||||
try {
|
||||
services.validateDishId(req.body.dishId);
|
||||
await services.validateDishId(req.body.dishId);
|
||||
const token = req.headers["x-auth-token"];
|
||||
services.validateUserToken(token);
|
||||
const dish = services.createDish(req.body.dish, false);
|
||||
const dish = createDish(req.body.dish, req.body.restaurantId, false);
|
||||
await Dish.replaceOne({ _id: req.body.dishId }, dish);
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user