45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { Injectable, NotFoundException } from "@nestjs/common";
|
|
import { RemarkRepository } from "../repositories/remark.repository";
|
|
import { TMRemark } from "../entities/remark.entity";
|
|
import { CreateRemarkDto } from "../dto/remark/create-remark.dto";
|
|
import { UpdateRemarkDto } from "../dto/remark/update-remark.dto";
|
|
import { remarkMessage } from "../../../common/enums/message.enum";
|
|
import { TaskService } from './task.service';
|
|
|
|
@Injectable()
|
|
export class RemarkService {
|
|
constructor(
|
|
private readonly remarkRepository: RemarkRepository,
|
|
private readonly taskService: TaskService,
|
|
) {}
|
|
|
|
async findOneOrFail(id: string): Promise<TMRemark> {
|
|
const remark = await this.remarkRepository.findOne({ where: { id } });
|
|
if (!remark) throw new NotFoundException(remarkMessage.REMARK_NOT_FOUND);
|
|
return remark;
|
|
}
|
|
|
|
async create(dto: CreateRemarkDto): Promise<TMRemark> {
|
|
await this.taskService.findOneOrFail(dto.taskId);
|
|
|
|
const remark = this.remarkRepository.create(dto);
|
|
return this.remarkRepository.save(remark);
|
|
}
|
|
|
|
async update(id: string, dto: UpdateRemarkDto): Promise<TMRemark> {
|
|
const remark = await this.findOneOrFail(id);
|
|
|
|
if (dto.taskId) {
|
|
await this.taskService.findOneOrFail(dto.taskId);
|
|
}
|
|
|
|
Object.assign(remark, dto);
|
|
return this.remarkRepository.save(remark);
|
|
}
|
|
|
|
async remove(id: string): Promise<void> {
|
|
await this.findOneOrFail(id);
|
|
await this.remarkRepository.delete(id);
|
|
}
|
|
}
|