This commit is contained in:
2025-12-18 12:39:25 +03:30
parent ec56e03907
commit 60c0bfb8d2
6 changed files with 108 additions and 11 deletions
+29 -3
View File
@@ -9,6 +9,7 @@ import { Food } from '../entities/food.entity';
import { CategoryMessage, FoodMessage, RestMessage } from 'src/common/enums/message.enum';
import { RestRepository } from 'src/modules/restaurants/repositories/rest.repository';
import { CacheService } from '../../utils/cache.service';
import { Favorite } from '../entities/favorite.entity';
@Injectable()
export class FoodService {
@@ -76,10 +77,17 @@ export class FoodService {
/**
* 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'] });
async findPublicById(foodId: string, userId?: string):Promise<any> {
const food = await this.foodRepository.findOne({ id: foodId, isActive: true }, { populate: ['category'] });
if (!food) throw new NotFoundException(FoodMessage.NOT_FOUND);
return food;
let isFavorite = false
if (userId) {
isFavorite = await this.em.count(Favorite, { user: { id: userId }, food: { id: foodId } }) > 0
}
return {
...food,
isFavorite,
};;
}
/**
@@ -87,6 +95,7 @@ export class FoodService {
*/
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;
}
@@ -164,6 +173,23 @@ export class FoodService {
await this.invalidateRestaurantFoodsCache(restaurantSlug);
}
async removeFavorite(userId: string, foodId: string) {
return this.em.nativeDelete(Favorite, {
user: userId,
food: foodId,
});
}
async addFavorite(userId: string, foodId: string) {
const favorite = this.em.create(Favorite, {
user: userId,
food: foodId,
});
await this.em.persistAndFlush(favorite);
}
/**
* Invalidate cache for restaurant foods
*/