category
This commit is contained in:
@@ -6,7 +6,7 @@ import { CategoryRepository } from '../repositories/category.repository';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { Food } from '../entities/food.entity';
|
||||
import { FoodMessage, RestMessage } from 'src/common/enums/message.enum';
|
||||
import { CategoryMessage, FoodMessage, RestMessage } from 'src/common/enums/message.enum';
|
||||
import { RestRepository } from 'src/modules/restaurants/repositories/rest.repository';
|
||||
|
||||
@Injectable()
|
||||
@@ -19,11 +19,16 @@ export class FoodService {
|
||||
) {}
|
||||
|
||||
async create(restId: string, createFoodDto: CreateFoodDto): Promise<Food> {
|
||||
const { categoryIds, ...rest } = createFoodDto;
|
||||
const { categoryId, ...rest } = createFoodDto;
|
||||
const restaurant = await this.restRepository.findOne({ id: restId });
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
const category = await this.categoryRepository.findOne({ id: categoryId });
|
||||
if (!category) {
|
||||
throw new NotFoundException(CategoryMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
// prepare data with defaults for required fields so repository.create typing is satisfied
|
||||
const data: RequiredEntityData<Food> = {
|
||||
// boolean/day flags
|
||||
@@ -49,6 +54,7 @@ export class FoodService {
|
||||
prepareTime: rest.prepareTime ?? 0,
|
||||
images: rest.images ?? undefined,
|
||||
restaurant: restaurant,
|
||||
category: category
|
||||
};
|
||||
|
||||
const food = this.foodRepository.create(data);
|
||||
@@ -56,12 +62,6 @@ export class FoodService {
|
||||
throw new Error('Failed to create food entity');
|
||||
}
|
||||
|
||||
// attach categories if provided
|
||||
if (categoryIds && categoryIds.length) {
|
||||
const categories = await this.categoryRepository.find({ id: { $in: categoryIds } });
|
||||
categories.forEach(cat => food.categories.add(cat));
|
||||
}
|
||||
|
||||
await this.em.persistAndFlush(food);
|
||||
return food;
|
||||
}
|
||||
@@ -70,40 +70,36 @@ 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);
|
||||
}
|
||||
return food;
|
||||
}
|
||||
|
||||
async findAllByRestaurant(slug: string): Promise<Food[]> {
|
||||
const restaurant = await this.restRepository.findOne({ slug });
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
return this.foodRepository.find({ restaurant: { slug } });
|
||||
}
|
||||
|
||||
findOne(id: string) {
|
||||
return this.foodRepository.findOne({ id }, { populate: ['categories'] });
|
||||
return this.foodRepository.find({ restaurant: { slug }, isActive: true }, { populate: ['category'] });
|
||||
}
|
||||
|
||||
async update(id: string, dto: Partial<CreateFoodDto>): Promise<Food> {
|
||||
const food = await this.foodRepository.findOne({ id });
|
||||
|
||||
if (!food) {
|
||||
throw new NotFoundException(FoodMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
const { categoryIds, ...rest } = dto;
|
||||
|
||||
// assign scalar fields
|
||||
this.em.assign(food, rest as Partial<Food>);
|
||||
|
||||
// attach new categories if provided (adds, does not attempt to preserve/remove existing ones)
|
||||
if (Array.isArray(categoryIds) && categoryIds.length) {
|
||||
const categories = await this.categoryRepository.find({ id: { $in: categoryIds } });
|
||||
if (dto.categoryId) {
|
||||
const category = await this.categoryRepository.findOne({ id: dto.categoryId });
|
||||
|
||||
food.categories.removeAll();
|
||||
if (!category) {
|
||||
throw new NotFoundException(CategoryMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
categories.forEach(cat => {
|
||||
// Collection.add will ignore duplicates
|
||||
food.categories.add(cat);
|
||||
});
|
||||
this.em.assign(food, { category: category });
|
||||
}
|
||||
|
||||
await this.em.persistAndFlush(food);
|
||||
|
||||
Reference in New Issue
Block a user