food
This commit is contained in:
@@ -1,70 +1,65 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query } 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 { CategoryService } from '../providers/category.service';
|
||||
import { CreateCategoryDto } from '../dto/create-category.dto';
|
||||
import { UpdateCategoryDto } from '../dto/update-category.dto';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiResponse,
|
||||
ApiCreatedResponse,
|
||||
ApiOkResponse,
|
||||
ApiNotFoundResponse,
|
||||
ApiQuery,
|
||||
ApiBody,
|
||||
ApiParam,
|
||||
ApiBody,
|
||||
ApiQuery,
|
||||
} from '@nestjs/swagger';
|
||||
|
||||
@ApiTags('foods')
|
||||
@Controller('foods')
|
||||
@ApiTags('categories')
|
||||
@Controller('categories')
|
||||
export class CategoryController {
|
||||
constructor(private readonly foodService: FoodService) {}
|
||||
constructor(private readonly categoryService: CategoryService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create a new food' })
|
||||
@ApiCreatedResponse({ description: 'The food has been successfully created.', type: CreateFoodDto })
|
||||
@ApiBody({ type: CreateFoodDto })
|
||||
create(@Body() createFoodDto: CreateFoodDto) {
|
||||
return this.foodService.create(createFoodDto);
|
||||
@ApiOperation({ summary: 'Create category' })
|
||||
@ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto })
|
||||
@ApiBody({ type: CreateCategoryDto })
|
||||
create(@Body() dto: CreateCategoryDto) {
|
||||
return this.categoryService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@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 })
|
||||
@ApiOperation({ summary: 'List categories' })
|
||||
@ApiOkResponse({ description: 'List of categories' })
|
||||
@ApiQuery({ name: 'restId', required: false, type: String })
|
||||
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
|
||||
findAll(@Query() dto: FindFoodsDto) {
|
||||
return this.foodService.findAll(dto);
|
||||
findAll(@Query('restId') restId?: string, @Query('isActive') isActive?: string) {
|
||||
// convert isActive from query string to boolean when provided
|
||||
const isActiveBool: boolean | undefined = typeof isActive !== 'undefined' ? isActive === 'true' : undefined;
|
||||
return this.categoryService.findAll({ restId, isActive: isActiveBool });
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get a food by id' })
|
||||
@ApiParam({ name: 'id', required: true })
|
||||
@ApiOkResponse({ description: 'The food', type: CreateFoodDto })
|
||||
@ApiNotFoundResponse({ description: 'Food not found' })
|
||||
@ApiOperation({ summary: 'Get category' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto })
|
||||
@ApiNotFoundResponse({ description: 'Category not found' })
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.foodService.findOne(id);
|
||||
return this.categoryService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':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.foodService.update(id, updateFoodDto);
|
||||
@ApiOperation({ summary: 'Update category' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiBody({ type: UpdateCategoryDto })
|
||||
@ApiOkResponse({ description: 'Updated category', type: UpdateCategoryDto })
|
||||
update(@Param('id') id: string, @Body() dto: UpdateCategoryDto) {
|
||||
return this.categoryService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'Delete (soft) a food' })
|
||||
@ApiParam({ name: 'id', required: true })
|
||||
@ApiResponse({ status: 200, description: 'Food deleted' })
|
||||
@ApiOperation({ summary: 'Delete category' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiOkResponse({ description: 'Deleted' })
|
||||
remove(@Param('id') id: string) {
|
||||
return this.foodService.remove(id);
|
||||
return this.categoryService.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
|
||||
import { CategoryService } from '../providers/category.service';
|
||||
import { CreateCategoryDto } from '../dto/create-category.dto';
|
||||
import { UpdateCategoryDto } from '../dto/update-category.dto';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiCreatedResponse,
|
||||
ApiOkResponse,
|
||||
ApiNotFoundResponse,
|
||||
ApiParam,
|
||||
ApiBody,
|
||||
ApiQuery,
|
||||
} from '@nestjs/swagger';
|
||||
|
||||
@ApiTags('categories')
|
||||
@Controller('categories')
|
||||
export class CategorysController {
|
||||
constructor(private readonly categoryService: CategoryService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create category' })
|
||||
@ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto })
|
||||
@ApiBody({ type: CreateCategoryDto })
|
||||
create(@Body() dto: CreateCategoryDto) {
|
||||
return this.categoryService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List categories' })
|
||||
@ApiOkResponse({ description: 'List of categories' })
|
||||
@ApiQuery({ name: 'restId', required: false, type: String })
|
||||
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
|
||||
findAll(@Query('restId') restId?: string, @Query('isActive') isActive?: string) {
|
||||
// convert isActive from query string to boolean when provided
|
||||
const isActiveBool: boolean | undefined = typeof isActive !== 'undefined' ? isActive === 'true' : undefined;
|
||||
return this.categoryService.findAll({ restId, isActive: isActiveBool });
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get category' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto })
|
||||
@ApiNotFoundResponse({ description: 'Category not found' })
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.categoryService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Update category' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiBody({ type: UpdateCategoryDto })
|
||||
@ApiOkResponse({ description: 'Updated category', type: UpdateCategoryDto })
|
||||
update(@Param('id') id: string, @Body() dto: UpdateCategoryDto) {
|
||||
return this.categoryService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'Delete category' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiOkResponse({ description: 'Deleted' })
|
||||
remove(@Param('id') id: string) {
|
||||
return this.categoryService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { FoodService } from './providers/food.service';
|
||||
import { FoodController } from './controllers/food.controller';
|
||||
import { CategorysController } from './controllers/categorys.controller';
|
||||
import { CategoryController } from './controllers/category.controller';
|
||||
import { CategoryService } from './providers/category.service';
|
||||
import { FoodRepository } from './repositories/food.repository';
|
||||
import { CategoryRepository } from './repositories/category.repository';
|
||||
@@ -11,7 +11,7 @@ import { Food } from './entities/food.entity';
|
||||
|
||||
@Module({
|
||||
imports: [MikroOrmModule.forFeature([Food, Category])],
|
||||
controllers: [FoodController, CategorysController],
|
||||
controllers: [FoodController, CategoryController],
|
||||
providers: [FoodService, CategoryService, FoodRepository, CategoryRepository],
|
||||
exports: [FoodRepository, CategoryRepository],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user