Add endpoint to clear user cart

master
nicekid1 1 month ago
parent 9e84860f48
commit 04716f6973
  1. 17
      src/cart/cart.controller.ts
  2. 7
      src/cart/cart.service.ts

@ -10,21 +10,21 @@ import { Invoice } from "src/invoice/entities/invoice.entity";
export class CartController { export class CartController {
constructor(private readonly cartService: CartService) {} constructor(private readonly cartService: CartService) {}
//create and a item to cart by user
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post() @Post()
async createAndAddItemToCart(@Body() addToCartDto: AddToCartDto, @Request() req: any): Promise<{ message: string; cartItem: Cart }> { async createAndAddItemToCart(@Body() addToCartDto: AddToCartDto, @Request() req: any): Promise<{ message: string; cartItem: Cart }> {
const userId = req.user.id; const userId = req.user.id;
return this.cartService.createAndAddItemToCart({ ...addToCartDto, userId }); return this.cartService.createAndAddItemToCart({ ...addToCartDto, userId });
} }
//get user cart items
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get() @Get()
async getUserOpenCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> { async getUserOpenCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> {
const userId = req.user.id; const userId = req.user.id;
return this.cartService.getUserOpenCart(userId); return this.cartService.getUserOpenCart(userId);
} }
//edit quantity an item in cart by user
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Patch(":productId") @Patch(":productId")
async updateCart(@Param("productId") productId: number, @Body() updateCartDto: UpdateCartDto, @Request() req: any): Promise<{ message: string; updatedCart: Cart }> { 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, updatedCart,
}; };
} }
//delete an item from cart by user
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Delete(":productId") @Delete(":productId")
async removeFromCart(@Param("productId") productId: number, @Request() req: any) { async removeFromCart(@Param("productId") productId: number, @Request() req: any) {
const userId = req.user.id; const userId = req.user.id;
return await this.cartService.removeFromCart(userId, productId); 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) @UseGuards(JwtAuthGuard)
@Get("checkout") @Get("checkout")
async processOrder(@Request() req: any): Promise<{ message: string; invoice: Invoice }> { async processOrder(@Request() req: any): Promise<{ message: string; invoice: Invoice }> {

@ -125,11 +125,12 @@ export class CartService {
} }
} }
//delete whole cart //delete whole cart by user
async clearCart(userId: number): Promise<void> { async clearCart(userId: number) {
await this.cartModel.destroy({ where: { userId } }); await this.cartModel.destroy({ where: { userId } });
return { message: "cart cleared successful" };
} }
//order(clearCart disable) //order(clearCart disable)
async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> { async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> {
try { try {

Loading…
Cancel
Save