diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts index f69fdc6..42e8e25 100644 --- a/src/controllers/ProductController.ts +++ b/src/controllers/ProductController.ts @@ -1,30 +1,16 @@ import { Request, Response } from 'express'; import { Product, IProduct } from '../schemas/productModel'; +import { handleCreateProductError } from '../middlewares/errorHandlers'; + export async function createProduct(req: Request, res: Response) { try { - const { name, description, price, userId } = req.body; - if(!name || !description || !price || !userId) { - res.status(400).json({ error: 'Name, description, price are required.' }); - return; - } - - const productExists = await Product.exists({ name, userId }); - if(productExists) { - res.status(400).json({ error: 'Product already exists.' }); - return; - } - const product: IProduct = await Product.create({ - name, - description, - price, - userId, - }); - - res.status(201).json(product); + const product = new Product(req.body); + await product.save(); + res.json(product); } catch (error) { console.error('Error creating product:', error); - res.status(500).json({ error: 'An error occurred while creating the product.' }); + handleCreateProductError(res, error); } } diff --git a/src/middlewares/errorHandlers.ts b/src/middlewares/errorHandlers.ts new file mode 100644 index 0000000..1c39482 --- /dev/null +++ b/src/middlewares/errorHandlers.ts @@ -0,0 +1,18 @@ +import { Request, Response, NextFunction } from 'express'; + + + +export function handleCreateProductError(res: Response, error: Error) { + let statusCode = 500; + let errorMessage = 'An error occurred while creating the product.'; + + if (error.message === 'Missing required fields.') { + statusCode = 400; + errorMessage = 'Missing required fields.'; + } else if (error.message === 'Product already exists.') { + statusCode = 409; + errorMessage = 'Product already exists.'; + } + + res.status(statusCode).json({ error: errorMessage }); +} \ No newline at end of file diff --git a/src/routes/cartRouter.ts b/src/routes/cartRouter.ts index 5a87d44..93acab3 100644 --- a/src/routes/cartRouter.ts +++ b/src/routes/cartRouter.ts @@ -3,8 +3,6 @@ import { authenticateToken } from '../middlewares/checkAuth'; import { addToCart, listCart, checkout, clearCart } from '../controllers/cartController'; import { checkUuid } from '../middlewares/checkUuid'; - - const cartRouter = express.Router(); cartRouter.post('/', [authenticateToken], addToCart); diff --git a/src/schemas/orderModel.ts b/src/schemas/orderModel.ts index bc0eaa0..b9c619c 100644 --- a/src/schemas/orderModel.ts +++ b/src/schemas/orderModel.ts @@ -1,7 +1,5 @@ import mongoose, { Schema, Document } from 'mongoose'; - - interface IOrder extends Document { userId: string; products: { [itemId: string]: number }; diff --git a/src/schemas/productModel.ts b/src/schemas/productModel.ts index 6385862..00206b4 100644 --- a/src/schemas/productModel.ts +++ b/src/schemas/productModel.ts @@ -1,4 +1,5 @@ import mongoose, { Schema, Document } from 'mongoose'; +import { Request } from 'express'; interface IProduct extends Document { name: string; @@ -18,10 +19,27 @@ const ProductSchema: Schema = new Schema({ updatedAt: { type: Date, default: Date.now }, }); -const Product = mongoose.model('Product', ProductSchema); +ProductSchema.pre('save', async function (next) { + try { + if (!this.name || !this.description || !this.price || !this.userId) { + throw new Error('Missing required fields.'); + } + const productExists = await mongoose.model('Product').exists({ name: this.name, userId: this.userId }); + if (productExists) { + throw new Error('Product already exists.'); + } + + next(); + } catch (error) { + next(error); + } +}); + +const Product = mongoose.model('Product', ProductSchema); ProductSchema.index({ name: 1, userId: 1 }, { unique: true }); + export { Product, IProduct