up
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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[];
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Schedule[]> {
|
||||
const where = restId ? { restId } : {};
|
||||
async findAll(restId?: string, query?: FindSchedulesDto): Promise<Schedule[]> {
|
||||
const where: any = restId ? { restId } : {};
|
||||
|
||||
if (query?.weekDay) {
|
||||
where.weekDay = query.weekDay;
|
||||
}
|
||||
|
||||
return this.scheduleRepository.find(where, { orderBy: { weekDay: 'asc' } });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user