Refactoring day2 (error handling)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import express from "express";
|
||||
import * as services from "../services/services.js";
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
import Dish from "../models/dish.js";
|
||||
import sanitizer from "string-sanitizer";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
@@ -65,25 +64,8 @@ router.get("/dishes", async (req, res) => {
|
||||
const query = services.decodeAndSanitize(req.query.restaurantId);
|
||||
await services.validateRestaurant(query);
|
||||
let restaurant = await services.fetchRestaurant(query);
|
||||
let dishesCount = restaurant.dishes.length;
|
||||
let dishes = [];
|
||||
let dishes2 = await services.fetchAllDishesForRestaurant(restaurant);
|
||||
console.log(dishes2);
|
||||
restaurant.dishes.forEach((element) => {
|
||||
Dish.findById(element._id, (err, result) => {
|
||||
if (err) {
|
||||
res.sendStatus(500);
|
||||
} else {
|
||||
if (result === null) {
|
||||
dishesCount--;
|
||||
if (dishes.length == dishesCount) res.send(dishes);
|
||||
} else {
|
||||
dishes.push(result);
|
||||
if (dishes.length == dishesCount) res.send(dishes);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
let dishes = await services.fetchAllDishesForRestaurant(restaurant);
|
||||
res.send(dishes);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.sendStatus(400);
|
||||
|
||||
17
routes/routeTest.js
Normal file
17
routes/routeTest.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import express from "express";
|
||||
import * as services from "../services/services.js";
|
||||
|
||||
var router = express.Router();
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
await services
|
||||
.fetchUser()
|
||||
.then((response) => {
|
||||
res.send(response);
|
||||
})
|
||||
.catch((e) => {
|
||||
services.handleError(e, res);
|
||||
});
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -16,66 +16,47 @@ var error = function (err) {
|
||||
console.log("Task failed successfully");
|
||||
};
|
||||
|
||||
router.post("/login", (req, res) => {
|
||||
if (req.body.password && req.body.email) {
|
||||
services.fetchUser(req.body.email, (result) => {
|
||||
if (!result) {
|
||||
res.sendStatus(404);
|
||||
} else {
|
||||
var user = result;
|
||||
bcrypt.compare(req.body.password, user.password, function (
|
||||
err,
|
||||
result
|
||||
) {
|
||||
if (err) {
|
||||
res.sendStatus(500);
|
||||
} else {
|
||||
if (result) {
|
||||
const userNoPass = {
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
email: user.email,
|
||||
id: user._id,
|
||||
};
|
||||
var token = services.generateAuthToken(userNoPass);
|
||||
res.header("x-auth-token", token).status(202).send(userNoPass);
|
||||
} else {
|
||||
res.sendStatus(401);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
// LOGIN
|
||||
router.post("/login", async (req, res) => {
|
||||
try {
|
||||
if (!req.body.password || !req.body.email) {
|
||||
throw services.newError("No input data", 204);
|
||||
}
|
||||
const user = await services.fetchUser(req.body.email);
|
||||
/* await services.checkPassword(req.body.password, user.password);
|
||||
const userNoPass = {
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
email: user.email,
|
||||
id: user._id,
|
||||
};
|
||||
var token = services.generateAuthToken(userNoPass);
|
||||
res.header("x-auth-token", token).status(202).send(userNoPass); */
|
||||
res.send(user);
|
||||
} catch (error) {
|
||||
services.handleError(error, res);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/register", (req, res) => {
|
||||
services.checkEmailTaken(req.body.email, (result) => {
|
||||
if (result) {
|
||||
res.sendStatus(409);
|
||||
} else {
|
||||
services.hashPass(req.body.password, (hashedPass) => {
|
||||
const user = new User({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
email: req.body.email,
|
||||
password: hashedPass,
|
||||
firstname: req.body.firstname,
|
||||
lastname: req.body.lastname,
|
||||
});
|
||||
user.save((err) => {
|
||||
if (err) {
|
||||
res.sendStatus(500);
|
||||
} else {
|
||||
const contact = services.composeNewContact(user);
|
||||
agileAPI.contactAPI.add(contact, success, error);
|
||||
res.sendStatus(201);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
// REGISTER
|
||||
router.post("/register", async (req, res) => {
|
||||
try {
|
||||
await services.checkEmailTaken(req.body.email);
|
||||
const password = await services.hashPass(req.body.password);
|
||||
const user = new User({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
email: req.body.email,
|
||||
password: password,
|
||||
firstname: req.body.firstname,
|
||||
lastname: req.body.lastname,
|
||||
});
|
||||
await user.save();
|
||||
const contact = services.composeNewContact(user);
|
||||
agileAPI.contactAPI.add(contact, success, error);
|
||||
res.sendStatus(201);
|
||||
} catch (e) {
|
||||
services.handleError(e, res);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user