diff --git a/src/products/products.controller.ts b/src/products/products.controller.ts index bc51fa5..428b5c6 100644 --- a/src/products/products.controller.ts +++ b/src/products/products.controller.ts @@ -19,5 +19,9 @@ export class ProductsController { const { search, priceMin, priceMax } = query; return this.productsService.findAll(search, priceMin, priceMax); } + @Get(':id') + async findOne(@Param('id') id: string): Promise { + return this.productsService.findOne(id); + } } diff --git a/src/products/products.service.ts b/src/products/products.service.ts index 22c446e..cf9eb55 100644 --- a/src/products/products.service.ts +++ b/src/products/products.service.ts @@ -50,4 +50,22 @@ export class ProductsService { throw new HttpException("An error occurred while retrieving products.", HttpStatus.INTERNAL_SERVER_ERROR); } } + + async findOne(id: string): Promise { + try { + const product = await this.productModel.findByPk(id); + + if (!product) { + throw new HttpException("Product not found with the given id.", HttpStatus.NOT_FOUND); + } + + return product; + } catch (error) { + if (error instanceof HttpException) { + throw error; + } + + throw new HttpException("An error occurred while retrieving the product.", HttpStatus.INTERNAL_SERVER_ERROR); + } + } }