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) @UseGuards(AdminAuthGuard)
@ApiBearerAuth() @ApiBearerAuth()
@ApiTags('admin/categories') @ApiTags('category')
@Controller('admin/categories') @Controller()
export class CategoryController { export class CategoryController {
constructor(private readonly categoryService: CategoryService) {} 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' }) @ApiOperation({ summary: 'Create category' })
@ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto }) @ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto })
@ApiBody({ type: CreateCategoryDto }) @ApiBody({ type: CreateCategoryDto })
@Post() @UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Post('admin/categories')
create(@Body() dto: CreateCategoryDto) { create(@Body() dto: CreateCategoryDto) {
return this.categoryService.create(dto); return this.categoryService.create(dto);
} }
@@ -36,14 +46,18 @@ export class CategoryController {
@ApiOkResponse({ description: 'List of categories' }) @ApiOkResponse({ description: 'List of categories' })
@ApiQuery({ name: 'restId', required: false, type: String }) @ApiQuery({ name: 'restId', required: false, type: String })
@ApiQuery({ name: 'isActive', required: false, type: Boolean }) @ApiQuery({ name: 'isActive', required: false, type: Boolean })
@Get() @UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/categories')
findAll(@RestId() restId: string, @Query('isActive') isActive?: string) { findAll(@RestId() restId: string, @Query('isActive') isActive?: string) {
// convert isActive from query string to boolean when provided // convert isActive from query string to boolean when provided
const isActiveBool: boolean | undefined = typeof isActive !== 'undefined' ? isActive === 'true' : undefined; const isActiveBool: boolean | undefined = typeof isActive !== 'undefined' ? isActive === 'true' : undefined;
return this.categoryService.findAll({ restId, isActive: isActiveBool }); return this.categoryService.findAll({ restId, isActive: isActiveBool });
} }
@Get(':id') @UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/categories/:id')
@ApiOperation({ summary: 'Get category' }) @ApiOperation({ summary: 'Get category' })
@ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto }) @ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto })
@ApiNotFoundResponse({ description: 'Category not found' }) @ApiNotFoundResponse({ description: 'Category not found' })
@@ -52,7 +66,9 @@ export class CategoryController {
return this.categoryService.findOne(restId, id); return this.categoryService.findOne(restId, id);
} }
@Patch(':id') @UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Patch('admin/categories/:id')
@ApiOperation({ summary: 'Update category' }) @ApiOperation({ summary: 'Update category' })
@ApiParam({ name: 'id' }) @ApiParam({ name: 'id' })
@ApiBody({ type: UpdateCategoryDto }) @ApiBody({ type: UpdateCategoryDto })
@@ -61,7 +77,9 @@ export class CategoryController {
return this.categoryService.update(restId, id, dto); return this.categoryService.update(restId, id, dto);
} }
@Delete(':id') @UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Delete('admin/categories/:id')
@ApiOperation({ summary: 'Delete category' }) @ApiOperation({ summary: 'Delete category' })
@ApiParam({ name: 'id' }) @ApiParam({ name: 'id' })
@ApiOkResponse({ description: 'Deleted' }) @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 { FoodService } from './providers/food.service';
import { FoodController } from './controllers/food.controller'; import { FoodController } from './controllers/food.controller';
import { CategoryController } from './controllers/category.controller'; import { CategoryController } from './controllers/category.controller';
import { PublicCategoryController } from './controllers/public-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';
@@ -15,7 +14,7 @@ import { JwtModule } from '@nestjs/jwt';
@Module({ @Module({
imports: [MikroOrmModule.forFeature([Food, Category]), RestaurantsModule, AuthModule, JwtModule], imports: [MikroOrmModule.forFeature([Food, Category]), RestaurantsModule, AuthModule, JwtModule],
controllers: [FoodController, CategoryController, PublicCategoryController], controllers: [FoodController, CategoryController],
providers: [FoodService, CategoryService, FoodRepository, CategoryRepository], providers: [FoodService, CategoryService, FoodRepository, CategoryRepository],
exports: [FoodRepository, CategoryRepository], exports: [FoodRepository, CategoryRepository],
}) })
@@ -5,7 +5,8 @@ import { CategoryRepository } from '../repositories/category.repository';
import { EntityManager } from '@mikro-orm/postgresql'; import { EntityManager } from '@mikro-orm/postgresql';
import { RequiredEntityData, FilterQuery } from '@mikro-orm/core'; import { RequiredEntityData, FilterQuery } from '@mikro-orm/core';
import { Category } from '../entities/category.entity'; 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() @Injectable()
export class CategoryService { export class CategoryService {
@@ -28,6 +29,14 @@ export class CategoryService {
return category; 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[]> { async findAll(opts?: { restId?: string; isActive?: boolean }): Promise<Category[]> {
const where: FilterQuery<Category> = {}; const where: FilterQuery<Category> = {};
if (opts?.restId) where.restId = opts.restId; if (opts?.restId) where.restId = opts.restId;