Image upload / Cookies
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
import express from "express";
|
||||
import * as validators from "../services/validation.js";
|
||||
import * as services from "../services/services.js";
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
import Dish from "../models/dish.js";
|
||||
import User from "../models/users.js";
|
||||
import sanitizer from "string-sanitizer";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
var router = express.Router();
|
||||
|
||||
// GET RESTAURANT BY ID
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
validators.validateRestaurant(req.body.restaurantId, (result) => {
|
||||
services.validateRestaurant(req.body.restaurantId, (result) => {
|
||||
if (!result) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
@@ -21,50 +23,53 @@ router.get("/", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// ADD NEW RESTAURANT
|
||||
|
||||
router.post("/", (req, res) => {
|
||||
//validate user
|
||||
validators.validateUser(req.body.userId, (result) => {
|
||||
if (!result) res.sendStatus(401);
|
||||
//create restaurant
|
||||
const restaurant = new Restaurant({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: sanitizer.sanitize.keepUnicode(req.body.name),
|
||||
city: sanitizer.sanitize.keepUnicode(req.body.city),
|
||||
imgUrl: req.body.imgUrl,
|
||||
workingHours: req.body.workingHours,
|
||||
hidden: req.body.hidden,
|
||||
});
|
||||
//add restaurant to DB
|
||||
restaurant.save((err) => {
|
||||
if (err) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
res.status(201);
|
||||
}
|
||||
});
|
||||
services.validateUser(req.body.userId, (result) => {
|
||||
if (!result) {
|
||||
res.sendStatus(401);
|
||||
} else {
|
||||
const restaurant = new Restaurant({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: sanitizer.sanitize.keepUnicode(req.body.name),
|
||||
city: sanitizer.sanitize.keepUnicode(req.body.city),
|
||||
imgUrl: services.saveImage(req.cookies["img"]),
|
||||
workingHours: req.body.workingHours,
|
||||
hidden: req.body.hidden,
|
||||
});
|
||||
restaurant.save((err) => {
|
||||
if (err) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
res.clearCookie("img").status(201).send();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// GET ALL DISHES FROM A RESTAURANT ID
|
||||
|
||||
router.get("/dishes", (req, res) => {
|
||||
//validate restaurant
|
||||
validators.validateRestaurant(req.body.restaurantId, (result) => {
|
||||
services.validateRestaurant(req.body.restaurantId, (result) => {
|
||||
if (!result) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
//get restaurant
|
||||
Restaurant.findById(req.body.restaurantId, (err, result) => {
|
||||
if (err) {
|
||||
res.sendStatus(404);
|
||||
} else {
|
||||
//prepare variables
|
||||
const dishesCount = result.dishes.length;
|
||||
let dishes = [];
|
||||
//fetch all dishes
|
||||
result.dishes.forEach((element) => {
|
||||
Dish.findById(element, (err, result) => {
|
||||
if (err) console.log("ERROR fetching dish");
|
||||
dishes.push(result);
|
||||
if (dishes.length == dishesCount) res.send(dishes);
|
||||
if (err) {
|
||||
console.log("ERROR fetching dish");
|
||||
} else {
|
||||
dishes.push(result);
|
||||
if (dishes.length == dishesCount) res.send(dishes);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user