diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts new file mode 100644 index 0000000..fc9e3a2 --- /dev/null +++ b/src/controllers/ProductController.ts @@ -0,0 +1,21 @@ +import { Request, Response } from 'express'; +import { Product, IProduct } from '../models/Schema'; + +export async function createProduct(req: Request, res: Response) { + try { + const { name, description, price, userId } = req.body; + const product: IProduct = await Product.create({ + name, + description, + price, + userId: userId, + }); + + res.status(201).json(product); + } catch (error) { + console.error('Error creating product:', error); + res.status(500).json({ error: 'An error occurred while creating the product.' }); + } +} + + diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index ef1614f..9d3fa45 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -1,14 +1,17 @@ -import express, { Request, Response } from 'express'; +import { Request, Response } from 'express'; import bcrypt from 'bcryptjs'; import jwt from 'jsonwebtoken'; -import { User, IUser } from '../models/User'; +import { User, IUser } from '../models/Schema'; import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth'; -const app = express(); - export async function createUser(req: Request, res: Response) { try { const { firstName, lastName, email, password, address } = req.body; + // validate the request body first + if (!(email && password && firstName && lastName && address)) { + return res.status(400).json({ error: 'All inputs are required' }); + } + const hashedPassword = await bcrypt.hash(password, 10); const user: IUser = await User.create({ firstName, diff --git a/src/middlewares/checkAuth.ts b/src/middlewares/checkAuth.ts index 927e42b..b0a0b5c 100644 --- a/src/middlewares/checkAuth.ts +++ b/src/middlewares/checkAuth.ts @@ -18,7 +18,7 @@ export function authenticateToken(req: AuthenticatedRequest, res: Response, next if (err) { return res.status(401).json({ error: 'In Valid Token' }); } - req.userId = decoded.userId; + req.body.userId = decoded.userId; next(); }); } diff --git a/src/models/User.ts b/src/models/Schema.ts similarity index 89% rename from src/models/User.ts rename to src/models/Schema.ts index 7223b16..7eb5892 100644 --- a/src/models/User.ts +++ b/src/models/Schema.ts @@ -33,11 +33,13 @@ const ProductSchema: Schema = new Schema({ name: { type: String, required: true }, description: { type: String, required: true }, price: { type: Number, required: true }, - userId: { type: Schema.Types.ObjectId, ref: 'User' }, + userId: { type: Schema.Types.ObjectId, ref: 'User' , required: true}, createdAt: { type: Date, default: Date.now }, updatedAt: { type: Date, default: Date.now }, }); +ProductSchema.index({ name: 1, userId: 1 }, { unique: true }); + const User = mongoose.model('User', UserSchema); const Product = mongoose.model('Product', ProductSchema); diff --git a/src/routes/product.ts b/src/routes/product.ts index 20353c6..ad2688b 100644 --- a/src/routes/product.ts +++ b/src/routes/product.ts @@ -1,10 +1,13 @@ -import express from 'express'; +import express, { Request } from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; +import { createProduct } from '../controllers/ProductController'; + + const productRouter = express.Router(); -productRouter.post('/', authenticateToken, (req, res) => { - res.send('Create product'); -}) +// Create a product: This should require an authenticated user to provide a +// product name, description, and price. +productRouter.post('/', authenticateToken, createProduct) export default productRouter; \ No newline at end of file