From c3bac6d348a61dd2cfbde21ccd1b224c53f9711d Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 15 Jul 2024 11:43:46 +0200 Subject: [PATCH] feat(products): implement edit and delete product functions of the controller. Implemented functionality to edit and delete products from the database in the products controller. The functions now throw a BadRequestException when update or deletion fails. The getAllProducts function is now also enabled to support pagination. --- src/products/products.controller.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/products/products.controller.ts b/src/products/products.controller.ts index fa0e70e..75fca83 100644 --- a/src/products/products.controller.ts +++ b/src/products/products.controller.ts @@ -30,26 +30,31 @@ export class ProductsController { return result.statement } - //TODO list all product with pagination. @HttpCode(HttpStatus.OK) @Get("all") async getAllProducts(@Query('page') page: number) { return this.productsService.findAll(page || 0, 20); } - //TODO edit a product (admin) @HttpCode(HttpStatus.OK) @UseGuards(AdminGuard) @Patch(':id') async editProduct(@Param('id') id: string, @Body() dto: EditProductDto) { - + const result = await this.productsService.edit(id, dto); + if (result.count !== 1) { + throw new BadRequestException('Update failed'); + } + return result.statement; } - //TODO delete a product (admin) @HttpCode(HttpStatus.ACCEPTED) @UseGuards(AdminGuard) @Delete(':id') async deleteProduct(@Param('id') id: string) { - + const result = await this.productsService.delete(id); + if (result.count !== 1) { + throw new BadRequestException('Deletion failed'); + } + return result.statement; } }