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