91 lines
3.4 KiB
TypeScript
91 lines
3.4 KiB
TypeScript
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
|
import { FoodService } from '../providers/food.service';
|
|
import { CreateFoodDto } from '../dto/create-food.dto';
|
|
import { UpdateFoodDto } from '../dto/update-food.dto';
|
|
import { FindFoodsDto } from '../dto/find-foods.dto';
|
|
import {
|
|
ApiTags,
|
|
ApiOperation,
|
|
ApiCreatedResponse,
|
|
ApiOkResponse,
|
|
ApiNotFoundResponse,
|
|
ApiQuery,
|
|
ApiBody,
|
|
ApiParam,
|
|
ApiBearerAuth,
|
|
ApiHeader,
|
|
} from '@nestjs/swagger';
|
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
|
import { RestId } from 'src/common/decorators';
|
|
|
|
@ApiTags('foods')
|
|
@Controller()
|
|
export class FoodController {
|
|
constructor(private readonly foodsService: FoodService) {}
|
|
|
|
@Get('public/foods/restaurant/:slug')
|
|
@ApiOperation({ summary: 'Get all foods by restaurant slug' })
|
|
@ApiHeader({ name: 'X-Slug', required: true, description: 'Restaurant slug identifier', example: 'zhivan' })
|
|
@ApiParam({ name: 'slug', required: true, description: 'Restaurant Slug' })
|
|
@ApiOkResponse({ description: 'List of all foods for the restaurant' })
|
|
findAllByRestaurant(@Param('slug') slug: string) {
|
|
return this.foodsService.findAllByRestaurant(slug);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Post('admin/foods')
|
|
@ApiOperation({ summary: 'Create a new food' })
|
|
@ApiCreatedResponse({ description: 'The food has been successfully created.', type: CreateFoodDto })
|
|
@ApiBody({ type: CreateFoodDto })
|
|
create(@Body() createFoodDto: CreateFoodDto, @RestId() restId: string) {
|
|
return this.foodsService.create(restId, createFoodDto);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Get('admin/foods')
|
|
@ApiOperation({ summary: 'Get a paginated list of foods' })
|
|
@ApiOkResponse({ description: 'Paginated list of foods' })
|
|
@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 })
|
|
findAll(@Query() dto: FindFoodsDto, @RestId() restId: string) {
|
|
return this.foodsService.findAll(restId, dto);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Get('admin/foods/:id')
|
|
@ApiOperation({ summary: 'Get a food by id' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
@ApiOkResponse({ description: 'The food', type: CreateFoodDto })
|
|
@ApiNotFoundResponse({ description: 'Food not found' })
|
|
findById(@Param('id') id: string) {
|
|
return this.foodsService.findById(id);
|
|
}
|
|
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Patch('admin/foods/:id')
|
|
@ApiOperation({ summary: 'Update a food' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
@ApiBody({ type: UpdateFoodDto })
|
|
@ApiOkResponse({ description: 'The updated food', type: UpdateFoodDto })
|
|
update(@Param('id') id: string, @Body() updateFoodDto: UpdateFoodDto) {
|
|
return this.foodsService.update(id, updateFoodDto);
|
|
}
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Delete('admin/foods/:id')
|
|
@ApiOperation({ summary: 'Delete (soft) a food' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
remove(@Param('id') id: string) {
|
|
return this.foodsService.remove(id);
|
|
}
|
|
}
|