split services / delete dish / mail init

This commit is contained in:
2020-08-28 18:54:30 +02:00
parent 3674e4ce3c
commit f304463b46
17 changed files with 507 additions and 204 deletions

View File

@@ -1,4 +1,10 @@
import express from "express";
import { createRestaurant } from "../services/dataPrepServices.js";
import {
addRestaurantToUser,
fetchRestaurant,
fetchAllDishesForRestaurant,
} from "../services/databaseServices.js";
import * as services from "../services/services.js";
import Restaurant from "../models/restaurant.js";
@@ -21,9 +27,10 @@ router.get("/", async (req, res) => {
router.post("/", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
services.validateUserToken(token);
const restaurant = services.createRestaurant(req);
const user = services.validateUserToken(token);
const restaurant = createRestaurant(req.body);
await restaurant.save();
await addRestaurantToUser(user, restaurant);
res.sendStatus(201);
} catch (error) {
services.handleError(error, res);
@@ -36,8 +43,8 @@ router.get("/dishes", async (req, res) => {
try {
const query = services.decodeAndSanitize(req.query.restaurantId);
await services.validateRestaurant(query);
let restaurant = await services.fetchRestaurant(query);
let dishes = await services.fetchAllDishesForRestaurant(restaurant);
let restaurant = await fetchRestaurant(query);
let dishes = await fetchAllDishesForRestaurant(restaurant);
res.send(dishes);
} catch (error) {
services.handleError(error, res);