refactor food
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user