Mongoose Validation
This commit is contained in:
221
app.js
221
app.js
@@ -35,20 +35,19 @@ mongoose.connect(
|
||||
"@menui-database.9quwf.mongodb.net/<dbname>?retryWrites=true&w=majority",
|
||||
{ useNewUrlParser: true, useUnifiedTopology: true },
|
||||
(err) => {
|
||||
if (err) console.log(err);
|
||||
if (err) console.log("Unable to connect :(");
|
||||
else console.log("Connected To Database");
|
||||
}
|
||||
);
|
||||
|
||||
// GET RESTAURANT BY ID
|
||||
|
||||
app.get("/restaurant/:restaurantId", function (req, res) {
|
||||
app.get("/restaurant", function (req, res) {
|
||||
//validate restaurant
|
||||
validators.validateRestaurant(req.body.restaurantId, (result) => {
|
||||
console.log("Restaurant validation: " + result);
|
||||
if (!result) res.sendStatus(404);
|
||||
Restaurant.findById(req.params.restaurantId, (err, data) => {
|
||||
if (err) res.send(err);
|
||||
if (!result) res.sendStatus(400);
|
||||
Restaurant.findById(req.body.restaurantId, (err, data) => {
|
||||
if (err) res.sendStatus(404);
|
||||
else res.send(data);
|
||||
});
|
||||
});
|
||||
@@ -56,17 +55,17 @@ app.get("/restaurant/:restaurantId", function (req, res) {
|
||||
|
||||
// GET RESTAURANTS IN A SPECIFIED CITY
|
||||
|
||||
app.get("/city/:cityName", function (req, res) {
|
||||
Restaurant.find({ city: decodeURI(req.params.cityName) }, (err, data) => {
|
||||
if (err) res.send(err);
|
||||
app.get("/city", function (req, res) {
|
||||
Restaurant.find({ city: req.body.city }, (err, data) => {
|
||||
if (err) res.sendStatus(404);
|
||||
else res.send(data);
|
||||
});
|
||||
});
|
||||
|
||||
// GET DISH BY ID
|
||||
|
||||
app.get("/dish/:dishId", (req, res) => {
|
||||
Dish.findById(req.params.dishId, (err, data) => {
|
||||
app.get("/dish", (req, res) => {
|
||||
Dish.findById(req.body.dishId, (err, data) => {
|
||||
if (err) res.sendStatus(404);
|
||||
res.send(data);
|
||||
});
|
||||
@@ -77,7 +76,6 @@ app.get("/dish/:dishId", (req, res) => {
|
||||
app.post("/restaurant", (req, res) => {
|
||||
//validate user
|
||||
validators.validateUser(req.body.userId, (result) => {
|
||||
console.log("User validation: " + result);
|
||||
if (!result) res.sendStatus(401);
|
||||
//create restaurant
|
||||
const restaurant = new Restaurant({
|
||||
@@ -89,10 +87,12 @@ app.post("/restaurant", (req, res) => {
|
||||
hidden: req.body.hidden,
|
||||
});
|
||||
//add restaurant to DB
|
||||
restaurant.save().catch((err) => console.log(err));
|
||||
res.status(201).json({
|
||||
message: "Restaurant Created",
|
||||
addedRestaurant: restaurant,
|
||||
restaurant.save((err) => {
|
||||
if (err) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
res.status(201);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -102,112 +102,47 @@ app.post("/restaurant", (req, res) => {
|
||||
app.post("/dish", (req, res) => {
|
||||
//validate restaurant
|
||||
validators.validateRestaurant(req.body.restaurantId, (result) => {
|
||||
console.log("Restaurant validation: " + result);
|
||||
if (!result) res.sendStatus(404);
|
||||
if (!result) res.sendStatus(400);
|
||||
else {
|
||||
//validate user
|
||||
validators.validateUser(req.body.userId, (result) => {
|
||||
console.log("User validation: " + result);
|
||||
if (!result) res.sendStatus(401);
|
||||
else {
|
||||
//validate dish
|
||||
validators.validateDish(req.body.dish, (result) => {
|
||||
console.log("Dish validation: " + result);
|
||||
if (!result) res.sendStatus(400);
|
||||
else {
|
||||
//construct dish
|
||||
const dish = new Dish({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: req.body.dish.name,
|
||||
category: req.body.dish.category,
|
||||
price: req.body.dish.price,
|
||||
notes: req.body.dish.notes,
|
||||
imgUrl: req.body.dish.imgUrl,
|
||||
weight: req.body.dish.weight,
|
||||
allergens: {
|
||||
gluten: req.body.dish.allergens.gluten,
|
||||
lactose: req.body.dish.allergens.lactose,
|
||||
soy: req.body.dish.allergens.soy,
|
||||
eggs: req.body.dish.allergens.eggs,
|
||||
seaFood: req.body.dish.allergens.seaFood,
|
||||
peanuts: req.body.dish.allergens.peanuts,
|
||||
sesame: req.body.dish.allergens.sesame,
|
||||
},
|
||||
vegan: req.body.dish.vegan,
|
||||
vegetarian: req.body.dish.vegetarian,
|
||||
});
|
||||
//add dish to DB
|
||||
dish.save().catch((err) => console.log(err));
|
||||
if (!result) {
|
||||
res.sendStatus(401);
|
||||
} else {
|
||||
//construct dish
|
||||
const dish = new Dish({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: req.body.dish.name,
|
||||
category: req.body.dish.category,
|
||||
price: req.body.dish.price,
|
||||
notes: req.body.dish.notes,
|
||||
imgUrl: req.body.dish.imgUrl,
|
||||
weight: req.body.dish.weight,
|
||||
allergens: {
|
||||
gluten: req.body.dish.allergens.gluten,
|
||||
lactose: req.body.dish.allergens.lactose,
|
||||
soy: req.body.dish.allergens.soy,
|
||||
eggs: req.body.dish.allergens.eggs,
|
||||
seaFood: req.body.dish.allergens.seaFood,
|
||||
peanuts: req.body.dish.allergens.peanuts,
|
||||
sesame: req.body.dish.allergens.sesame,
|
||||
},
|
||||
vegan: req.body.dish.vegan,
|
||||
vegetarian: req.body.dish.vegetarian,
|
||||
});
|
||||
//add dish to DB
|
||||
dish.save((err) => {
|
||||
if (err) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
//add dish ID to restaurant
|
||||
Restaurant.updateOne(
|
||||
{ _id: req.body.restaurantId },
|
||||
{ $push: { dishes: dish._id } },
|
||||
(err) => {
|
||||
if (err) console.log(err);
|
||||
}
|
||||
);
|
||||
res.sendStatus(201);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// GET ALL DISHES FROM A RESTAURANT ID (All at once)
|
||||
|
||||
app.get("/dishes/:restaurantId", (req, res) => {
|
||||
//validate restaurant
|
||||
validators.validateRestaurant(req.params.restaurantId, (result) => {
|
||||
if (!result) res.sendStatus(404);
|
||||
//get restaurant
|
||||
Restaurant.findById(req.params.restaurantId, (err, result) => {
|
||||
if (err) res.sendStatus(401);
|
||||
//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(err);
|
||||
dishes.push(result);
|
||||
if (dishes.length == dishesCount) res.send(dishes);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// UPDATE DISH
|
||||
|
||||
app.put("/dish", (req, res) => {
|
||||
//validate dish ID
|
||||
validators.validateDishId(req.body.dishId, (result) => {
|
||||
console.log("DishID valid: " + result);
|
||||
if (!result) {
|
||||
res.sendStatus(204);
|
||||
} else {
|
||||
//validate user
|
||||
validators.validateUser(req.body.userId, (result) => {
|
||||
console.log("User validation: " + result);
|
||||
if (!result) {
|
||||
res.sendStatus(403);
|
||||
} else {
|
||||
//validate dish
|
||||
validators.validateDish(req.body.dish, (result) => {
|
||||
if (!result) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
//replace dish in DB
|
||||
Dish.replaceOne(
|
||||
{ _id: req.body.dishId },
|
||||
req.body.dish,
|
||||
(err) => {
|
||||
if (err) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
console.log("Dish Replaced with: " + req.body.dish);
|
||||
res.sendStatus(201);
|
||||
}
|
||||
}
|
||||
@@ -220,8 +155,66 @@ app.put("/dish", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/img", upload.single("menuiImage"), (req, res) => {
|
||||
res.sendStatus(200);
|
||||
// GET ALL DISHES FROM A RESTAURANT ID (All at once)
|
||||
|
||||
app.get("/dishes", (req, res) => {
|
||||
//validate restaurant
|
||||
validators.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);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(port, () => console.log("Menui back-end is listening at: " + port));
|
||||
// UPDATE DISH
|
||||
|
||||
app.put("/dish", (req, res) => {
|
||||
//validate dish ID
|
||||
validators.validateDishId(req.body.dishId, (result) => {
|
||||
if (!result) {
|
||||
res.sendStatus(204);
|
||||
} else {
|
||||
//validate user
|
||||
validators.validateUser(req.body.userId, (result) => {
|
||||
if (!result) {
|
||||
res.sendStatus(401);
|
||||
} else {
|
||||
//replace dish in DB
|
||||
Dish.replaceOne({ _id: req.body.dishId }, req.body.dish, (err) => {
|
||||
if (err) {
|
||||
res.sendStatus(304);
|
||||
} else {
|
||||
console.log("Dish Replaced with: " + req.body.dish);
|
||||
res.sendStatus(200);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/img", upload.single("menuiImage"), (req, res) => {
|
||||
res.sendStatus(201);
|
||||
});
|
||||
|
||||
app.listen(port, () => console.log("Menui listening at: " + port));
|
||||
|
||||
@@ -2,11 +2,25 @@ const mongoose = require("mongoose");
|
||||
|
||||
const dishSchema = mongoose.Schema({
|
||||
_id: mongoose.Types.ObjectId,
|
||||
name: String,
|
||||
category: String,
|
||||
price: Number,
|
||||
name: {
|
||||
type: String,
|
||||
maxlength: 128,
|
||||
required: true,
|
||||
},
|
||||
category: {
|
||||
type: String,
|
||||
maxlength: 64,
|
||||
required: true,
|
||||
},
|
||||
price: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
notes: String,
|
||||
imgUrl: String,
|
||||
imgUrl: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
hidden: Boolean,
|
||||
weight: Number,
|
||||
allergens: {
|
||||
|
||||
@@ -2,10 +2,22 @@ const mongoose = require("mongoose");
|
||||
|
||||
const restaurantSchema = mongoose.Schema({
|
||||
_id: mongoose.Types.ObjectId,
|
||||
name: String,
|
||||
city: String,
|
||||
imgUrl: String,
|
||||
workingHours: String,
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
city: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
imgUrl: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
workingHours: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
hidden: Boolean,
|
||||
dishes: [mongoose.Types.ObjectId],
|
||||
});
|
||||
|
||||
@@ -2,10 +2,19 @@ const mongoose = require("mongoose");
|
||||
|
||||
const userSchema = mongoose.Schema({
|
||||
_id: mongoose.Types.ObjectId,
|
||||
email: String,
|
||||
password: String,
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
password: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
restaurantId: mongoose.Types.ObjectId,
|
||||
subscriptionActive: Boolean,
|
||||
subscriptionActive: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
},
|
||||
subscriptionDue: Date,
|
||||
});
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ function validateRestaurant(id, callback) {
|
||||
if (mongoose.Types.ObjectId.isValid(id)) {
|
||||
Restaurant.exists({ _id: id }, (err, res) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(false);
|
||||
} else {
|
||||
callback(res);
|
||||
@@ -20,15 +19,10 @@ function validateUser(id, callback) {
|
||||
callback(true);
|
||||
}
|
||||
|
||||
function validateDish(dish, callback) {
|
||||
callback(true);
|
||||
}
|
||||
|
||||
function validateDishId(id, callback) {
|
||||
if (mongoose.Types.ObjectId.isValid(id)) {
|
||||
Dish.exists({ _id: id }, (err, res) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(false);
|
||||
} else {
|
||||
callback(res);
|
||||
@@ -39,5 +33,4 @@ function validateDishId(id, callback) {
|
||||
|
||||
exports.validateRestaurant = validateRestaurant;
|
||||
exports.validateUser = validateUser;
|
||||
exports.validateDish = validateDish;
|
||||
exports.validateDishId = validateDishId;
|
||||
|
||||
Reference in New Issue
Block a user