diff --git a/backend/src/app/controllers/review/crud/create.ts b/backend/src/app/controllers/review/crud/create.ts new file mode 100644 index 0000000..56d8f5a --- /dev/null +++ b/backend/src/app/controllers/review/crud/create.ts @@ -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 }); + } +}; diff --git a/backend/src/app/controllers/review/crud/delete.ts b/backend/src/app/controllers/review/crud/delete.ts new file mode 100644 index 0000000..602179c --- /dev/null +++ b/backend/src/app/controllers/review/crud/delete.ts @@ -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 }); + } +}; diff --git a/backend/src/app/controllers/review/crud/getByProduct.ts b/backend/src/app/controllers/review/crud/getByProduct.ts new file mode 100644 index 0000000..e67603d --- /dev/null +++ b/backend/src/app/controllers/review/crud/getByProduct.ts @@ -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 }); + } +}; diff --git a/backend/src/app/controllers/review/crud/getByUser.ts b/backend/src/app/controllers/review/crud/getByUser.ts new file mode 100644 index 0000000..4bd5c11 --- /dev/null +++ b/backend/src/app/controllers/review/crud/getByUser.ts @@ -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 }); + } +}; diff --git a/backend/src/app/controllers/review/crud/update.ts b/backend/src/app/controllers/review/crud/update.ts new file mode 100644 index 0000000..9107253 --- /dev/null +++ b/backend/src/app/controllers/review/crud/update.ts @@ -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 }); + } +}; diff --git a/backend/src/app/controllers/review/routes.ts b/backend/src/app/controllers/review/routes.ts index d6d1738..e9134a7 100644 --- a/backend/src/app/controllers/review/routes.ts +++ b/backend/src/app/controllers/review/routes.ts @@ -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;