Init review logic
This commit is contained in:
parent
0d5cd1f4a5
commit
5aad81f09e
6 changed files with 185 additions and 1 deletions
21
backend/src/app/controllers/review/crud/create.ts
Normal file
21
backend/src/app/controllers/review/crud/create.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
import Review from "../../../models/Review";
|
||||||
|
import { LOGGER } from "../../../util/Logger";
|
||||||
|
|
||||||
|
export default async (req: Request, res: Response) => {
|
||||||
|
// TODO: Validation logic to verify that the user has ever purchased one
|
||||||
|
try {
|
||||||
|
if (!req.user) {
|
||||||
|
return res
|
||||||
|
.status(403)
|
||||||
|
.json({ error: "You must be logged in to leave a review" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const tmp = new Review(req.body);
|
||||||
|
await tmp.save();
|
||||||
|
return res.status(200).json(tmp);
|
||||||
|
} catch (ex) {
|
||||||
|
LOGGER.error(ex);
|
||||||
|
return res.status(500).json({ error: ex });
|
||||||
|
}
|
||||||
|
};
|
||||||
42
backend/src/app/controllers/review/crud/delete.ts
Normal file
42
backend/src/app/controllers/review/crud/delete.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
import Reviews from "../../../models/Review";
|
||||||
|
import { LOGGER } from "../../../util/Logger";
|
||||||
|
|
||||||
|
export default async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
if (!req.params.id) {
|
||||||
|
return res
|
||||||
|
.status(422)
|
||||||
|
.json({ error: "Request is missing Parameter 'id'" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!req.user) {
|
||||||
|
return res
|
||||||
|
.status(403)
|
||||||
|
.json({ error: "You must be logged in to delete a review" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const review = await Reviews.findById(req.params.id);
|
||||||
|
if (!review) {
|
||||||
|
return res
|
||||||
|
.status(404)
|
||||||
|
.json({ error: `No such review with ID ${req.params.id} found` });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the user is an Admin, or the user owns the review
|
||||||
|
if (
|
||||||
|
!req.user.admin ||
|
||||||
|
review.userId.toString() !== req.user._id.toString()
|
||||||
|
) {
|
||||||
|
return res.status(403).json({
|
||||||
|
error: "You do not own this review to be capable of deleting it",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await review.delete();
|
||||||
|
return res.status(200).json({ "Successfully Deleted": review });
|
||||||
|
} catch (ex) {
|
||||||
|
LOGGER.error(ex);
|
||||||
|
return res.status(500).json({ error: ex });
|
||||||
|
}
|
||||||
|
};
|
||||||
19
backend/src/app/controllers/review/crud/getByProduct.ts
Normal file
19
backend/src/app/controllers/review/crud/getByProduct.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
import Reviews from "../../../models/Review";
|
||||||
|
import { LOGGER } from "../../../util/Logger";
|
||||||
|
|
||||||
|
export default async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
if (!req.params.productId) {
|
||||||
|
return res
|
||||||
|
.status(422)
|
||||||
|
.json({ error: "Request is missing Parameter 'productId'" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const reviews = await Reviews.find({ productId: req.params.productId });
|
||||||
|
return res.status(200).json(reviews);
|
||||||
|
} catch (ex) {
|
||||||
|
LOGGER.error(ex);
|
||||||
|
return res.status(500).json({ error: ex });
|
||||||
|
}
|
||||||
|
};
|
||||||
21
backend/src/app/controllers/review/crud/getByUser.ts
Normal file
21
backend/src/app/controllers/review/crud/getByUser.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
import Reviews from "../../../models/Review";
|
||||||
|
import Users from "../../../models/User";
|
||||||
|
import { LOGGER } from "../../../util/Logger";
|
||||||
|
|
||||||
|
export default async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
if (!req.params.userEmail) {
|
||||||
|
return res
|
||||||
|
.status(422)
|
||||||
|
.json({ error: "Request is missing Parameter 'userEmail'" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await Users.findOne({ email: req.params.userEmail });
|
||||||
|
const reviews = await Reviews.find({ userId: user._id });
|
||||||
|
return res.status(200).json(reviews);
|
||||||
|
} catch (ex) {
|
||||||
|
LOGGER.error(ex);
|
||||||
|
return res.status(500).json({ error: ex });
|
||||||
|
}
|
||||||
|
};
|
||||||
46
backend/src/app/controllers/review/crud/update.ts
Normal file
46
backend/src/app/controllers/review/crud/update.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
import Reviews from "../../../models/Review";
|
||||||
|
import { LOGGER } from "../../../util/Logger";
|
||||||
|
|
||||||
|
export default async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
if (!req.params.id) {
|
||||||
|
return res
|
||||||
|
.status(422)
|
||||||
|
.json({ error: "Request is missing Parameter 'id'" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!req.user) {
|
||||||
|
return res
|
||||||
|
.status(403)
|
||||||
|
.json({ error: "You must be logged in to modify a review" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const review = await Reviews.findById(req.params.id);
|
||||||
|
if (!review) {
|
||||||
|
return res
|
||||||
|
.status(404)
|
||||||
|
.json({ error: `Review with id ${req.params.id} could not be found` });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the user is an Admin, or the user owns the review
|
||||||
|
if (
|
||||||
|
!req.user.admin ||
|
||||||
|
review.userId.toString() !== req.user._id.toString()
|
||||||
|
) {
|
||||||
|
return res.status(403).json({
|
||||||
|
error: "You do not own this review to be capable of managing it",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
review.rating = req.body.rating || review.rating;
|
||||||
|
review.reviewTitle = req.body.reviewTitle || review.reviewTitle;
|
||||||
|
review.reviewBody = req.body.reviewBody || review.reviewBody;
|
||||||
|
|
||||||
|
await review.save();
|
||||||
|
return res.status(200).json(review);
|
||||||
|
} catch (ex) {
|
||||||
|
LOGGER.error(ex);
|
||||||
|
return res.status(500).json({ error: ex });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -1 +1,36 @@
|
||||||
export default [];
|
import { RouteCollection } from "../routes";
|
||||||
|
import create from "./crud/create";
|
||||||
|
import update from "./crud/update";
|
||||||
|
import del from "./crud/delete";
|
||||||
|
import getByUser from "./crud/getByUser";
|
||||||
|
import getByProduct from "./crud/getByProduct";
|
||||||
|
|
||||||
|
const routes: RouteCollection[] = [
|
||||||
|
{
|
||||||
|
httpMethod: "GET",
|
||||||
|
path: "/api/v1/products/reviews/:userEmail",
|
||||||
|
handler: getByUser,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
httpMethod: "GET",
|
||||||
|
path: "/api/v1/products/reviews/:productId",
|
||||||
|
handler: getByProduct,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
httpMethod: "PUT",
|
||||||
|
path: "/api/v1/products/reviews/",
|
||||||
|
handler: create,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
httpMethod: "PATCH",
|
||||||
|
path: "/api/v1/products/reviews/:id",
|
||||||
|
handler: update,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
httpMethod: "DELETE",
|
||||||
|
path: "/api/v1/products/reviews/:id",
|
||||||
|
handler: del,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export default routes;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue