refactor food

This commit is contained in:
2025-12-17 09:06:03 +03:30
parent 1b42a0b4b4
commit 87534e8c6f
4 changed files with 34 additions and 23 deletions
+22 -11
View File
@@ -29,7 +29,7 @@ export class FoodService {
if (!restaurant) {
throw new NotFoundException(RestMessage.NOT_FOUND);
}
const category = await this.categoryRepository.findOne({ id: categoryId });
const category = await this.categoryRepository.findOne({ id: categoryId, restaurant: { id: restId } });
if (!category) {
throw new NotFoundException(CategoryMessage.NOT_FOUND);
}
@@ -74,11 +74,21 @@ export class FoodService {
return this.foodRepository.findAllPaginated(restId, dto);
}
async findById(id: string): Promise<Food> {
const food = await this.foodRepository.findOne({ id }, { populate: ['category'] });
if (!food) {
throw new NotFoundException(FoodMessage.NOT_FOUND);
}
/**
* Public food detail (only active foods are visible).
*/
async findPublicById(id: string): Promise<Food> {
const food = await this.foodRepository.findOne({ id, isActive: true }, { populate: ['category'] });
if (!food) throw new NotFoundException(FoodMessage.NOT_FOUND);
return food;
}
/**
* Admin food detail (scoped to the authenticated restaurant).
*/
async findAdminById(restId: string, id: string): Promise<Food> {
const food = await this.foodRepository.findOne({ id, restaurant: { id: restId } }, { populate: ['category'] });
if (!food) throw new NotFoundException(FoodMessage.NOT_FOUND);
return food;
}
@@ -112,14 +122,14 @@ export class FoodService {
return foods;
}
async update(id: string, dto: Partial<CreateFoodDto>): Promise<Food> {
const food = await this.foodRepository.findOne({ id }, { populate: ['restaurant'] });
async update(restId: string, id: string, dto: Partial<CreateFoodDto>): Promise<Food> {
const food = await this.foodRepository.findOne({ id, restaurant: { id: restId } }, { populate: ['restaurant'] });
if (!food) {
throw new NotFoundException(FoodMessage.NOT_FOUND);
}
// attach new categories if provided (adds, does not attempt to preserve/remove existing ones)
if (dto.categoryId) {
const category = await this.categoryRepository.findOne({ id: dto.categoryId });
const category = await this.categoryRepository.findOne({ id: dto.categoryId, restaurant: { id: restId } });
if (!category) {
throw new NotFoundException(CategoryMessage.NOT_FOUND);
@@ -130,6 +140,7 @@ export class FoodService {
// assign other fields from DTO
const { categoryId, ...rest } = dto;
void categoryId;
this.em.assign(food, rest);
await this.em.persistAndFlush(food);
@@ -140,8 +151,8 @@ export class FoodService {
return food;
}
async remove(id: string) {
const food = await this.foodRepository.findOne({ id }, { populate: ['restaurant'] });
async remove(restId: string, id: string): Promise<void> {
const food = await this.foodRepository.findOne({ id, restaurant: { id: restId } }, { populate: ['restaurant'] });
if (!food) {
throw new NotFoundException(FoodMessage.NOT_FOUND);
}