From 725412c59c80e4f12f86669f28d309b5c7cd4b8a Mon Sep 17 00:00:00 2001 From: aliMohtarami Date: Wed, 22 Jan 2025 09:20:19 +0330 Subject: [PATCH] fix error handling of product endpoints --- src/products/products.controller.ts | 19 ++++++++++++++----- src/products/products.service.ts | 10 ++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/products/products.controller.ts b/src/products/products.controller.ts index 43ab505..687a2ff 100644 --- a/src/products/products.controller.ts +++ b/src/products/products.controller.ts @@ -9,6 +9,8 @@ import { RoleGuard } from "src/guard/role.guard"; export class ProductsController { constructor(private readonly productsService: ProductsService) {} + ////////////////////////////////////////products//////////////////////////////////////// + //create a new product (admin) @UseGuards(RoleGuard) @Post() async create(@Body() createProductDto: CreateProductDto) { @@ -18,28 +20,35 @@ export class ProductsController { product, }; } - + //list of all product @Get() async findAll(@Query() query: { search?: string; priceMin?: number; priceMax?: number }) { const { search, priceMin, priceMax } = query; return this.productsService.findAll(search, priceMin, priceMax); } - + //get a product detail @Get(":id") - async findOne(@Param("id") id: string): Promise { + async findOne(@Param("id") id: string){ return this.productsService.findOne(id); } + //edit a product info (admin) @UseGuards(RoleGuard) @Put(":id") async update( @Param("id") id: string, @Body() updateProductDto: UpdateProductDto - ): Promise { - return this.productsService.update(id, updateProductDto); + ){ + const product = await this.productsService.update(id, updateProductDto); + return { + message : 'product updated successful', + product + } } + //delete a product (admin) @UseGuards(RoleGuard) @Delete(':id') async remove(@Param('id') id: string): Promise<{ message: string }> { return this.productsService.remove(id); } + } diff --git a/src/products/products.service.ts b/src/products/products.service.ts index e033d59..44bd52f 100644 --- a/src/products/products.service.ts +++ b/src/products/products.service.ts @@ -26,6 +26,9 @@ export class ProductsService { const newProduct = await this.productModel.create(createProductDto); return newProduct; } catch (error) { + if(error instanceof HttpException){ + throw error + } throw new HttpException("An error occurred while creating or updating the product.", HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -80,6 +83,7 @@ export class ProductsService { where, limit, offset, + attributes: { exclude: ["description", "quantity", "createdAt", "updatedAt", "tags"] }, }); const totalPages = Math.ceil(total / limit); @@ -111,7 +115,7 @@ export class ProductsService { try { const { name, description, price, imageUrl, tags, quantity, brand, color, category } = updateProductDto; - let updated = false; // متغیر برای بررسی تغییرات + let updated = false; if (name && name !== product.name) { product.name = name; @@ -156,6 +160,9 @@ export class ProductsService { return product; } catch (error) { + if(error instanceof HttpException){ + throw error + } throw new HttpException("An error occurred while updating the product.", HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -177,7 +184,6 @@ export class ProductsService { if (error instanceof HttpException) { throw error; } - throw new HttpException("An unexpected error occurred while deleting the product.", HttpStatus.INTERNAL_SERVER_ERROR); } }