refactor: refactored remark entirely
This commit is contained in:
@@ -1,45 +1,49 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { RemarkRepository } from "../repositories/remark.repository";
|
||||
import { TaskRepository } from "../repositories/task.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 taskRepository: TaskRepository,
|
||||
private readonly taskService: TaskService,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<TMRemark[]> {
|
||||
return this.remarkRepository.findAll();
|
||||
return this.remarkRepository.find({ relations: { task: true } });
|
||||
}
|
||||
|
||||
findByTask(taskId: string): Promise<TMRemark[]> {
|
||||
return this.remarkRepository.findByTask(taskId);
|
||||
return this.remarkRepository.find({
|
||||
where: { taskId },
|
||||
relations: {
|
||||
task: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMRemark> {
|
||||
const remark = await this.remarkRepository.findOneById(id);
|
||||
if (!remark) throw new NotFoundException(`Remark #${id} not found`);
|
||||
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> {
|
||||
const task = await this.taskRepository.findOneById(dto.taskId);
|
||||
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
|
||||
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.findOne(id);
|
||||
const remark = await this.findOneOrFail(id);
|
||||
|
||||
if (dto.taskId) {
|
||||
const task = await this.taskRepository.findOneById(dto.taskId);
|
||||
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
|
||||
await this.taskService.findOneOrFail(dto.taskId);
|
||||
}
|
||||
|
||||
Object.assign(remark, dto);
|
||||
@@ -47,7 +51,7 @@ export class RemarkService {
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.findOneOrFail(id);
|
||||
await this.remarkRepository.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user