This commit is contained in:
2025-11-15 10:18:50 +03:30
parent 84163f6c8c
commit 58e6390ddb
3 changed files with 37 additions and 107 deletions
@@ -1,70 +1,65 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common'; import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
import { FoodService } from '../providers/food.service'; import { CategoryService } from '../providers/category.service';
import { CreateFoodDto } from '../dto/create-food.dto'; import { CreateCategoryDto } from '../dto/create-category.dto';
import { UpdateFoodDto } from '../dto/update-food.dto'; import { UpdateCategoryDto } from '../dto/update-category.dto';
import { FindFoodsDto } from '../dto/find-foods.dto';
import { import {
ApiTags, ApiTags,
ApiOperation, ApiOperation,
ApiResponse,
ApiCreatedResponse, ApiCreatedResponse,
ApiOkResponse, ApiOkResponse,
ApiNotFoundResponse, ApiNotFoundResponse,
ApiQuery,
ApiBody,
ApiParam, ApiParam,
ApiBody,
ApiQuery,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
@ApiTags('foods') @ApiTags('categories')
@Controller('foods') @Controller('categories')
export class CategoryController { export class CategoryController {
constructor(private readonly foodService: FoodService) {} constructor(private readonly categoryService: CategoryService) {}
@Post() @Post()
@ApiOperation({ summary: 'Create a new food' }) @ApiOperation({ summary: 'Create category' })
@ApiCreatedResponse({ description: 'The food has been successfully created.', type: CreateFoodDto }) @ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto })
@ApiBody({ type: CreateFoodDto }) @ApiBody({ type: CreateCategoryDto })
create(@Body() createFoodDto: CreateFoodDto) { create(@Body() dto: CreateCategoryDto) {
return this.foodService.create(createFoodDto); return this.categoryService.create(dto);
} }
@Get() @Get()
@ApiOperation({ summary: 'Get a paginated list of foods' }) @ApiOperation({ summary: 'List categories' })
@ApiOkResponse({ description: 'Paginated list of foods' }) @ApiOkResponse({ description: 'List of categories' })
@ApiQuery({ name: 'page', required: false, type: Number }) @ApiQuery({ name: 'restId', required: false, type: String })
@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 }) @ApiQuery({ name: 'isActive', required: false, type: Boolean })
findAll(@Query() dto: FindFoodsDto) { findAll(@Query('restId') restId?: string, @Query('isActive') isActive?: string) {
return this.foodService.findAll(dto); // 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') @Get(':id')
@ApiOperation({ summary: 'Get a food by id' }) @ApiOperation({ summary: 'Get category' })
@ApiParam({ name: 'id', required: true }) @ApiParam({ name: 'id' })
@ApiOkResponse({ description: 'The food', type: CreateFoodDto }) @ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto })
@ApiNotFoundResponse({ description: 'Food not found' }) @ApiNotFoundResponse({ description: 'Category not found' })
findOne(@Param('id') id: string) { findOne(@Param('id') id: string) {
return this.foodService.findOne(id); return this.categoryService.findOne(id);
} }
@Patch(':id') @Patch(':id')
@ApiOperation({ summary: 'Update a food' }) @ApiOperation({ summary: 'Update category' })
@ApiParam({ name: 'id', required: true }) @ApiParam({ name: 'id' })
@ApiBody({ type: UpdateFoodDto }) @ApiBody({ type: UpdateCategoryDto })
@ApiOkResponse({ description: 'The updated food', type: UpdateFoodDto }) @ApiOkResponse({ description: 'Updated category', type: UpdateCategoryDto })
update(@Param('id') id: string, @Body() updateFoodDto: UpdateFoodDto) { update(@Param('id') id: string, @Body() dto: UpdateCategoryDto) {
return this.foodService.update(id, updateFoodDto); return this.categoryService.update(id, dto);
} }
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete (soft) a food' }) @ApiOperation({ summary: 'Delete category' })
@ApiParam({ name: 'id', required: true }) @ApiParam({ name: 'id' })
@ApiResponse({ status: 200, description: 'Food deleted' }) @ApiOkResponse({ description: 'Deleted' })
remove(@Param('id') id: string) { 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);
}
}
+2 -2
View File
@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { FoodService } from './providers/food.service'; import { FoodService } from './providers/food.service';
import { FoodController } from './controllers/food.controller'; 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 { CategoryService } from './providers/category.service';
import { FoodRepository } from './repositories/food.repository'; import { FoodRepository } from './repositories/food.repository';
import { CategoryRepository } from './repositories/category.repository'; import { CategoryRepository } from './repositories/category.repository';
@@ -11,7 +11,7 @@ import { Food } from './entities/food.entity';
@Module({ @Module({
imports: [MikroOrmModule.forFeature([Food, Category])], imports: [MikroOrmModule.forFeature([Food, Category])],
controllers: [FoodController, CategorysController], controllers: [FoodController, CategoryController],
providers: [FoodService, CategoryService, FoodRepository, CategoryRepository], providers: [FoodService, CategoryService, FoodRepository, CategoryRepository],
exports: [FoodRepository, CategoryRepository], exports: [FoodRepository, CategoryRepository],
}) })