toggle favorite
This commit is contained in:
@@ -24,7 +24,7 @@ import { API_HEADER_SLUG } from 'src/common/constants';
|
|||||||
@ApiTags('foods')
|
@ApiTags('foods')
|
||||||
@Controller()
|
@Controller()
|
||||||
export class FoodController {
|
export class FoodController {
|
||||||
constructor(private readonly foodsService: FoodService) {}
|
constructor(private readonly foodsService: FoodService) { }
|
||||||
|
|
||||||
@Get('public/foods/restaurant/:slug')
|
@Get('public/foods/restaurant/:slug')
|
||||||
@ApiOperation({ summary: 'Get all foods by restaurant slug' })
|
@ApiOperation({ summary: 'Get all foods by restaurant slug' })
|
||||||
@@ -50,21 +50,13 @@ export class FoodController {
|
|||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiHeader(API_HEADER_SLUG)
|
@ApiHeader(API_HEADER_SLUG)
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ApiOperation({ summary: 'Set a food as favorite' })
|
@ApiOperation({ summary: 'toggle a food as favorite' })
|
||||||
@ApiParam({ name: 'foodId', required: true })
|
@ApiParam({ name: 'foodId', required: true })
|
||||||
setAsFavorute(@Param('foodId') foodId: string, @UserId() userId: string) {
|
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 ---------------------------------- */
|
/* ---------------------------------- Admin ---------------------------------- */
|
||||||
@UseGuards(AdminAuthGuard)
|
@UseGuards(AdminAuthGuard)
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@@ -89,7 +81,7 @@ export class FoodController {
|
|||||||
@ApiQuery({ name: 'categoryId', required: false, type: String })
|
@ApiQuery({ name: 'categoryId', required: false, type: String })
|
||||||
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
|
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
|
||||||
async findAll(@Query() dto: FindFoodsDto, @RestId() restId: string) {
|
async findAll(@Query() dto: FindFoodsDto, @RestId() restId: string) {
|
||||||
const result =await this.foodsService.findAll(restId, dto);
|
const result = await this.foodsService.findAll(restId, dto);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export class FoodService {
|
|||||||
private readonly restRepository: RestRepository,
|
private readonly restRepository: RestRepository,
|
||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
private readonly cacheService: CacheService,
|
private readonly cacheService: CacheService,
|
||||||
) {}
|
) { }
|
||||||
|
|
||||||
async create(restId: string, createFoodDto: CreateFoodDto) {
|
async create(restId: string, createFoodDto: CreateFoodDto) {
|
||||||
const { categoryId, dailyStock = 0, ...rest } = createFoodDto;
|
const { categoryId, dailyStock = 0, ...rest } = createFoodDto;
|
||||||
@@ -246,20 +246,18 @@ export class FoodService {
|
|||||||
// await this.invalidateRestaurantFoodsCache(restaurantSlug);
|
// 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,
|
user: userId,
|
||||||
food: foodId,
|
food: foodId,
|
||||||
});
|
});
|
||||||
}
|
return this.em.persistAndFlush(newFavorite);
|
||||||
|
|
||||||
async addFavorite(userId: string, foodId: string) {
|
|
||||||
const favorite = this.em.create(Favorite, {
|
|
||||||
user: userId,
|
|
||||||
food: foodId,
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.em.persistAndFlush(favorite);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user