Impl new review logic
This commit is contained in:
parent
88105549c9
commit
7ad02ccfe2
10 changed files with 203 additions and 54 deletions
|
|
@ -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=
|
||||
18
backend/.envrc.default
Normal file
18
backend/.envrc.default
Normal file
|
|
@ -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=
|
||||
5
backend/package-lock.json
generated
5
backend/package-lock.json
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
37
backend/src/app/controllers/product/reviews/create.ts
Normal file
37
backend/src/app/controllers/product/reviews/create.ts
Normal file
|
|
@ -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();
|
||||
};
|
||||
46
backend/src/app/controllers/product/reviews/delete.ts
Normal file
46
backend/src/app/controllers/product/reviews/delete.ts
Normal file
|
|
@ -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();
|
||||
};
|
||||
46
backend/src/app/controllers/product/reviews/update.ts
Normal file
46
backend/src/app/controllers/product/reviews/update.ts
Normal file
|
|
@ -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();
|
||||
};
|
||||
|
|
@ -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 });
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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<Review>({
|
||||
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<ProductModel>({
|
||||
name: {
|
||||
type: String,
|
||||
|
|
@ -33,6 +63,12 @@ const ProductSchema = new Schema<ProductModel>({
|
|||
required: true,
|
||||
default: false,
|
||||
},
|
||||
reviews: [
|
||||
{
|
||||
type: ReviewSchema,
|
||||
default: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export default model<ProductModel>("products", ProductSchema, "products");
|
||||
|
|
|
|||
|
|
@ -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<ReviewModel>({
|
||||
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<ReviewModel>("productReviews", PRSchema, "productReviews");
|
||||
Loading…
Reference in a new issue