This commit is contained in:
2025-11-23 00:02:26 +03:30
parent cd4dc69aef
commit c8b0ee3c56
6 changed files with 373 additions and 345 deletions
+53 -40
View File
@@ -1,8 +1,9 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
import { Controller, Get, Post, Body, Patch, Delete, UseGuards, Param } from '@nestjs/common';
import { CartService } from '../providers/cart.service';
import { CreateCartDto } from '../dto/create-cart.dto';
import { UpdateCartDto } from '../dto/update-cart.dto';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiBody } from '@nestjs/swagger';
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';
@@ -14,50 +15,62 @@ import { RestId } from 'src/common/decorators/rest-id.decorator';
export class CartController {
constructor(private readonly cartService: CartService) {}
@Post()
@ApiOperation({ summary: 'Create or update user cart' })
@ApiBody({ type: CreateCartDto })
@ApiResponse({ status: 201, description: 'Cart created successfully' })
async create(@UserId() userId: string, @RestId() restaurantId: string, @Body() createCartDto: CreateCartDto) {
return this.cartService.create(userId, restaurantId, createCartDto);
}
@Get()
@ApiOperation({ summary: 'Get all active carts for the current user' })
@ApiResponse({ status: 200, description: 'Carts retrieved successfully' })
async findAll(@UserId() userId: string) {
return this.cartService.findByUser(userId);
}
@Get(':id')
@ApiOperation({ summary: 'Get a specific cart by ID' })
@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(@Param('id') id: string, @UserId() userId: string) {
return this.cartService.findOne(id, userId);
async findOne(@UserId() userId: string, @RestId() restaurantId: string) {
return this.cartService.findOne(userId, restaurantId);
}
@Patch(':id')
@ApiOperation({ summary: 'Update a cart' })
@ApiBody({ type: UpdateCartDto })
@ApiResponse({ status: 200, description: 'Cart updated successfully' })
@ApiResponse({ status: 404, description: 'Cart not found' })
async update(@Param('id') id: string, @Body() updateCartDto: UpdateCartDto, @UserId() userId: string) {
return this.cartService.update(id, userId, updateCartDto);
@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);
}
@Delete('clear')
@ApiOperation({ summary: 'Clear all active carts for the current user' })
@ApiResponse({ status: 200, description: 'All carts cleared successfully' })
async clearAll(@UserId() userId: string) {
return this.cartService.clearUserCart(userId);
@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(':id')
@ApiOperation({ summary: 'Delete a cart (soft delete)' })
@ApiResponse({ status: 200, description: 'Cart deleted successfully' })
@ApiResponse({ status: 404, description: 'Cart not found' })
async remove(@Param('id') id: string, @UserId() userId: string) {
return this.cartService.remove(id, userId);
@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);
}
}