121 lines
4.6 KiB
TypeScript
121 lines
4.6 KiB
TypeScript
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
|
import { ProductService } from '../providers/product.service';
|
|
import { CreateProductDto } from '../dto/create-product.dto';
|
|
import { UpdateFoodDto } from '../dto/update-product.dto';
|
|
import { FindProductsDto } from '../dto/find-products.dto';
|
|
import {
|
|
ApiTags,
|
|
ApiOperation,
|
|
ApiQuery,
|
|
ApiBody,
|
|
ApiParam,
|
|
ApiBearerAuth,
|
|
ApiHeader,
|
|
} from '@nestjs/swagger';
|
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
|
import { ShopId, UserId } from 'src/common/decorators';
|
|
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
|
|
import { OptionalAuthGuard } from 'src/modules/auth/guards/optinalAuth.guard';
|
|
import { API_HEADER_SLUG } from 'src/common/constants';
|
|
import { Permission } from 'src/common/enums/permission.enum';
|
|
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
|
|
|
@ApiTags('products')
|
|
@Controller()
|
|
export class FoodController {
|
|
constructor(private readonly productService: ProductService) { }
|
|
|
|
@Get('public/products/shop/:slug')
|
|
@ApiOperation({ summary: 'Get all products by shop slug' })
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiParam({ name: 'slug', required: true, description: 'Shop Slug' })
|
|
findAllByRestaurant(@Param('slug') slug: string) {
|
|
return this.productService.findByShop(slug);
|
|
}
|
|
|
|
@Get('public/products/:id')
|
|
@UseGuards(OptionalAuthGuard)
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({ summary: 'Get a product by id' })
|
|
findPublicFoodById(@Param('id') productId: string, @UserId() userId: string): Promise<any> {
|
|
const a = this.productService.findPublicById(productId, userId);
|
|
return a;
|
|
}
|
|
|
|
@Post('public/products/:id/favorite')
|
|
@UseGuards(AuthGuard)
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({ summary: 'toggle a product as favorite' })
|
|
@ApiParam({ name: 'id', required: true, description: 'Product ID' })
|
|
setAsFavorite(@Param('id') productId: string, @UserId() userId: string) {
|
|
return this.productService.toggleFavorite(userId, productId);
|
|
}
|
|
|
|
@Get('public/products/favorite')
|
|
@UseGuards(AuthGuard)
|
|
@ApiHeader(API_HEADER_SLUG)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({ summary: 'get my favorites' })
|
|
getMyFavorites(@UserId() userId: string, @ShopId() shopId: string) {
|
|
return this.productService.getMyFavorites(userId, shopId);
|
|
}
|
|
|
|
/* ---------------------------------- Admin ---------------------------------- */
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Permissions(Permission.MANAGE_FOODS)
|
|
@Post('admin/products')
|
|
@ApiOperation({ summary: 'Create a new product' })
|
|
create(@Body() createFoodDto: CreateProductDto, @ShopId() shopId: string) {
|
|
return this.productService.create(shopId, createFoodDto);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Permissions(Permission.MANAGE_FOODS)
|
|
@Get('admin/products')
|
|
@ApiOperation({ summary: 'Get a paginated list of products' })
|
|
@ApiQuery({ name: 'page', required: false, type: Number })
|
|
@ApiQuery({ name: 'limit', required: false, type: Number })
|
|
@ApiQuery({ name: 'search', required: false, type: String })
|
|
@ApiQuery({ name: 'orderBy', required: false, type: String })
|
|
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
|
|
@ApiQuery({ name: 'categoryId', required: false, type: String })
|
|
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
|
|
async findAll(@Query() dto: FindProductsDto, @ShopId() shopId: string) {
|
|
const result = await this.productService.findAll(shopId, dto);
|
|
return result;
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Permissions(Permission.MANAGE_FOODS)
|
|
@Get('admin/products/:id')
|
|
@ApiOperation({ summary: 'Get a product by id' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
findById(@Param('id') id: string, @ShopId() shopId: string) {
|
|
return this.productService.findAdminById(shopId, id);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Permissions(Permission.MANAGE_FOODS)
|
|
@Patch('admin/products/:id')
|
|
@ApiOperation({ summary: 'Update a product' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
@ApiBody({ type: UpdateFoodDto })
|
|
update(@Param('id') id: string, @Body() updateFoodDto: UpdateFoodDto, @ShopId() shopId: string) {
|
|
return this.productService.update(shopId, id, updateFoodDto);
|
|
}
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Permissions(Permission.MANAGE_FOODS)
|
|
@Delete('admin/products/:id')
|
|
@ApiOperation({ summary: 'Delete (soft) a product' })
|
|
remove(@Param('id') id: string, @ShopId() shopId: string) {
|
|
return this.productService.remove(shopId, id);
|
|
}
|
|
}
|