Files
dmenu-api/src/modules/cart/controllers/cart.controller.ts
T
2025-12-03 09:05:35 +03:30

145 lines
6.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 { UpdateItemQuantityDto } from '../dto/update-item.dto';
import { ApplyCouponDto } from '../dto/apply-coupon.dto';
import { SetAddressDto } from '../dto/set-address.dto';
import { SetPaymentMethodDto } from '../dto/set-payment-method.dto';
import { SetDeliveryMethodDto } from '../dto/set-delivery-method.dto';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiBody, ApiParam } from '@nestjs/swagger';
import { AuthGuard } from '../../auth/guards/auth.guard';
import { UserId } from 'src/common/decorators/user-id.decorator';
import { RestId } from 'src/common/decorators/rest-id.decorator';
@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 restaurant' })
@ApiResponse({ status: 200, description: 'Cart retrieved successfully' })
@ApiResponse({ status: 404, description: 'Cart not found' })
async findOne(@UserId() userId: string, @RestId() restaurantId: string) {
return this.cartService.getOrCreateCart(userId, restaurantId);
}
@Post('items/:foodId/increment')
@ApiOperation({ summary: 'Increment item quantity in cart by 1' })
@ApiParam({ name: 'foodId', description: 'Food ID to increment in cart' })
@ApiResponse({ status: 201, description: 'Item incremented in cart successfully' })
@ApiResponse({ status: 400, description: 'Bad request (e.g., insufficient stock)' })
@ApiResponse({ status: 404, description: 'Food not found' })
async incrementItem(@UserId() userId: string, @RestId() restaurantId: string, @Param('foodId') foodId: string) {
return this.cartService.incrementItem(userId, restaurantId, foodId);
}
@Post('items/:foodId/decrement')
@ApiOperation({ summary: 'Decrement item quantity in cart by 1 (removes item if quantity reaches 0)' })
@ApiParam({ name: 'foodId', description: 'Food ID to decrement in cart' })
@ApiResponse({ status: 200, description: 'Item decremented in cart successfully' })
@ApiResponse({ status: 404, description: 'Item not found in cart' })
async decrementItem(@UserId() userId: string, @RestId() restaurantId: string, @Param('foodId') foodId: string) {
return this.cartService.decrementItem(userId, restaurantId, foodId);
}
@Post('items/bulk')
@ApiOperation({ summary: 'Bulk add items to cart (increments quantity if items exist)' })
@ApiBody({ type: BulkAddItemsToCartDto })
@ApiResponse({ status: 201, description: 'Items added to cart successfully' })
@ApiResponse({ status: 400, description: 'Bad request (e.g., insufficient stock, invalid items)' })
@ApiResponse({ status: 404, description: 'Food not found' })
bulkAddItems(
@UserId() userId: string,
@RestId() restaurantId: string,
@Body() bulkAddItemsDto: BulkAddItemsToCartDto,
) {
return this.cartService.bulkAddItems(userId, restaurantId, bulkAddItemsDto);
}
@Patch('items/:foodId')
@ApiOperation({ summary: 'Update item quantity in cart' })
@ApiParam({ name: 'foodId', description: 'Food ID in the cart' })
@ApiBody({ type: UpdateItemQuantityDto })
@ApiResponse({ status: 200, description: 'Item quantity updated successfully' })
@ApiResponse({ status: 400, description: 'Bad request (e.g., insufficient stock)' })
@ApiResponse({ status: 404, description: 'Item not found in cart' })
async updateItemQuantity(
@UserId() userId: string,
@RestId() restaurantId: string,
@Param('foodId') foodId: string,
@Body() updateItemDto: UpdateItemQuantityDto,
) {
return this.cartService.updateItemQuantity(userId, restaurantId, foodId, updateItemDto);
}
@Delete('items/:foodId')
@ApiOperation({ summary: 'Remove item from cart' })
@ApiParam({ name: 'foodId', description: 'Food ID in the cart' })
@ApiResponse({ status: 200, description: 'Item removed from cart successfully' })
@ApiResponse({ status: 404, description: 'Item not found in cart' })
async removeItem(@UserId() userId: string, @RestId() restaurantId: string, @Param('foodId') foodId: string) {
return this.cartService.removeItem(userId, restaurantId, foodId);
}
@Delete()
@ApiOperation({ summary: 'Clear entire cart' })
@ApiResponse({ status: 200, description: 'Cart cleared successfully' })
async clearCart(@UserId() userId: string, @RestId() restaurantId: string) {
return this.cartService.clearCart(userId, restaurantId);
}
@Post('apply-coupon')
@ApiOperation({ summary: 'Apply coupon to cart' })
@ApiBody({ type: ApplyCouponDto })
@ApiResponse({ status: 200, description: 'Coupon applied successfully' })
@ApiResponse({ status: 400, description: 'Invalid coupon code' })
async applyCoupon(@UserId() userId: string, @RestId() restaurantId: string, @Body() applyCouponDto: ApplyCouponDto) {
return this.cartService.applyCoupon(userId, restaurantId, applyCouponDto);
}
@Delete('coupon')
@ApiOperation({ summary: 'Remove coupon from cart' })
@ApiResponse({ status: 200, description: 'Coupon removed successfully' })
async removeCoupon(@UserId() userId: string, @RestId() restaurantId: string) {
return this.cartService.removeCoupon(userId, restaurantId);
}
@Patch('address')
@ApiOperation({ summary: 'Set address for cart' })
@ApiBody({ type: SetAddressDto })
@ApiResponse({ status: 200, description: 'Address set successfully' })
@ApiResponse({ status: 400, description: 'Bad request (e.g., address does not belong to user)' })
@ApiResponse({ status: 404, description: 'Address not found' })
async setAddress(@UserId() userId: string, @RestId() restaurantId: string, @Body() setAddressDto: SetAddressDto) {
return this.cartService.setAddress(userId, restaurantId, setAddressDto);
}
@Patch('payment-method')
@ApiOperation({ summary: 'Set payment method for cart' })
@ApiBody({ type: SetPaymentMethodDto })
@ApiResponse({ status: 200, description: 'Payment method set successfully' })
@ApiResponse({ status: 400, description: 'Bad request (e.g., payment method is not active)' })
@ApiResponse({ status: 404, description: 'Payment method not found for restaurant' })
async setPaymentMethod(
@UserId() userId: string,
@RestId() restaurantId: string,
@Body() setPaymentMethodDto: SetPaymentMethodDto,
) {
return this.cartService.setPaymentMethod(userId, restaurantId, setPaymentMethodDto);
}
@Patch('delivery-method')
@ApiOperation({ summary: 'Set delivery method for cart' })
@ApiBody({ type: SetDeliveryMethodDto })
setDeliveryMethod(
@UserId() userId: string,
@RestId() restaurantId: string,
@Body() setDeliveryMethodDto: SetDeliveryMethodDto,
) {
return this.cartService.setDeliveryMethod(userId, restaurantId, setDeliveryMethodDto);
}
}