From ef598a4c288b04d1d1b612806e01636fbe34f211 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 20 Jun 2026 20:24:12 +0330 Subject: [PATCH] background --- .../controllers/restaurants.controller.ts | 20 ++++++++++++++++ .../dto/update-restaurant-bg.dto.ts | 24 +++++++++++++++++++ .../restaurants/entities/background.entity.ts | 9 +++++++ .../restaurants/entities/restaurant.entity.ts | 18 +++++++++++--- .../providers/restaurants.service.ts | 24 +++++++++++++++++-- .../repositories/background.repository.ts | 10 ++++++++ src/modules/restaurants/restaurants.module.ts | 7 ++++-- 7 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 src/modules/restaurants/dto/update-restaurant-bg.dto.ts create mode 100644 src/modules/restaurants/entities/background.entity.ts create mode 100644 src/modules/restaurants/repositories/background.repository.ts diff --git a/src/modules/restaurants/controllers/restaurants.controller.ts b/src/modules/restaurants/controllers/restaurants.controller.ts index ad223a4..9ced16f 100644 --- a/src/modules/restaurants/controllers/restaurants.controller.ts +++ b/src/modules/restaurants/controllers/restaurants.controller.ts @@ -21,6 +21,7 @@ import { Permissions } from 'src/common/decorators/permissions.decorator'; import { Permission } from 'src/common/enums/permission.enum'; import { CacheResponse } from 'src/common/decorators/cache-response.decorator'; import { CacheKeyPrefixes } from 'src/common/constants/cache-keys.constant'; +import { UpdateRestaurantBgDto } from '../dto/update-restaurant-bg.dto'; @ApiTags('restaurants') @Controller() @@ -67,6 +68,25 @@ export class RestaurantsController { return this.restaurantsService.remove(id); } + @UseGuards(AdminAuthGuard) + @ApiBearerAuth() + @Permissions(Permission.UPDATE_RESTAURANT) + @ApiOperation({ summary: 'Update restaurant background' }) + @ApiBody({ type: UpdateRestaurantBgDto }) + @Patch('admin/restaurants/my-restaurant/background') + updateMyRestaurantBg(@RestId() restId: string, @Body() dto: UpdateRestaurantBgDto) { + return this.restaurantsService.updateBackground(restId, dto); + } + + @UseGuards(AdminAuthGuard) + @ApiBearerAuth() + @Permissions(Permission.UPDATE_RESTAURANT) + @ApiOperation({ summary: 'Get backgrounds' }) + @Get('admin/restaurants/background') + getBackgrounds() { + return this.restaurantsService.findAllBackgrounds(); + } + /** Super Admin Endpoints */ @UseGuards(SuperAdminAuthGuard) @ApiBearerAuth() diff --git a/src/modules/restaurants/dto/update-restaurant-bg.dto.ts b/src/modules/restaurants/dto/update-restaurant-bg.dto.ts new file mode 100644 index 0000000..c095aca --- /dev/null +++ b/src/modules/restaurants/dto/update-restaurant-bg.dto.ts @@ -0,0 +1,24 @@ +import { ApiPropertyOptional } from '@nestjs/swagger'; +import { IsOptional, IsString } from 'class-validator'; + +export class UpdateRestaurantBgDto { + @ApiPropertyOptional({ example: 'https://cdn.example.com/backgrounds/wood.jpg', description: 'آدرس تصویر پس‌زمینه' }) + @IsOptional() + @IsString() + bgUrl?: string; + + @ApiPropertyOptional({ example: '0.8', description: 'شفافیت پس‌زمینه' }) + @IsOptional() + @IsString() + bgOpacity?: string; + + @ApiPropertyOptional({ example: '4px', description: 'میزان بلور پس‌زمینه' }) + @IsOptional() + @IsString() + bgBlur?: string; + + @ApiPropertyOptional({ example: 'rgba(0, 0, 0, 0.4)', description: 'رنگ لایه روی پس‌زمینه' }) + @IsOptional() + @IsString() + bgOverlay?: string; +} diff --git a/src/modules/restaurants/entities/background.entity.ts b/src/modules/restaurants/entities/background.entity.ts new file mode 100644 index 0000000..58ee7c2 --- /dev/null +++ b/src/modules/restaurants/entities/background.entity.ts @@ -0,0 +1,9 @@ +import { Entity, Property } from '@mikro-orm/core'; +import { BaseEntity } from '../../../common/entities/base.entity'; + +@Entity({ tableName: 'backgrounds' }) +export class Background extends BaseEntity { + @Property() + url: string; + +} diff --git a/src/modules/restaurants/entities/restaurant.entity.ts b/src/modules/restaurants/entities/restaurant.entity.ts index c0f7c62..150e4ce 100644 --- a/src/modules/restaurants/entities/restaurant.entity.ts +++ b/src/modules/restaurants/entities/restaurant.entity.ts @@ -100,13 +100,25 @@ export class Restaurant extends BaseEntity { @Enum(() => PlanEnum) plan: PlanEnum = PlanEnum.Base; - @Property({unique: true,nullable: true}) + @Property({ unique: true, nullable: true }) subscriptionId?: string; - @Property({nullable: true}) + @Property({ nullable: true }) subscriptionEndDate?: Date; - @Property({nullable: true}) + @Property({ nullable: true }) subscriptionStartDate?: Date; + @Property({ nullable: true }) + bgUrl?: string; + + @Property({ nullable: true }) + bgOpacity?: string; + + @Property({ nullable: true }) + bgBlur?: string; + + @Property({ nullable: true }) + bgOverlay?: string; + } diff --git a/src/modules/restaurants/providers/restaurants.service.ts b/src/modules/restaurants/providers/restaurants.service.ts index b44dc04..5d78169 100644 --- a/src/modules/restaurants/providers/restaurants.service.ts +++ b/src/modules/restaurants/providers/restaurants.service.ts @@ -1,6 +1,7 @@ import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { CreateRestaurantDto } from '../dto/create-restaurant.dto'; import { UpdateRestaurantDto } from '../dto/update-restaurant.dto'; +import { UpdateRestaurantBgDto } from '../dto/update-restaurant-bg.dto'; import { UpgradeSubscriptionDto } from '../dto/upgrade-subscription.dto'; import { Restaurant } from '../entities/restaurant.entity'; import { EntityManager } from '@mikro-orm/postgresql'; @@ -29,6 +30,7 @@ import { SmsLog } from '../../notifications/entities/smsLogs.entity'; import { Notification } from '../../notifications/entities/notification.entity'; import { CacheService } from 'src/modules/utils/cache.service'; import { CacheKeys } from 'src/common/constants/cache-keys.constant'; +import { BackgroundRepository } from '../repositories/background.repository'; @Injectable() @@ -36,6 +38,7 @@ export class RestaurantsService { constructor( private readonly em: EntityManager, private readonly restRepository: RestRepository, + private readonly backgroundRepository: BackgroundRepository, private readonly cacheService: CacheService, ) { } @@ -142,8 +145,8 @@ export class RestaurantsService { } async findOneBySubscriptionId(subscriptionId: string): Promise { - const restaurant = await this.restRepository.findOne({ subscriptionId }); - if (!restaurant) { + const restaurant = await this.restRepository.findOne({ subscriptionId }); + if (!restaurant) { throw new NotFoundException(RestMessage.NOT_FOUND); } return restaurant; @@ -294,4 +297,21 @@ export class RestaurantsService { }); } + findAllBackgrounds() { + return this.backgroundRepository.findAll(); + } + + async updateBackground(id: string, dto: UpdateRestaurantBgDto): Promise { + const restaurant = await this.restRepository.findOne({ id }); + + if (!restaurant) { + throw new NotFoundException(RestMessage.NOT_FOUND); + } + + this.restRepository.assign(restaurant, dto); + await this.em.persistAndFlush(restaurant); + await this.invalidateRestaurantCache(restaurant.slug); + + return restaurant; + } } diff --git a/src/modules/restaurants/repositories/background.repository.ts b/src/modules/restaurants/repositories/background.repository.ts new file mode 100644 index 0000000..d773816 --- /dev/null +++ b/src/modules/restaurants/repositories/background.repository.ts @@ -0,0 +1,10 @@ +import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; +import { Injectable } from '@nestjs/common'; +import { Background } from '../entities/background.entity'; + +@Injectable() +export class BackgroundRepository extends EntityRepository { + constructor(readonly em: EntityManager) { + super(em, Background); + } +} diff --git a/src/modules/restaurants/restaurants.module.ts b/src/modules/restaurants/restaurants.module.ts index 579937a..8a35be2 100644 --- a/src/modules/restaurants/restaurants.module.ts +++ b/src/modules/restaurants/restaurants.module.ts @@ -12,11 +12,14 @@ import { RestaurantCrone } from './crone/restaurant.crone'; import { JwtModule } from '@nestjs/jwt'; import { AuthModule } from '../auth/auth.module'; import { UtilsModule } from '../utils/utils.module'; +import { Background } from './entities/background.entity'; +import { BackgroundRepository } from './repositories/background.repository'; @Module({ controllers: [RestaurantsController, ScheduleController], - providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService, RestaurantCrone], - imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule), UtilsModule], + providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService, RestaurantCrone, BackgroundRepository], + imports: [MikroOrmModule.forFeature([Restaurant, Schedule, Background]), + JwtModule, forwardRef(() => AuthModule), UtilsModule], exports: [RestRepository, ScheduleRepository, ScheduleService, RestaurantsService], }) export class RestaurantsModule { }