Implement restaurant ID handling in schedule operations; add AdminAuthGuard for access control; enhance schedule service with validation for existing schedules and restaurant existence checks.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, ValidationPipe } from '@nestjs/common';
|
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, ValidationPipe, UseGuards } from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
ApiTags,
|
ApiTags,
|
||||||
ApiOperation,
|
ApiOperation,
|
||||||
@@ -13,9 +13,12 @@ import { ScheduleService } from '../providers/schedule.service';
|
|||||||
import { CreateScheduleDto } from '../dto/create-schedule.dto';
|
import { CreateScheduleDto } from '../dto/create-schedule.dto';
|
||||||
import { UpdateScheduleDto } from '../dto/update-schedule.dto';
|
import { UpdateScheduleDto } from '../dto/update-schedule.dto';
|
||||||
import { Schedule } from '../entities/schedule.entity';
|
import { Schedule } from '../entities/schedule.entity';
|
||||||
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||||
|
import { RestId } from 'src/common/decorators/rest-id.decorator';
|
||||||
|
|
||||||
@ApiTags('schedules')
|
@ApiTags('schedules')
|
||||||
@Controller('schedules')
|
@Controller('schedules')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
export class ScheduleController {
|
export class ScheduleController {
|
||||||
constructor(private readonly scheduleService: ScheduleService) {}
|
constructor(private readonly scheduleService: ScheduleService) {}
|
||||||
|
|
||||||
@@ -23,15 +26,15 @@ export class ScheduleController {
|
|||||||
@ApiOperation({ summary: 'ایجاد برنامه جدید' })
|
@ApiOperation({ summary: 'ایجاد برنامه جدید' })
|
||||||
@ApiBody({ type: CreateScheduleDto })
|
@ApiBody({ type: CreateScheduleDto })
|
||||||
@ApiCreatedResponse({ description: 'برنامه ایجاد شد', type: Schedule })
|
@ApiCreatedResponse({ description: 'برنامه ایجاد شد', type: Schedule })
|
||||||
create(@Body(new ValidationPipe({ transform: true, whitelist: true })) createScheduleDto: CreateScheduleDto) {
|
create(@Body() createScheduleDto: CreateScheduleDto, @RestId() restId: string) {
|
||||||
return this.scheduleService.create(createScheduleDto);
|
return this.scheduleService.create(restId, createScheduleDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@ApiOperation({ summary: 'لیست همه برنامهها' })
|
@ApiOperation({ summary: 'لیست همه برنامهها' })
|
||||||
@ApiQuery({ name: 'restId', required: false, type: String, description: 'شناسه رستوران برای فیلتر کردن' })
|
@ApiQuery({ name: 'restId', required: false, type: String, description: 'شناسه رستوران برای فیلتر کردن' })
|
||||||
@ApiOkResponse({ description: 'لیست برنامهها', type: [Schedule] })
|
@ApiOkResponse({ description: 'لیست برنامهها', type: [Schedule] })
|
||||||
findAll(@Query('restId') restId?: string) {
|
findAll(@RestId() restId: string) {
|
||||||
return this.scheduleService.findAll(restId);
|
return this.scheduleService.findAll(restId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,8 +43,8 @@ export class ScheduleController {
|
|||||||
@ApiParam({ name: 'id', description: 'شناسه برنامه' })
|
@ApiParam({ name: 'id', description: 'شناسه برنامه' })
|
||||||
@ApiOkResponse({ description: 'جزئیات برنامه', type: Schedule })
|
@ApiOkResponse({ description: 'جزئیات برنامه', type: Schedule })
|
||||||
@ApiNotFoundResponse({ description: 'برنامه یافت نشد' })
|
@ApiNotFoundResponse({ description: 'برنامه یافت نشد' })
|
||||||
findOne(@Param('id') id: string) {
|
findOne(@Param('id') id: string, @RestId() restId: string) {
|
||||||
return this.scheduleService.findOne(id);
|
return this.scheduleService.findOne(id, restId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id')
|
@Patch(':id')
|
||||||
@@ -50,11 +53,8 @@ export class ScheduleController {
|
|||||||
@ApiBody({ type: UpdateScheduleDto })
|
@ApiBody({ type: UpdateScheduleDto })
|
||||||
@ApiOkResponse({ description: 'برنامه بهروز شد', type: Schedule })
|
@ApiOkResponse({ description: 'برنامه بهروز شد', type: Schedule })
|
||||||
@ApiNotFoundResponse({ description: 'برنامه یافت نشد' })
|
@ApiNotFoundResponse({ description: 'برنامه یافت نشد' })
|
||||||
update(
|
update(@Param('id') id: string, @Body() updateScheduleDto: UpdateScheduleDto, @RestId() restId: string) {
|
||||||
@Param('id') id: string,
|
return this.scheduleService.update(id, restId, updateScheduleDto);
|
||||||
@Body(new ValidationPipe({ transform: true, whitelist: true })) updateScheduleDto: UpdateScheduleDto,
|
|
||||||
) {
|
|
||||||
return this.scheduleService.update(id, updateScheduleDto);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
@@ -62,7 +62,7 @@ export class ScheduleController {
|
|||||||
@ApiParam({ name: 'id', description: 'شناسه برنامه' })
|
@ApiParam({ name: 'id', description: 'شناسه برنامه' })
|
||||||
@ApiOkResponse({ description: 'برنامه حذف شد' })
|
@ApiOkResponse({ description: 'برنامه حذف شد' })
|
||||||
@ApiNotFoundResponse({ description: 'برنامه یافت نشد' })
|
@ApiNotFoundResponse({ description: 'برنامه یافت نشد' })
|
||||||
remove(@Param('id') id: string) {
|
remove(@Param('id') id: string, @RestId() restId: string) {
|
||||||
return this.scheduleService.remove(id);
|
return this.scheduleService.remove(id, restId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,4 @@ export class CreateScheduleDto {
|
|||||||
@Type(() => Boolean)
|
@Type(() => Boolean)
|
||||||
isActive?: boolean;
|
isActive?: boolean;
|
||||||
|
|
||||||
@ApiProperty({ example: 'rest-ulid-123', description: 'شناسه رستوران' })
|
|
||||||
@IsString()
|
|
||||||
restId!: string;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||||
import { EntityManager, RequiredEntityData } from '@mikro-orm/postgresql';
|
import { EntityManager, RequiredEntityData } from '@mikro-orm/postgresql';
|
||||||
import { Schedule } from '../entities/schedule.entity';
|
import { Schedule } from '../entities/schedule.entity';
|
||||||
import { ScheduleRepository } from '../repositories/schedule.repository';
|
import { ScheduleRepository } from '../repositories/schedule.repository';
|
||||||
@@ -15,13 +15,23 @@ export class ScheduleService {
|
|||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async create(createScheduleDto: CreateScheduleDto): Promise<Schedule> {
|
async create(restId: string, createScheduleDto: CreateScheduleDto): Promise<Schedule> {
|
||||||
|
const rest = await this.restRepository.findOne({ id: restId });
|
||||||
|
if (!rest) {
|
||||||
|
throw new NotFoundException(`رستوران با شناسه ${restId} یافت نشد.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingSchedule = await this.scheduleRepository.findOne({ restId, weekDay: createScheduleDto.weekDay });
|
||||||
|
if (existingSchedule) {
|
||||||
|
throw new BadRequestException(`برنامه برای این روز هفته و رستوران قبلا ایجاد شده است.`);
|
||||||
|
}
|
||||||
|
|
||||||
const data: RequiredEntityData<Schedule> = {
|
const data: RequiredEntityData<Schedule> = {
|
||||||
weekDay: createScheduleDto.weekDay,
|
weekDay: createScheduleDto.weekDay,
|
||||||
openTime: createScheduleDto.openTime,
|
openTime: createScheduleDto.openTime,
|
||||||
closeTime: createScheduleDto.closeTime,
|
closeTime: createScheduleDto.closeTime,
|
||||||
isActive: createScheduleDto.isActive ?? true,
|
isActive: createScheduleDto.isActive ?? true,
|
||||||
restId: '01KA35CFFN0F6VA04DF0W6KCE3',
|
restId,
|
||||||
};
|
};
|
||||||
|
|
||||||
const schedule = this.scheduleRepository.create(data);
|
const schedule = this.scheduleRepository.create(data);
|
||||||
@@ -34,16 +44,16 @@ export class ScheduleService {
|
|||||||
return this.scheduleRepository.find(where);
|
return this.scheduleRepository.find(where);
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: string): Promise<Schedule> {
|
async findOne(id: string, restId: string): Promise<Schedule> {
|
||||||
const schedule = await this.scheduleRepository.findOne({ id });
|
const schedule = await this.scheduleRepository.findOne({ id, restId });
|
||||||
if (!schedule) {
|
if (!schedule) {
|
||||||
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
|
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
|
||||||
}
|
}
|
||||||
return schedule;
|
return schedule;
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, updateScheduleDto: UpdateScheduleDto): Promise<Schedule> {
|
async update(id: string, restId: string, updateScheduleDto: UpdateScheduleDto): Promise<Schedule> {
|
||||||
const schedule = await this.scheduleRepository.findOne({ id });
|
const schedule = await this.scheduleRepository.findOne({ id, restId });
|
||||||
if (!schedule) {
|
if (!schedule) {
|
||||||
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
|
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
|
||||||
}
|
}
|
||||||
@@ -61,17 +71,14 @@ export class ScheduleService {
|
|||||||
if (updateScheduleDto.isActive !== undefined) {
|
if (updateScheduleDto.isActive !== undefined) {
|
||||||
updateData.isActive = updateScheduleDto.isActive;
|
updateData.isActive = updateScheduleDto.isActive;
|
||||||
}
|
}
|
||||||
// if (updateScheduleDto.restId) {
|
|
||||||
// updateData.restId = updateScheduleDto.restId;
|
|
||||||
// }
|
|
||||||
|
|
||||||
this.em.assign(schedule, updateData);
|
this.em.assign(schedule, updateData);
|
||||||
await this.em.persistAndFlush(schedule);
|
await this.em.persistAndFlush(schedule);
|
||||||
return schedule;
|
return schedule;
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: string): Promise<void> {
|
async remove(id: string, restId: string): Promise<void> {
|
||||||
const schedule = await this.scheduleRepository.findOne({ id });
|
const schedule = await this.scheduleRepository.findOne({ id, restId });
|
||||||
if (!schedule) {
|
if (!schedule) {
|
||||||
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
|
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user