server v1.0.4

+ working hours for a whole week
+ dish visibility API
+ categories API
+ lunch menu API
+ updated Readme
This commit is contained in:
2020-09-23 18:41:02 +02:00
parent d7395f5472
commit 3a6a6f4c5c
7 changed files with 194 additions and 23 deletions

View File

@@ -3,8 +3,15 @@ import { createDish } from "../services/dataPrepServices.js";
import {
removeDish,
addDishToRestaurant,
setDishVisibility,
} from "../services/databaseServices.js";
import * as services from "../services/services.js";
import {
validateRestaurant,
validateUserToken,
validateDishId,
handleError,
verifyDishAccess,
} from "../services/services.js";
import Dish from "../models/dish.js";
var router = express.Router();
@@ -23,15 +30,29 @@ router.get("/", (req, res) => {
router.post("/", async (req, res) => {
try {
await services.validateRestaurant(req.body.restaurantId);
await validateRestaurant(req.body.restaurantId);
const token = req.headers["x-auth-token"];
services.validateUserToken(token);
validateUserToken(token);
const dish = createDish(req.body.dish, req.body.restaurantId, true);
await dish.save();
await addDishToRestaurant(req.body.restaurantId, dish._id);
res.status(201).send(dish._id);
} catch (error) {
services.handleError(error, res);
handleError(error, res);
}
});
// HIDE, UNHIDE DISH
router.post("/hidden", async (req, res) => {
try {
await validateDishId(req.body.dishId);
const token = req.headers["x-auth-token"];
validateUserToken(token);
await setDishVisibility(req.body.dishId, req.body.visible);
res.sendStatus(200);
} catch (error) {
handleError(error, res);
}
});
@@ -39,14 +60,14 @@ router.post("/", async (req, res) => {
router.delete("/", async (req, res) => {
try {
await services.validateDishId(req.body.dishId);
await validateDishId(req.body.dishId);
const token = req.headers["x-auth-token"];
const decodedToken = services.validateUserToken(token);
await services.verifyDishAccess(req.body.dishId, decodedToken);
const decodedToken = validateUserToken(token);
await verifyDishAccess(req.body.dishId, decodedToken);
await removeDish(req.body.dishId);
res.sendStatus(200);
} catch (error) {
services.handleError(error, res);
handleError(error, res);
}
});
@@ -54,14 +75,14 @@ router.delete("/", async (req, res) => {
router.put("/", async (req, res) => {
try {
await services.validateDishId(req.body.dishId);
await validateDishId(req.body.dishId);
const token = req.headers["x-auth-token"];
services.validateUserToken(token);
validateUserToken(token);
const dish = createDish(req.body.dish, req.body.restaurantId, false);
await Dish.replaceOne({ _id: req.body.dishId }, dish);
res.sendStatus(200);
} catch (error) {
services.handleError(error, res);
handleError(error, res);
}
});

View File

@@ -5,6 +5,8 @@ import {
fetchRestaurant,
fetchAllDishesForRestaurant,
removeRestaurant,
changeCategory,
changeLunchMenu,
} from "../services/databaseServices.js";
import {
decodeAndSanitize,
@@ -76,6 +78,44 @@ router.get("/dishes", async (req, res) => {
}
});
// CHANGE CATEGORY
router.post("/category", 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 changeCategory(
req.body.restaurantId,
req.body.category,
req.body.action
);
res.send("Kategoria zmieniona pomyślnie");
} catch (error) {
handleError(error, res);
}
});
// CHANGE LUNCH MENU
router.post("/lunch", 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 changeLunchMenu(
req.body.restaurantId,
req.body.dishId,
req.body.action
);
res.send("Lunch menu zmienione pomyślnie.");
} catch (error) {
handleError(error, res);
}
});
// DELETE RESTAURANT
router.post("/delete", async (req, res) => {

View File

@@ -1,16 +1,10 @@
import express from "express";
import * as services from "../services/services.js";
import * as databaseServices from "../services/databaseServices.js";
var router = express.Router();
router.post("/", async (req, res) => {
try {
const newDate = await databaseServices.renewSubscription(
req.body.restaurantId,
1
);
res.send(`Subskrypcja przedłużona do: ${newDate}`);
console.log("test");
} catch (error) {
services.handleError(error, res);
}