Files
dmenu-api/src/modules/foods/controllers/category.controller.ts
T
2025-11-19 00:44:11 +03:30

79 lines
2.9 KiB
TypeScript

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,
ApiBearerAuth,
} from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { UseGuards } from '@nestjs/common';
import { RestId } from 'src/common/decorators';
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@ApiTags('admin/categories')
@Controller('admin/categories')
export class CategoryController {
constructor(private readonly categoryService: CategoryService) {}
@ApiOperation({ summary: 'Create category' })
@ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto })
@ApiBody({ type: CreateCategoryDto })
@Post()
create(@Body() dto: CreateCategoryDto) {
return this.categoryService.create(dto);
}
@ApiOperation({ summary: 'my restaurant categories' })
@ApiOkResponse({ description: 'List of categories' })
@ApiQuery({ name: 'restId', required: false, type: String })
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
@Get()
findAll(@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' })
@ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto })
@ApiNotFoundResponse({ description: 'Category not found' })
findOne(@Param() id: string, @RestId() restId: string) {
return this.categoryService.findOne(restId, 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, @RestId() restId: string) {
return this.categoryService.update(restId, id, dto);
}
@Delete(':id')
@ApiOperation({ summary: 'Delete category' })
@ApiParam({ name: 'id' })
@ApiOkResponse({ description: 'Deleted' })
remove(@Param('id') id: string, @RestId() restId: string) {
return this.categoryService.remove(restId, id);
}
// @Get('restaurant/:restId')
// @ApiOperation({ summary: 'Restaurant categories for user' })
// @ApiOkResponse({ description: 'List of categories' })
// @ApiParam({ name: 'restId', required: true, type: String })
// findAllForRestaurant(@Param('restId') restId: string) {
// return this.categoryService.findRestaurantCategories(restId);
// }
}