toggle favorite

This commit is contained in:
2025-12-22 09:56:09 +03:30
parent 2fcb7521ad
commit 7205319a81
2 changed files with 15 additions and 25 deletions
+10 -12
View File
@@ -24,7 +24,7 @@ export class FoodService {
private readonly restRepository: RestRepository,
private readonly em: EntityManager,
private readonly cacheService: CacheService,
) {}
) { }
async create(restId: string, createFoodDto: CreateFoodDto) {
const { categoryId, dailyStock = 0, ...rest } = createFoodDto;
@@ -246,20 +246,18 @@ export class FoodService {
// await this.invalidateRestaurantFoodsCache(restaurantSlug);
}
async removeFavorite(userId: string, foodId: string) {
return this.em.nativeDelete(Favorite, {
async toggleFavorite(userId: string, foodId: string) {
const favorite = await this.em.findOne(Favorite, { user: userId, food: foodId });
if (favorite) {
return this.em.removeAndFlush(favorite);
}
const newFavorite = this.em.create(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);
return this.em.persistAndFlush(newFavorite);
}
/**