From 7ad02ccfe26d5a99245a6babf441153990537856 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Mon, 18 Apr 2022 09:10:41 -0500 Subject: [PATCH] Impl new review logic --- backend/.env.default | 18 -------- backend/.envrc.default | 18 ++++++++ backend/package-lock.json | 5 -- backend/src/app/controllers/product/index.ts | 19 ++++++++ .../app/controllers/product/reviews/create.ts | 37 +++++++++++++++ .../app/controllers/product/reviews/delete.ts | 46 +++++++++++++++++++ .../app/controllers/product/reviews/update.ts | 46 +++++++++++++++++++ backend/src/app/controllers/s3/crud/post.ts | 3 -- backend/src/app/models/Product.ts | 38 ++++++++++++++- backend/src/app/models/Review.ts | 27 ----------- 10 files changed, 203 insertions(+), 54 deletions(-) delete mode 100644 backend/.env.default create mode 100644 backend/.envrc.default create mode 100644 backend/src/app/controllers/product/reviews/create.ts create mode 100644 backend/src/app/controllers/product/reviews/delete.ts create mode 100644 backend/src/app/controllers/product/reviews/update.ts delete mode 100644 backend/src/app/models/Review.ts diff --git a/backend/.env.default b/backend/.env.default deleted file mode 100644 index bfbfed7..0000000 --- a/backend/.env.default +++ /dev/null @@ -1,18 +0,0 @@ -APP_NAME= -LOGO= -URL= - -MONGO_URI= -MONGO_COLLECTION= - -JWT_SECRET= - -URL= - -NODEMAILER_ADDRESS= -NODEMAILER_PASSWORD= - -AWS_REGION= -AWS_BUCKET= -AWS_ACCESS_KEY_ID= -AWS_SECRET_ACCESS_KEY= \ No newline at end of file diff --git a/backend/.envrc.default b/backend/.envrc.default new file mode 100644 index 0000000..cb64d25 --- /dev/null +++ b/backend/.envrc.default @@ -0,0 +1,18 @@ +export APP_NAME= +export LOGO= +export URL= + +export MONGO_URI= +export MONGO_COLLECTION= + +export JWT_SECRET= + +export URL= + +export NODEMAILER_ADDRESS= +export NODEMAILER_PASSWORD= + +export AWS_REGION= +export AWS_BUCKET= +export AWS_ACCESS_KEY_ID= +export AWS_SECRET_ACCESS_KEY= diff --git a/backend/package-lock.json b/backend/package-lock.json index fa10899..8dd4f7a 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -507,11 +507,6 @@ "streamsearch": "0.1.2" } }, - "dotenv": { - "version": "16.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.0.tgz", - "integrity": "sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==" - }, "ecdsa-sig-formatter": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", diff --git a/backend/src/app/controllers/product/index.ts b/backend/src/app/controllers/product/index.ts index 8b59eb9..d7b351b 100644 --- a/backend/src/app/controllers/product/index.ts +++ b/backend/src/app/controllers/product/index.ts @@ -5,6 +5,10 @@ import get from "./crud/get"; import list from "./crud/list"; import update from "./crud/update"; +import rupdate from "./reviews/update"; +import rcreate from "./reviews/create"; +import rdel from "./reviews/delete"; + const routes: RouteCollection[] = [ { httpMethod: "GET", @@ -31,6 +35,21 @@ const routes: RouteCollection[] = [ path: "/api/v1/products/:id", handler: del, }, + { + httpMethod: "PUT", + path: "/api/v1/products/:id/reviews", + handler: rcreate, + }, + { + httpMethod: "PATCH", + path: "/api/v1/products/:id/reviews/:revId", + handler: rupdate, + }, + { + httpMethod: "DELETE", + path: "/api/v1/products/:id/reviews/:revId", + handler: rdel, + }, ]; export default routes; diff --git a/backend/src/app/controllers/product/reviews/create.ts b/backend/src/app/controllers/product/reviews/create.ts new file mode 100644 index 0000000..fd11f7f --- /dev/null +++ b/backend/src/app/controllers/product/reviews/create.ts @@ -0,0 +1,37 @@ +import { Request, Response } from "express"; +import Products, { Review } from "../../../models/Product"; + +export default async (req: Request, res: Response) => { + if (!req.user) { + return res + .status(403) + .json({ error: "You must be logged in to leave a review" }); + } + + if (!req.params.id) { + return res + .status(422) + .json({ error: "Missing parameter 'id' to specify product" }); + } + + const product = await Products.findById(req.params.id); + if (!product) { + return res + .status(404) + .json({ error: `Product with id ${req.params.id} could not be located` }); + } + + product.reviews = [ + ...product.reviews, + { + ...req.body, + user: { + first: req.user.first, + last: req.user.last, + _id: req.user._id, + }, + } as Review, + ]; + await product.save(); + return res.status(200).send(); +}; diff --git a/backend/src/app/controllers/product/reviews/delete.ts b/backend/src/app/controllers/product/reviews/delete.ts new file mode 100644 index 0000000..9ca6bf8 --- /dev/null +++ b/backend/src/app/controllers/product/reviews/delete.ts @@ -0,0 +1,46 @@ +import { Request, Response } from "express"; +import Products, { Review } from "../../../models/Product"; + +export default async (req: Request, res: Response) => { + if (!req.user) { + return res + .status(403) + .json({ error: "You must be logged in to leave a review" }); + } + + if (!req.params.id) { + return res + .status(422) + .json({ error: "Missing parameter 'id' to specify product" }); + } + + if (!req.params.revId) { + return res + .status(422) + .json({ error: "Missing parameter 'revId' to specify which review" }); + } + + const product = await Products.findById(req.params.id); + if (!product) { + return res.status(404).json({ error: "Product not found" }); + } + + const rev = product.reviews.find( + (x) => x._id.toString() === req.params.revId + ); + + if (!rev) { + return res.status(404).json({ error: "Review not found" }); + } + + if (!rev.user._id.equals(req.user._id)) { + return res.status(403).json({ error: "Review isn't yours" }); + } + + product.reviews = product.reviews.filter( + (x) => x._id.toString() !== req.params.revId + ); + + await product.save(); + return res.status(200).send(); +}; diff --git a/backend/src/app/controllers/product/reviews/update.ts b/backend/src/app/controllers/product/reviews/update.ts new file mode 100644 index 0000000..e19e407 --- /dev/null +++ b/backend/src/app/controllers/product/reviews/update.ts @@ -0,0 +1,46 @@ +import { Request, Response } from "express"; +import Products, { Review } from "../../../models/Product"; + +export default async (req: Request, res: Response) => { + if (!req.user) { + return res + .status(403) + .json({ error: "You must be logged in to leave a review" }); + } + + if (!req.params.id) { + return res + .status(422) + .json({ error: "Missing parameter 'id' to specify product" }); + } + + if (!req.params.revId) { + return res + .status(422) + .json({ error: "Missing parameter 'revId' to specify which review" }); + } + + const product = await Products.findById(req.params.id); + if (!product) { + return res.status(404).json({ error: "Product not found" }); + } + + const rev = product.reviews.find( + (x) => x._id.toString() === req.params.revId + ); + + if (!rev) { + return res.status(404).json({ error: "Review not found" }); + } + + if (!rev.user._id.equals(req.user._id)) { + return res.status(403).json({ error: "Review isn't yours" }); + } + + rev.rating = req.body.rating || rev.rating; + rev.review.title = req.body.review?.title || rev.review.title; + rev.review.body = req.body.review?.body || rev.review.body; + + await product.save(); + return res.status(200).send(); +}; diff --git a/backend/src/app/controllers/s3/crud/post.ts b/backend/src/app/controllers/s3/crud/post.ts index 5e864b2..a626fdf 100644 --- a/backend/src/app/controllers/s3/crud/post.ts +++ b/backend/src/app/controllers/s3/crud/post.ts @@ -1,8 +1,5 @@ import { Request, Response } from "express"; -import S3FileLink from "../../../models/S3FileLink"; export default async (req: Request, res: Response) => { - // const f = new S3FileLink({ key: (req.file as any).key }); - // await f.save(); return res.status(200).json({ output: (req.file as any).key }); }; diff --git a/backend/src/app/models/Product.ts b/backend/src/app/models/Product.ts index 679d253..fc6b0e9 100644 --- a/backend/src/app/models/Product.ts +++ b/backend/src/app/models/Product.ts @@ -1,4 +1,17 @@ -import { Document, model, Schema } from "mongoose"; +import { Document, model, Schema, Types } from "mongoose"; + +export type Review = Document & { + rating: number; + user: { + first: string; + last: string; + _id: Types.ObjectId; + }; + review: { + title: string; + body: string; + }; +}; export type ProductModel = Document & { name: string; @@ -6,8 +19,25 @@ export type ProductModel = Document & { price: number; images: string[]; hidden: boolean; + reviews: Review[]; }; +const ReviewSchema = new Schema({ + rating: { + type: Number, + required: true, + }, + user: { + _id: { type: Schema.Types.ObjectId, required: true }, + first: { type: String, required: true }, + last: { type: String, required: true }, + }, + review: { + title: { type: String, required: true }, + body: { type: String, required: true }, + }, +}); + const ProductSchema = new Schema({ name: { type: String, @@ -33,6 +63,12 @@ const ProductSchema = new Schema({ required: true, default: false, }, + reviews: [ + { + type: ReviewSchema, + default: [], + }, + ], }); export default model("products", ProductSchema, "products"); diff --git a/backend/src/app/models/Review.ts b/backend/src/app/models/Review.ts deleted file mode 100644 index a1938aa..0000000 --- a/backend/src/app/models/Review.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Document, model, Schema, ObjectId, Types } from "mongoose"; - -export type ReviewModel = Document & { - productId: ObjectId; - userId: ObjectId; - rating: number; // Out of Decimal from 0.0 to 5.0 (stars) - reviewTitle: string; // General title of the review left - reviewBody: string; // The actual review left behind. -}; - -const PRSchema = new Schema({ - productId: { - type: Types.ObjectId, - ref: "products", - required: true, - }, - userId: { - type: Types.ObjectId, - ref: "users", - required: true, - }, - rating: Number, - reviewTitle: String, - reviewBody: String, -}); - -export default model("productReviews", PRSchema, "productReviews");