diff --git a/src/modules/restaurants/controllers/schedule.controller.ts b/src/modules/restaurants/controllers/schedule.controller.ts index 55dfeda..b0dc156 100644 --- a/src/modules/restaurants/controllers/schedule.controller.ts +++ b/src/modules/restaurants/controllers/schedule.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common'; +import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Query, ValidationPipe } from '@nestjs/common'; import { ApiTags, ApiOperation, @@ -8,10 +8,12 @@ import { ApiParam, ApiBody, ApiBearerAuth, + ApiQuery, } from '@nestjs/swagger'; import { ScheduleService } from '../providers/schedule.service'; import { CreateScheduleDto } from '../dto/create-schedule.dto'; import { UpdateScheduleDto } from '../dto/update-schedule.dto'; +import { FindSchedulesDto } from '../dto/find-schedules.dto'; import { Schedule } from '../entities/schedule.entity'; import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; import { RestId } from 'src/common/decorators/rest-id.decorator'; @@ -33,9 +35,18 @@ export class ScheduleController { @Get() @ApiOperation({ summary: 'Get all schedules' }) + @ApiQuery({ + name: 'weekDay', + required: false, + type: Number, + description: 'Filter by week days (0=Sunday, 6=Saturday). Can pass multiple: ?weekDay=0&weekDay=1&weekDay=2', + }) @ApiOkResponse({ description: 'List of schedules', type: [Schedule] }) - findAll(@RestId() restId: string) { - return this.scheduleService.findAll(restId); + findAll( + @Query(new ValidationPipe({ transform: true, whitelist: true })) query: FindSchedulesDto, + @RestId() restId: string, + ) { + return this.scheduleService.findAll(restId, query); } @Get(':id') diff --git a/src/modules/restaurants/dto/find-schedules.dto.ts b/src/modules/restaurants/dto/find-schedules.dto.ts new file mode 100644 index 0000000..d67fd1e --- /dev/null +++ b/src/modules/restaurants/dto/find-schedules.dto.ts @@ -0,0 +1,15 @@ +import { IsOptional, IsNumber, Min, Max } from 'class-validator'; +import { ApiPropertyOptional } from '@nestjs/swagger'; + +export class FindSchedulesDto { + @IsOptional() + @IsNumber({}, { each: true }) + @Min(0, { each: true }) + @Max(6, { each: true }) + @ApiPropertyOptional({ + example: 0, + description: 'Filter by week days (0=Sunday, 6=Saturday)', + type: [Number], + }) + weekDay?: number[]; +} diff --git a/src/modules/restaurants/entities/schedule.entity.ts b/src/modules/restaurants/entities/schedule.entity.ts index 79966c9..e78fedf 100644 --- a/src/modules/restaurants/entities/schedule.entity.ts +++ b/src/modules/restaurants/entities/schedule.entity.ts @@ -1,8 +1,7 @@ -import { Entity, Property, Unique } from '@mikro-orm/core'; +import { Entity, Property } from '@mikro-orm/core'; import { BaseEntity } from '../../../common/entities/base.entity'; @Entity({ tableName: 'schedules' }) -@Unique({ properties: ['restId', 'weekDay'] }) export class Schedule extends BaseEntity { @Property({ type: 'int' }) weekDay!: number; diff --git a/src/modules/restaurants/providers/schedule.service.ts b/src/modules/restaurants/providers/schedule.service.ts index c306a71..7c300e6 100644 --- a/src/modules/restaurants/providers/schedule.service.ts +++ b/src/modules/restaurants/providers/schedule.service.ts @@ -4,6 +4,7 @@ import { Schedule } from '../entities/schedule.entity'; import { ScheduleRepository } from '../repositories/schedule.repository'; import { CreateScheduleDto } from '../dto/create-schedule.dto'; import { UpdateScheduleDto } from '../dto/update-schedule.dto'; +import { FindSchedulesDto } from '../dto/find-schedules.dto'; import { RestRepository } from '../repositories/rest.repository'; // import { RestMessage } from 'src/common/enums/message.enum'; @@ -34,8 +35,13 @@ export class ScheduleService { return schedule; } - async findAll(restId?: string): Promise { - const where = restId ? { restId } : {}; + async findAll(restId?: string, query?: FindSchedulesDto): Promise { + const where: any = restId ? { restId } : {}; + + if (query?.weekDay) { + where.weekDay = query.weekDay; + } + return this.scheduleRepository.find(where, { orderBy: { weekDay: 'asc' } }); }