From 2baa649390b5b19144fe9d06432491d9613b79bc Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Fri, 9 Jun 2023 19:04:21 +0300 Subject: [PATCH] done adding new product --- src/controllers/ProductController.ts | 21 +++++++++++++++++++++ src/controllers/UserController.ts | 11 +++++++---- src/middlewares/checkAuth.ts | 2 +- src/models/{User.ts => Schema.ts} | 4 +++- src/routes/product.ts | 11 +++++++---- 5 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 src/controllers/ProductController.ts rename src/models/{User.ts => Schema.ts} (89%) 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