Remove old Review logic
This commit is contained in:
parent
882885f65b
commit
88105549c9
7 changed files with 0 additions and 187 deletions
|
|
@ -1,6 +1,5 @@
|
|||
import { Request, Response } from "express";
|
||||
import ProductRoutes from "./product";
|
||||
import ReviewRoutes from "./review";
|
||||
import UserRoutes from "./user";
|
||||
import S3Routes from "./s3";
|
||||
|
||||
|
|
@ -14,7 +13,6 @@ export type RouteCollection = {
|
|||
const routes: RouteCollection[] = [
|
||||
...ProductRoutes,
|
||||
...UserRoutes,
|
||||
...ReviewRoutes,
|
||||
...S3Routes,
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
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 });
|
||||
}
|
||||
};
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
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 });
|
||||
}
|
||||
};
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
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 });
|
||||
}
|
||||
};
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
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 });
|
||||
}
|
||||
};
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
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,36 +0,0 @@
|
|||
import { RouteCollection } from "..";
|
||||
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