This commit is contained in:
2026-04-07 16:31:06 +03:30
parent 055ba55586
commit 0386082d5f
5 changed files with 23 additions and 46 deletions
@@ -7,11 +7,13 @@ import { RequiredEntityData } from '@mikro-orm/core';
import { Category } from '../entities/category.entity';
import { CategoryMessage, RestMessage } from 'src/common/enums/message.enum';
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
import { RestaurantsService } from 'src/modules/restaurants/providers/restaurants.service';
@Injectable()
export class CategoryService {
constructor(
private readonly categoryRepository: CategoryRepository,
private readonly restaurantsService: RestaurantsService,
private readonly em: EntityManager,
) { }
@@ -33,10 +35,8 @@ export class CategoryService {
}
async findAllByRestaurant(slug: string): Promise<Category[]> {
const restaurant = await this.em.findOne(Restaurant, { slug });
if (!restaurant || !restaurant.id) {
throw new NotFoundException(RestMessage.NOT_FOUND);
}
const restaurant = await this.restaurantsService.findBySlug(slug)
return this.categoryRepository.find(
{ restaurant: restaurant, isActive: true }, { orderBy: { order: 'ASC' } });
}
@@ -47,34 +47,22 @@ export class CategoryService {
{ orderBy: { order: 'asc' } });
}
async findOne(restId: string, id: string): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id, restaurant: { id: restId } }, { populate: ['foods'] });
async findOneOrFail(restId: string, id: string): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id, restaurant: { id: restId } });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
return {
id: cat.id,
title: cat.title,
isActive: cat.isActive,
restaurant: cat.restaurant,
avatarUrl: cat.avatarUrl,
createdAt: cat.createdAt,
updatedAt: cat.updatedAt,
foods: cat.foods.getItems().map(f => ({ id: f.id, title: f.title })),
} as unknown as Category;
return cat
}
async update(restId: string, id: string, dto: UpdateCategoryDto): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id, restaurant: { id: restId } });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
const cat = await this.findOneOrFail(restId, id)
this.em.assign(cat, dto);
await this.em.persistAndFlush(cat);
return cat;
}
async remove(restId: string, id: string): Promise<void> {
const cat = await this.categoryRepository.findOne({ id, restaurant: { id: restId } });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
const cat = await this.findOneOrFail(restId, id)
cat.deletedAt = new Date();
await this.em.persistAndFlush(cat);
}