This commit is contained in:
2025-11-15 11:54:39 +03:30
parent f0913840f2
commit e521e56f3e
10 changed files with 213 additions and 15 deletions
@@ -0,0 +1,77 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { EntityManager, RequiredEntityData } from '@mikro-orm/postgresql';
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 { RestRepository } from '../repositories/rest.repository';
// import { RestMessage } from 'src/common/enums/message.enum';
@Injectable()
export class ScheduleService {
constructor(
private readonly scheduleRepository: ScheduleRepository,
private readonly restRepository: RestRepository,
private readonly em: EntityManager,
) {}
async create(createScheduleDto: CreateScheduleDto): Promise<Schedule> {
const data: RequiredEntityData<Schedule> = {
date: new Date(createScheduleDto.date),
openTime: createScheduleDto.openTime,
closeTime: createScheduleDto.closeTime,
isActive: createScheduleDto.isActive ?? true,
restId: '01KA35CFFN0F6VA04DF0W6KCE3',
};
const schedule = this.scheduleRepository.create(data);
await this.em.persistAndFlush(schedule);
return schedule;
}
async findAll(restId?: string): Promise<Schedule[]> {
const where = restId ? { restId } : {};
return this.scheduleRepository.find(where);
}
async findOne(id: string): Promise<Schedule> {
const schedule = await this.scheduleRepository.findOne({ id });
if (!schedule) {
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
}
return schedule;
}
async update(id: string, updateScheduleDto: UpdateScheduleDto): Promise<Schedule> {
const schedule = await this.scheduleRepository.findOne({ id });
if (!schedule) {
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
}
const updateData: Partial<Schedule> = {};
if (updateScheduleDto.date) {
updateData.date = new Date(updateScheduleDto.date);
}
if (updateScheduleDto.openTime !== undefined) {
updateData.openTime = updateScheduleDto.openTime;
}
if (updateScheduleDto.closeTime !== undefined) {
updateData.closeTime = updateScheduleDto.closeTime;
}
if (updateScheduleDto.isActive !== undefined) {
updateData.isActive = updateScheduleDto.isActive;
}
this.em.assign(schedule, updateData);
await this.em.persistAndFlush(schedule);
return schedule;
}
async remove(id: string): Promise<void> {
const schedule = await this.scheduleRepository.findOne({ id });
if (!schedule) {
throw new NotFoundException(`برنامه با شناسه ${id} یافت نشد.`);
}
await this.em.removeAndFlush(schedule);
}
}