remove food cache
This commit is contained in:
@@ -19,6 +19,7 @@ import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
import { RestId, UserId } from 'src/common/decorators';
|
||||
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
|
||||
import { OptionalAuthGuard } from 'src/modules/auth/guards/optinalAuth.guard';
|
||||
import { API_HEADER_SLUG } from 'src/common/constants';
|
||||
|
||||
@ApiTags('foods')
|
||||
@Controller()
|
||||
@@ -27,49 +28,27 @@ export class FoodController {
|
||||
|
||||
@Get('public/foods/restaurant/:slug')
|
||||
@ApiOperation({ summary: 'Get all foods by restaurant slug' })
|
||||
@ApiHeader({
|
||||
name: 'X-Slug',
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'string',
|
||||
default: 'zhivan',
|
||||
},
|
||||
})
|
||||
@ApiHeader(API_HEADER_SLUG)
|
||||
@ApiParam({ name: 'slug', required: true, description: 'Restaurant Slug' })
|
||||
@ApiOkResponse({ description: 'List of all foods for the restaurant' })
|
||||
findAllByRestaurant(@Param('slug') slug: string) {
|
||||
return this.foodsService.findAllByRestaurant(slug);
|
||||
}
|
||||
|
||||
|
||||
@Get('public/foods/:foodId')
|
||||
@UseGuards(OptionalAuthGuard)
|
||||
@ApiHeader({
|
||||
name: 'X-Slug',
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'string',
|
||||
default: 'zhivan',
|
||||
},
|
||||
})
|
||||
@ApiHeader(API_HEADER_SLUG)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Get a food by id' })
|
||||
@ApiParam({ name: 'foodId', required: true })
|
||||
findPublicFoodById(@Param('foodId') foodId: string, @UserId() userId?: string): Promise<any> {
|
||||
const a = this.foodsService.findPublicById(foodId, userId);
|
||||
return a
|
||||
return a;
|
||||
}
|
||||
|
||||
@Post('public/foods/favorite/:foodId')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiHeader({
|
||||
name: 'X-Slug',
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'string',
|
||||
default: 'zhivan',
|
||||
},
|
||||
})
|
||||
@ApiHeader(API_HEADER_SLUG)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Set a food as favorite' })
|
||||
@ApiParam({ name: 'foodId', required: true })
|
||||
@@ -79,21 +58,14 @@ export class FoodController {
|
||||
|
||||
@Delete('public/foods/favorite/:foodId')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiHeader({
|
||||
name: 'X-Slug',
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'string',
|
||||
default: 'zhivan',
|
||||
},
|
||||
})
|
||||
@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()
|
||||
@Post('admin/foods')
|
||||
|
||||
@@ -65,7 +65,7 @@ export class FoodService {
|
||||
await this.em.persistAndFlush(food);
|
||||
|
||||
// Invalidate cache for the restaurant
|
||||
await this.invalidateRestaurantFoodsCache(restaurant.slug);
|
||||
// await this.invalidateRestaurantFoodsCache(restaurant.slug);
|
||||
|
||||
return food;
|
||||
}
|
||||
@@ -80,14 +80,14 @@ export class FoodService {
|
||||
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);
|
||||
let isFavorite = false
|
||||
let isFavorite = false;
|
||||
if (userId) {
|
||||
isFavorite = await this.em.count(Favorite, { user: { id: userId }, food: { id: foodId } }) > 0
|
||||
isFavorite = (await this.em.count(Favorite, { user: { id: userId }, food: { id: foodId } })) > 0;
|
||||
}
|
||||
return {
|
||||
...food,
|
||||
isFavorite,
|
||||
};;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,20 +101,20 @@ export class FoodService {
|
||||
}
|
||||
|
||||
async findAllByRestaurant(slug: string): Promise<Food[]> {
|
||||
const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||
// const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||
|
||||
// Try to get from cache first
|
||||
const cachedFoods = await this.cacheService.get<string>(cacheKey);
|
||||
if (cachedFoods) {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(cachedFoods);
|
||||
if (Array.isArray(parsed)) {
|
||||
return parsed as Food[];
|
||||
}
|
||||
} catch {
|
||||
// If parsing fails, continue to fetch from DB
|
||||
}
|
||||
}
|
||||
// const cachedFoods = await this.cacheService.get<string>(cacheKey);
|
||||
// if (cachedFoods) {
|
||||
// try {
|
||||
// const parsed: unknown = JSON.parse(cachedFoods);
|
||||
// if (Array.isArray(parsed)) {
|
||||
// return parsed as Food[];
|
||||
// }
|
||||
// } catch {
|
||||
// // If parsing fails, continue to fetch from DB
|
||||
// }
|
||||
// }
|
||||
|
||||
// If not in cache, fetch from database
|
||||
const restaurant = await this.restRepository.findOne({ slug });
|
||||
@@ -125,7 +125,7 @@ export class FoodService {
|
||||
const foods = await this.foodRepository.find({ restaurant: { slug }, isActive: true }, { populate: ['category'] });
|
||||
|
||||
// Cache the result
|
||||
await this.cacheService.set(cacheKey, JSON.stringify(foods), this.FOODS_CACHE_TTL);
|
||||
// await this.cacheService.set(cacheKey, JSON.stringify(foods), this.FOODS_CACHE_TTL);
|
||||
|
||||
return foods;
|
||||
}
|
||||
@@ -154,7 +154,7 @@ export class FoodService {
|
||||
await this.em.persistAndFlush(food);
|
||||
|
||||
// Invalidate cache for the restaurant
|
||||
await this.invalidateRestaurantFoodsCache(food.restaurant.slug);
|
||||
// await this.invalidateRestaurantFoodsCache(food.restaurant.slug);
|
||||
|
||||
return food;
|
||||
}
|
||||
@@ -165,12 +165,12 @@ export class FoodService {
|
||||
throw new NotFoundException(FoodMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
const restaurantSlug = food.restaurant.slug;
|
||||
// const restaurantSlug = food.restaurant.slug;
|
||||
food.deletedAt = new Date();
|
||||
await this.em.persistAndFlush(food);
|
||||
|
||||
// Invalidate cache for the restaurant
|
||||
await this.invalidateRestaurantFoodsCache(restaurantSlug);
|
||||
// await this.invalidateRestaurantFoodsCache(restaurantSlug);
|
||||
}
|
||||
|
||||
async removeFavorite(userId: string, foodId: string) {
|
||||
@@ -189,12 +189,11 @@ export class FoodService {
|
||||
await this.em.persistAndFlush(favorite);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Invalidate cache for restaurant foods
|
||||
*/
|
||||
private async invalidateRestaurantFoodsCache(slug: string): Promise<void> {
|
||||
const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||
await this.cacheService.del(cacheKey);
|
||||
}
|
||||
// private async invalidateRestaurantFoodsCache(slug: string): Promise<void> {
|
||||
// const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||
// await this.cacheService.del(cacheKey);
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user