Files
dmenu-api/src/modules/cart/controllers/cart.controller.ts
T
2025-11-23 00:02:26 +03:30

77 lines
3.6 KiB
TypeScript

import { Controller, Get, Post, Body, Patch, Delete, UseGuards, Param } from '@nestjs/common';
import { CartService } from '../providers/cart.service';
import { AddItemToCartDto } from '../dto/add-item.dto';
import { UpdateItemQuantityDto } from '../dto/update-item.dto';
import { ApplyCouponDto } from '../dto/apply-coupon.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('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.findOne(userId, restaurantId);
}
@Post('items')
@ApiOperation({ summary: 'Add item to cart (increments quantity if item exists)' })
@ApiBody({ type: AddItemToCartDto })
@ApiResponse({ status: 201, description: 'Item added to cart successfully' })
@ApiResponse({ status: 400, description: 'Bad request (e.g., insufficient stock)' })
@ApiResponse({ status: 404, description: 'Food not found' })
async addItem(@UserId() userId: string, @RestId() restaurantId: string, @Body() addItemDto: AddItemToCartDto) {
return this.cartService.addItem(userId, restaurantId, addItemDto);
}
@Patch('items/:itemId')
@ApiOperation({ summary: 'Update item quantity in cart' })
@ApiParam({ name: 'itemId', description: 'Item 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('itemId') itemId: string,
@Body() updateItemDto: UpdateItemQuantityDto,
) {
return this.cartService.updateItemQuantity(userId, restaurantId, itemId, updateItemDto);
}
@Delete('items/:itemId')
@ApiOperation({ summary: 'Remove item from cart' })
@ApiParam({ name: 'itemId', description: 'Item 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('itemId') itemId: string) {
return this.cartService.removeItem(userId, restaurantId, itemId);
}
@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);
}
}