Implement functionality to find a product by ID in product module

master
nicekid1 2 months ago
parent cfd85e729b
commit e058e82e6d
  1. 4
      src/products/products.controller.ts
  2. 18
      src/products/products.service.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<Product> {
return this.productsService.findOne(id);
}
}

@ -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<Product> {
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);
}
}
}

Loading…
Cancel
Save