From 146c096abead2c0407c81f5bbd4911a7996327d0 Mon Sep 17 00:00:00 2001 From: kianz20 Date: Thu, 5 Sep 2024 22:56:45 +1200 Subject: [PATCH] Added more details to product schema backend Added image path, stock count, category, franchise, rating to product Created new document schema for product reviews #14 --- server/src/models/Product.ts | 16 ++++++++++++--- server/src/models/Review.ts | 18 +++++++++++++++++ server/src/routes/product.ts | 39 +++++++++++++++++++++++++++++------- 3 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 server/src/models/Review.ts diff --git a/server/src/models/Product.ts b/server/src/models/Product.ts index a01680c..b036cf2 100644 --- a/server/src/models/Product.ts +++ b/server/src/models/Product.ts @@ -1,18 +1,28 @@ -import mongoose, { Schema, Document, ObjectId } from "mongoose"; +import mongoose, { Document, ObjectId, Schema } from "mongoose"; // TypeScript interface for the User model interface Product extends Document { name: string; - price: string; + price: number; description: string; _id?: ObjectId; + imgPath?: string; + stockCount?: number; + category: string; + franchise: string; + rating: string; } // Mongoose schema for the Product model const ProductSchema: Schema = new Schema({ name: { type: String, required: true }, - price: { type: String, required: true }, + price: { type: Number, required: true }, description: { type: String, required: true }, + imgPath: { type: String }, + stockCount: { type: Number, default: 0 }, + category: { type: String, required: true }, + franchise: { type: String, required: true }, + rating: { type: String }, }); export default mongoose.model("Product", ProductSchema); diff --git a/server/src/models/Review.ts b/server/src/models/Review.ts new file mode 100644 index 0000000..b6865dd --- /dev/null +++ b/server/src/models/Review.ts @@ -0,0 +1,18 @@ +import mongoose, { ObjectId, Schema } from "mongoose"; + +interface Review { + productId: ObjectId; + review: string; + reviewer: string; + stars: number; + _id?: ObjectId; +} + +// Mongoose schema for the Product model +const ReviewSchema: Schema = new Schema({ + review: { type: String, required: true }, + reviewer: { type: String, required: true }, + stars: { type: Number, required: true }, +}); + +export default mongoose.model("Product", ReviewSchema); diff --git a/server/src/routes/product.ts b/server/src/routes/product.ts index 150f5ba..b5638f7 100644 --- a/server/src/routes/product.ts +++ b/server/src/routes/product.ts @@ -18,12 +18,21 @@ router.get("/", async (req, res) => { router.post("/", authenticateToken, async (req, res) => { try { // Extract product data from the request body - const { name, price, description } = req.body; + const { + name, + price, + description, + imgPath, + stockCount, + category, + franchise, + } = req.body; // Validate the input - if (!name || !price || !description) { - return res - .status(400) - .json({ error: "name, price, and description args are required" }); + if (!name || !price || !description || category || franchise) { + return res.status(400).json({ + error: + "name, price, description, category, franchise args are required", + }); } const query = { name: name.toString }; @@ -36,6 +45,10 @@ router.post("/", authenticateToken, async (req, res) => { name, price, description, + imgPath, + stockCount, + category, + franchise, }); // Save the new user to the database await newProduct.save(); @@ -54,7 +67,15 @@ router.post("/", authenticateToken, async (req, res) => { router.put("/edit/:id", authenticateToken, async (req, res) => { try { const { id } = req.params; - const { name, price, description } = req.body; + const { + name, + price, + description, + imgPath, + stockCount, + category, + franchise, + } = req.body; if (!id) { return res.status(400).json({ error: "Product ID is required" }); } @@ -63,8 +84,12 @@ router.put("/edit/:id", authenticateToken, async (req, res) => { const update = { $set: { name: name.toString(), - price: price.toString(), + price: Number(price), details: description.toString(), + imgPath: imgPath.toString(), + stockCount: Number(stockCount), + category: category.toString(), + franchise: franchise.toString(), }, }; const options = { new: true };