Skip to content

Commit

Permalink
Added more details to product schema backend
Browse files Browse the repository at this point in the history
Added image path, stock count, category, franchise, rating to product
Created new document schema for product reviews
#14
  • Loading branch information
kianz20 committed Sep 5, 2024
1 parent 1bdaac4 commit 146c096
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
16 changes: 13 additions & 3 deletions server/src/models/Product.ts
Original file line number Diff line number Diff line change
@@ -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<Product> = 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>("Product", ProductSchema);
18 changes: 18 additions & 0 deletions server/src/models/Review.ts
Original file line number Diff line number Diff line change
@@ -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<Review> = new Schema({
review: { type: String, required: true },
reviewer: { type: String, required: true },
stars: { type: Number, required: true },
});

export default mongoose.model<Review>("Product", ReviewSchema);
39 changes: 32 additions & 7 deletions server/src/routes/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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();
Expand All @@ -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" });
}
Expand All @@ -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 };
Expand Down

0 comments on commit 146c096

Please sign in to comment.