diff --git a/src/cart/cart.controller.ts b/src/cart/cart.controller.ts index 7650774..9b35dfe 100644 --- a/src/cart/cart.controller.ts +++ b/src/cart/cart.controller.ts @@ -10,21 +10,21 @@ import { Invoice } from "src/invoice/entities/invoice.entity"; export class CartController { constructor(private readonly cartService: CartService) {} - + //create and a item to cart by user @UseGuards(JwtAuthGuard) @Post() async createAndAddItemToCart(@Body() addToCartDto: AddToCartDto, @Request() req: any): Promise<{ message: string; cartItem: Cart }> { const userId = req.user.id; return this.cartService.createAndAddItemToCart({ ...addToCartDto, userId }); } - + //get user cart items @UseGuards(JwtAuthGuard) @Get() async getUserOpenCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> { const userId = req.user.id; return this.cartService.getUserOpenCart(userId); } - + //edit quantity an item in cart by user @UseGuards(JwtAuthGuard) @Patch(":productId") async updateCart(@Param("productId") productId: number, @Body() updateCartDto: UpdateCartDto, @Request() req: any): Promise<{ message: string; updatedCart: Cart }> { @@ -35,14 +35,21 @@ export class CartController { updatedCart, }; } - + //delete an item from cart by user @UseGuards(JwtAuthGuard) @Delete(":productId") async removeFromCart(@Param("productId") productId: number, @Request() req: any) { const userId = req.user.id; return await this.cartService.removeFromCart(userId, productId); } - + //clear whole cart by user + @UseGuards(JwtAuthGuard) + @Get("clear-cart") + async clearCart(@Request() req: any) { + const userId = req.user.id; + return await this.cartService.clearCart(userId); + } + //get checkout process @UseGuards(JwtAuthGuard) @Get("checkout") async processOrder(@Request() req: any): Promise<{ message: string; invoice: Invoice }> { diff --git a/src/cart/cart.service.ts b/src/cart/cart.service.ts index 0aa51f4..97d7db4 100644 --- a/src/cart/cart.service.ts +++ b/src/cart/cart.service.ts @@ -125,11 +125,12 @@ export class CartService { } } - //delete whole cart - async clearCart(userId: number): Promise { + //delete whole cart by user + async clearCart(userId: number) { await this.cartModel.destroy({ where: { userId } }); + return { message: "cart cleared successful" }; } - + //order(clearCart disable) async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> { try {