From af5616225ef60c312523b391a77c16cb6a5dc705 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Fri, 19 Dec 2025 22:30:40 +0330 Subject: [PATCH] remove food cache --- .../foods/controllers/food.controller.ts | 50 ++++------------- src/modules/foods/providers/food.service.ts | 53 +++++++++---------- 2 files changed, 37 insertions(+), 66 deletions(-) diff --git a/src/modules/foods/controllers/food.controller.ts b/src/modules/foods/controllers/food.controller.ts index d49d2b0..77d40e9 100644 --- a/src/modules/foods/controllers/food.controller.ts +++ b/src/modules/foods/controllers/food.controller.ts @@ -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 { - const a= this.foodsService.findPublicById(foodId, userId); - return a + findPublicFoodById(@Param('foodId') foodId: string, @UserId() userId?: string): Promise { + 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,24 +58,17 @@ 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') + @Post('admin/foods') @ApiOperation({ summary: 'Create a new food' }) @ApiCreatedResponse({ description: 'The food has been successfully created.', type: CreateFoodDto }) @ApiBody({ type: CreateFoodDto }) diff --git a/src/modules/foods/providers/food.service.ts b/src/modules/foods/providers/food.service.ts index 1d1f5d8..fafa860 100644 --- a/src/modules/foods/providers/food.service.ts +++ b/src/modules/foods/providers/food.service.ts @@ -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 { 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 { + async findPublicById(foodId: string, userId?: string): Promise { 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 { - 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(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(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 { - const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`; - await this.cacheService.del(cacheKey); - } + // private async invalidateRestaurantFoodsCache(slug: string): Promise { + // const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`; + // await this.cacheService.del(cacheKey); + // } }