Files
dkala-api/src/modules/cart/controllers/cart.controller.ts
T
2026-02-09 15:47:17 +03:30

92 lines
3.8 KiB
TypeScript

import { Controller, Get, Post, Body, Patch, Delete, UseGuards, Param } from '@nestjs/common';
import { CartService } from '../providers/cart.service';
import { BulkAddItemsToCartDto } from '../dto/bulk-add-items.dto';
import { ApplyCouponDto } from '../dto/apply-coupon.dto';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiBody, ApiParam, ApiHeader } from '@nestjs/swagger';
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
import { UserId } from 'src/common/decorators/user-id.decorator';
import { ShopId } from 'src/common/decorators/rest-id.decorator';
import { SetAllCartParmsDto } from '../dto/set-all-cart-params.dto';
import { API_HEADER_SLUG } from 'src/common/constants/index';
@UseGuards(AuthGuard)
@ApiBearerAuth()
@ApiTags('cart')
@Controller('public/cart')
export class CartController {
constructor(private readonly cartService: CartService) { }
@Get()
@ApiOperation({ summary: 'Get cart for current user and shop' })
@ApiHeader(API_HEADER_SLUG)
async findOne(@UserId() userId: string, @ShopId() shopId: string) {
return this.cartService.getOrCreateCart(userId, shopId);
}
@Post('product/:productId/increment')
@ApiOperation({ summary: 'Increment item quantity in cart by 1' })
@ApiHeader(API_HEADER_SLUG)
@ApiParam({ name: 'productId', description: 'Product ID to increment in cart' })
async incrementItem(@UserId() userId: string, @ShopId() shopId: string, @Param('productId') productId: string) {
return this.cartService.incrementItem(userId, shopId, productId, 1);
}
@Post('product/:productId/decrement')
@ApiOperation({ summary: 'Decrement item quantity in cart by 1 (removes item if quantity reaches 0)' })
@ApiHeader(API_HEADER_SLUG)
@ApiParam({ name: 'productId', description: 'Product ID to decrement in cart' })
async decrementItem(@UserId() userId: string, @ShopId() shopId: string, @Param('productId') productId: string) {
return this.cartService.decrementItem(userId, shopId, productId);
}
@Post('product/bulk')
@ApiOperation({ summary: 'Bulk add items to cart (increments quantity if items exist)' })
@ApiHeader(API_HEADER_SLUG)
@ApiBody({ type: BulkAddItemsToCartDto })
bulkAddItems(
@UserId() userId: string,
@ShopId() shopId: string,
@Body() bulkAddItemsDto: BulkAddItemsToCartDto,
) {
return this.cartService.bulkAddItems(userId, shopId, bulkAddItemsDto);
}
@Delete('product/:productId')
@ApiOperation({ summary: 'Remove item from cart' })
@ApiHeader(API_HEADER_SLUG)
@ApiParam({ name: 'productId', description: 'Product ID in the cart' })
async removeItem(@UserId() userId: string, @ShopId() shopId: string, @Param('productId') productId: string) {
return this.cartService.removeItem(userId, shopId, productId);
}
@Delete()
@ApiOperation({ summary: 'Clear entire cart' })
@ApiHeader(API_HEADER_SLUG)
async clearCart(@UserId() userId: string, @ShopId() shopId: string) {
return this.cartService.clearCart(userId, shopId);
}
@Post('coupon')
@ApiOperation({ summary: 'Apply coupon to cart' })
@ApiHeader(API_HEADER_SLUG)
async applyCoupon(@UserId() userId: string, @ShopId() shopId: string, @Body() applyCouponDto: ApplyCouponDto) {
return this.cartService.applyCoupon(userId, shopId, applyCouponDto);
}
@Delete('coupon')
@ApiOperation({ summary: 'Remove coupon from cart' })
@ApiHeader(API_HEADER_SLUG)
async removeCoupon(@UserId() userId: string, @ShopId() shopId: string) {
return this.cartService.removeCoupon(userId, shopId);
}
@Patch('all')
@ApiOperation({ summary: 'Set all cart params' })
@ApiHeader(API_HEADER_SLUG)
@ApiBody({ type: SetAllCartParmsDto })
setAllCartParams(@UserId() userId: string, @ShopId() shopId: string, @Body() dto: SetAllCartParmsDto) {
return this.cartService.setAllCartParams(userId, shopId, dto);
}
}