diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts index 96e793e..9aae8af 100644 --- a/src/controllers/ProductController.ts +++ b/src/controllers/ProductController.ts @@ -1,20 +1,31 @@ import { Request, Response } from 'express'; import { Product } from '../schemas/productSchema'; -// import { handleCreateProductError } from '../middlewares/errorHandler'; +import { createProduct } from '../models/productModel'; +import { ApiError } from '../utils/ApiError'; - -export async function createProduct(req: Request, res: Response) { +const create = async (req: Request, res: Response, next) => { try { - const product = new Product(req.body); - await product.save(); - res.json(product); - } catch (error) { - console.error('Error creating product:', error); - // handleCreateProductError(res, error); + const { name, description, price } = req.body; + const product = await createProduct({ + name, + description, + price, + userId: req.body.userId + }); + if(product instanceof Error) { + console.log("Error in create product") + return next(product); + } + res.status(201).json(product); + } catch { + const error = new ApiError('Error during product creation'); + error.statusCode = 500; + error.status = 'fail'; + next(error); } } -export async function listProducts(req: Request, res: Response) { +const getAll = async (req: Request, res: Response) => { try { const { page, limit } = req.query; const dbPage = Number(page) || 0; @@ -32,7 +43,7 @@ export async function getProduct(req: Request, res: Response) { const { id } = req.params; try { const product = await Product.findById(id, 'name description price'); - if(!product) { + if (!product) { res.status(404).json({ error: 'Product not found.' }); return; } @@ -42,3 +53,8 @@ export async function getProduct(req: Request, res: Response) { res.status(404).json({ error: 'Product not found.' }); } } + +export { + create, + getAll +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 7bd22fd..1f63eaf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,7 +41,6 @@ app.use('/products', productRouter); app.use('/cart', cartRouter); app.all('*', (req, res, next) => { - // res.status(404).json({ error: 'Route not found' }); const error = new ApiError('Are you lost?'); error.statusCode = 404; error.status = 'fail'; diff --git a/src/models/productModel.ts b/src/models/productModel.ts new file mode 100644 index 0000000..dc0236e --- /dev/null +++ b/src/models/productModel.ts @@ -0,0 +1,31 @@ +import { Product } from '../schemas/productSchema'; +import { Request, Response } from 'express'; +import { errorHandler } from '../middlewares/errorHandler'; +import { ApiError } from '../utils/ApiError'; + +const createProduct = async (product: any) => { + try { + + const newProduct = new Product(product); + const isExist = await Product.findOne({ name: product.name, userId: product.userId }); + if(isExist) { + const error = new ApiError('Product already exists'); + error.statusCode = 400; + error.status = 'fail'; + return error; + } + console.log("newProduct", newProduct); + await newProduct.save(); + return newProduct; + } catch { + const error = new ApiError('Error during product creation'); + error.statusCode = 500; + error.status = 'fail'; + return error; + } +} + + +export { + createProduct +} diff --git a/src/models/userModel.ts b/src/models/userModel.ts index 28b61aa..3c4b6bc 100644 --- a/src/models/userModel.ts +++ b/src/models/userModel.ts @@ -1,10 +1,8 @@ -import { User, IUser } from "../schemas/userSchema"; +import { User } from "../schemas/userSchema"; import validate from 'deep-email-validator'; import { ApiError } from "../utils/ApiError"; -import { response } from "express"; import bcrypt from 'bcryptjs'; - const createUser = async (user: any) => { if (!user.email || !user.password || !user.address) { diff --git a/src/routes/productRouter.ts b/src/routes/productRouter.ts index 573427f..acdbdca 100644 --- a/src/routes/productRouter.ts +++ b/src/routes/productRouter.ts @@ -1,12 +1,12 @@ import express from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; -import { createProduct, listProducts, getProduct } from '../controllers/productController'; +import { create, getAll } from '../controllers/productController'; const productRouter = express.Router(); -productRouter.post('/', authenticateToken, createProduct) -productRouter.get('/', listProducts); -productRouter.get('/:id', getProduct); +// productRouter.get('/', listProducts); +productRouter.post('/', authenticateToken, create) +// productRouter.get('/:id', getProduct); export default productRouter; \ No newline at end of file