Impl new review logic

This commit is contained in:
oitsjustjose 2022-04-18 09:10:41 -05:00
commit 7ad02ccfe2
10 changed files with 203 additions and 54 deletions

View file

@ -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");

View file

@ -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");