toggle favorite
This commit is contained in:
@@ -50,21 +50,13 @@ export class FoodController {
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiHeader(API_HEADER_SLUG)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Set a food as favorite' })
|
||||
@ApiOperation({ summary: 'toggle a food as favorite' })
|
||||
@ApiParam({ name: 'foodId', required: true })
|
||||
setAsFavorute(@Param('foodId') foodId: string, @UserId() userId: string) {
|
||||
return this.foodsService.addFavorite(userId, foodId);
|
||||
return this.foodsService.toggleFavorite(userId, foodId);
|
||||
}
|
||||
|
||||
@Delete('public/foods/favorite/:foodId')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiHeader(API_HEADER_SLUG)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Remove a food favorite' })
|
||||
@ApiParam({ name: 'foodId', required: true })
|
||||
removeFavorite(@Param('foodId') foodId: string, @UserId() userId: string) {
|
||||
return this.foodsService.removeFavorite(userId, foodId);
|
||||
}
|
||||
|
||||
/* ---------------------------------- Admin ---------------------------------- */
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
|
||||
@@ -246,20 +246,18 @@ export class FoodService {
|
||||
// await this.invalidateRestaurantFoodsCache(restaurantSlug);
|
||||
}
|
||||
|
||||
async removeFavorite(userId: string, foodId: string) {
|
||||
return this.em.nativeDelete(Favorite, {
|
||||
user: userId,
|
||||
food: foodId,
|
||||
});
|
||||
|
||||
async toggleFavorite(userId: string, foodId: string) {
|
||||
|
||||
const favorite = await this.em.findOne(Favorite, { user: userId, food: foodId });
|
||||
if (favorite) {
|
||||
return this.em.removeAndFlush(favorite);
|
||||
}
|
||||
|
||||
async addFavorite(userId: string, foodId: string) {
|
||||
const favorite = this.em.create(Favorite, {
|
||||
const newFavorite = this.em.create(Favorite, {
|
||||
user: userId,
|
||||
food: foodId,
|
||||
});
|
||||
|
||||
await this.em.persistAndFlush(favorite);
|
||||
return this.em.persistAndFlush(newFavorite);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user