get routes changed to query string

This commit is contained in:
2020-07-24 20:27:41 +02:00
parent d2a29416fe
commit d2ab242fed
4 changed files with 64 additions and 35 deletions

View File

@@ -20,6 +20,19 @@ const restaurantSchema = mongoose.Schema({
type: String,
required: true,
},
description: {
type: String,
},
tags: {
cardPayments: Boolean,
petFriendly: Boolean,
glutenFree: Boolean,
vegan: Boolean,
vegetarian: Boolean,
alcohol: Boolean,
delivery: Boolean,
},
phone: Number,
hidden: Boolean,
dishes: [mongoose.Types.ObjectId],
});

View File

@@ -1,6 +1,7 @@
import express from "express";
import Restaurant from "../models/restaurant.js";
import * as services from "../services/services.js";
import sanitizer from "string-sanitizer";
import Dish from "../models/dish.js";
var router = express.Router();

View File

@@ -10,17 +10,24 @@ var router = express.Router();
// GET RESTAURANT BY ID
router.get("/", (req, res) => {
services.validateRestaurant(req.body.restaurantId, (result) => {
if (!result) {
res.sendStatus(400);
} else {
Restaurant.findById(req.body.restaurantId, (err, data) => {
if (err) {
res.sendStatus(404);
} else res.send(data);
});
}
});
if (req.query.restaurantId.length > 0) {
const query = sanitizer.sanitize.keepUnicode(
decodeURI(req.query.restaurantId)
);
services.validateRestaurant(query, (result) => {
if (!result) {
res.sendStatus(400);
} else {
Restaurant.findById(query, (err, data) => {
if (err) {
res.sendStatus(404);
} else res.send(data);
});
}
});
} else {
res.sendStatus(404);
}
});
// ADD NEW RESTAURANT
@@ -57,30 +64,38 @@ router.post("/", (req, res) => {
// GET ALL DISHES FROM A RESTAURANT ID
router.get("/dishes", (req, res) => {
services.validateRestaurant(req.body.restaurantId, (result) => {
if (!result) {
res.sendStatus(400);
} else {
Restaurant.findById(req.body.restaurantId, (err, result) => {
if (err) {
res.sendStatus(404);
} else {
const dishesCount = result.dishes.length;
let dishes = [];
result.dishes.forEach((element) => {
Dish.findById(element, (err, result) => {
if (err) {
console.log("ERROR fetching dish");
} else {
dishes.push(result);
if (dishes.length == dishesCount) res.send(dishes);
}
if (req.query.restaurantId.length > 0) {
const query = sanitizer.sanitize.keepUnicode(
decodeURI(req.query.restaurantId)
);
services.validateRestaurant(query, (result) => {
if (!result) {
res.sendStatus(400);
} else {
Restaurant.findById(query, (err, result) => {
if (err) {
res.sendStatus(404);
} else {
const dishesCount = result.dishes.length;
let dishes = [];
result.dishes.forEach((element) => {
Dish.findById(element, (err, result) => {
if (err) {
console.log("ERROR fetching dish");
} else {
dishes.push(result);
if (dishes.length == dishesCount) res.send(dishes);
}
});
});
});
}
});
}
});
}
});
}
});
} else {
res.sendStatus(404);
}
});
export default router;

View File

@@ -20,7 +20,7 @@ router.get("/", (req, res) => {
{ hidden: false },
],
},
"_id name city imgUrl workingHours",
"_id name city imgUrl workingHours description tags phone hidden",
(err, results) => {
if (err) {
res.sendStatus(500);