From 58e6390ddbb7568c4df340fb5f0c8aeb73b8a1da Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 15 Nov 2025 10:18:50 +0330 Subject: [PATCH] food --- .../foods/controllers/category.controller.ts | 75 +++++++++---------- .../foods/controllers/categorys.controller.ts | 65 ---------------- src/modules/foods/food.module.ts | 4 +- 3 files changed, 37 insertions(+), 107 deletions(-) delete mode 100644 src/modules/foods/controllers/categorys.controller.ts diff --git a/src/modules/foods/controllers/category.controller.ts b/src/modules/foods/controllers/category.controller.ts index d5cb952..9d2871f 100644 --- a/src/modules/foods/controllers/category.controller.ts +++ b/src/modules/foods/controllers/category.controller.ts @@ -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); } } diff --git a/src/modules/foods/controllers/categorys.controller.ts b/src/modules/foods/controllers/categorys.controller.ts deleted file mode 100644 index d5c030c..0000000 --- a/src/modules/foods/controllers/categorys.controller.ts +++ /dev/null @@ -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); - } -} diff --git a/src/modules/foods/food.module.ts b/src/modules/foods/food.module.ts index efe765a..acaa49c 100644 --- a/src/modules/foods/food.module.ts +++ b/src/modules/foods/food.module.ts @@ -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], })