This commit is contained in:
2025-11-25 09:58:13 +03:30
parent fdbc3c9dbe
commit a5992d8e29
4 changed files with 36 additions and 27 deletions
@@ -19,15 +19,25 @@ import { RestId } from 'src/common/decorators';
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@ApiTags('admin/categories')
@Controller('admin/categories')
@ApiTags('category')
@Controller()
export class CategoryController {
constructor(private readonly categoryService: CategoryService) {}
@Get('public/categories/restaurant/:slug')
@ApiOperation({ summary: 'Get all categories by restaurant slug' })
@ApiParam({ name: 'slug', required: true, description: 'Restaurant Slug' })
@ApiOkResponse({ description: 'List of all categories for the restaurant' })
findAllByRestaurant(@Param('slug') slug: string) {
return this.categoryService.findAllByRestaurant(slug);
}
@ApiOperation({ summary: 'Create category' })
@ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto })
@ApiBody({ type: CreateCategoryDto })
@Post()
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Post('admin/categories')
create(@Body() dto: CreateCategoryDto) {
return this.categoryService.create(dto);
}
@@ -36,14 +46,18 @@ export class CategoryController {
@ApiOkResponse({ description: 'List of categories' })
@ApiQuery({ name: 'restId', required: false, type: String })
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
@Get()
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/categories')
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')
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/categories/:id')
@ApiOperation({ summary: 'Get category' })
@ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto })
@ApiNotFoundResponse({ description: 'Category not found' })
@@ -52,7 +66,9 @@ export class CategoryController {
return this.categoryService.findOne(restId, id);
}
@Patch(':id')
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Patch('admin/categories/:id')
@ApiOperation({ summary: 'Update category' })
@ApiParam({ name: 'id' })
@ApiBody({ type: UpdateCategoryDto })
@@ -61,7 +77,9 @@ export class CategoryController {
return this.categoryService.update(restId, id, dto);
}
@Delete(':id')
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Delete('admin/categories/:id')
@ApiOperation({ summary: 'Delete category' })
@ApiParam({ name: 'id' })
@ApiOkResponse({ description: 'Deleted' })
@@ -1,17 +0,0 @@
import { Controller, Get, Param } from '@nestjs/common';
import { CategoryService } from '../providers/category.service';
import { ApiTags, ApiOperation, ApiOkResponse, ApiParam } from '@nestjs/swagger';
@ApiTags('public/categories')
@Controller('public/categories')
export class PublicCategoryController {
constructor(private readonly categoryService: CategoryService) {}
@Get('restaurant/:restId')
@ApiOperation({ summary: 'Get all categories by restaurant id' })
@ApiParam({ name: 'restId', required: true, description: 'Restaurant ID' })
@ApiOkResponse({ description: 'List of all categories for the restaurant' })
findAllByRestaurant(@Param('restId') restId: string) {
return this.categoryService.findAll({ restId });
}
}
+1 -2
View File
@@ -2,7 +2,6 @@ import { Module } from '@nestjs/common';
import { FoodService } from './providers/food.service';
import { FoodController } from './controllers/food.controller';
import { CategoryController } from './controllers/category.controller';
import { PublicCategoryController } from './controllers/public-category.controller';
import { CategoryService } from './providers/category.service';
import { FoodRepository } from './repositories/food.repository';
import { CategoryRepository } from './repositories/category.repository';
@@ -15,7 +14,7 @@ import { JwtModule } from '@nestjs/jwt';
@Module({
imports: [MikroOrmModule.forFeature([Food, Category]), RestaurantsModule, AuthModule, JwtModule],
controllers: [FoodController, CategoryController, PublicCategoryController],
controllers: [FoodController, CategoryController],
providers: [FoodService, CategoryService, FoodRepository, CategoryRepository],
exports: [FoodRepository, CategoryRepository],
})
@@ -5,7 +5,8 @@ import { CategoryRepository } from '../repositories/category.repository';
import { EntityManager } from '@mikro-orm/postgresql';
import { RequiredEntityData, FilterQuery } from '@mikro-orm/core';
import { Category } from '../entities/category.entity';
import { CategoryMessage } from 'src/common/enums/message.enum';
import { CategoryMessage, RestMessage } from 'src/common/enums/message.enum';
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
@Injectable()
export class CategoryService {
@@ -28,6 +29,14 @@ export class CategoryService {
return category;
}
async findAllByRestaurant(slug: string): Promise<Category[]> {
const restaurant = await this.em.findOne(Restaurant, { slug });
if (!restaurant || !restaurant.id) {
throw new NotFoundException(RestMessage.NOT_FOUND);
}
return this.categoryRepository.find({ restId: restaurant.id.toString() });
}
async findAll(opts?: { restId?: string; isActive?: boolean }): Promise<Category[]> {
const where: FilterQuery<Category> = {};
if (opts?.restId) where.restId = opts.restId;