remove food cache

This commit is contained in:
2025-12-19 22:30:40 +03:30
parent b7c8ba6862
commit af5616225e
2 changed files with 37 additions and 66 deletions
@@ -19,57 +19,36 @@ 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()
export class FoodController {
constructor(private readonly foodsService: FoodService) { }
constructor(private readonly foodsService: FoodService) {}
@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
findPublicFoodById(@Param('foodId') foodId: string, @UserId() userId?: string): Promise<any> {
const a = this.foodsService.findPublicById(foodId, userId);
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')
+26 -27
View File
@@ -22,7 +22,7 @@ export class FoodService {
private readonly restRepository: RestRepository,
private readonly em: EntityManager,
private readonly cacheService: CacheService,
) { }
) {}
async create(restId: string, createFoodDto: CreateFoodDto): Promise<Food> {
const { categoryId, ...rest } = createFoodDto;
@@ -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;
}
@@ -77,17 +77,17 @@ export class FoodService {
/**
* Public food detail (only active foods are visible).
*/
async findPublicById(foodId: string, userId?: string):Promise<any> {
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);
// }
}