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:
parent
dea5f8c48b
commit
c3bac6d348
@ -30,26 +30,31 @@ export class ProductsController {
|
|||||||
return result.statement
|
return result.statement
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO list all product with pagination.
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@Get("all")
|
@Get("all")
|
||||||
async getAllProducts(@Query('page') page: number) {
|
async getAllProducts(@Query('page') page: number) {
|
||||||
return this.productsService.findAll(page || 0, 20);
|
return this.productsService.findAll(page || 0, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO edit a product (admin)
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@UseGuards(AdminGuard)
|
@UseGuards(AdminGuard)
|
||||||
@Patch(':id')
|
@Patch(':id')
|
||||||
async editProduct(@Param('id') id: string, @Body() dto: EditProductDto) {
|
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)
|
@HttpCode(HttpStatus.ACCEPTED)
|
||||||
@UseGuards(AdminGuard)
|
@UseGuards(AdminGuard)
|
||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
async deleteProduct(@Param('id') id: string) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user