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.
This commit is contained in:
Mathis H (Avnyr) 2024-07-15 11:43:46 +02:00
parent dea5f8c48b
commit c3bac6d348
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -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;
}
}