feat: added remark service
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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";
|
||||
|
||||
@Injectable()
|
||||
export class RemarkService {
|
||||
constructor(
|
||||
private readonly remarkRepository: RemarkRepository,
|
||||
private readonly taskRepository: TaskRepository,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<TMRemark[]> {
|
||||
return this.remarkRepository.findAll();
|
||||
}
|
||||
|
||||
findByTask(taskId: string): Promise<TMRemark[]> {
|
||||
return this.remarkRepository.findByTask(taskId);
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMRemark> {
|
||||
const remark = await this.remarkRepository.findOneById(id);
|
||||
if (!remark) throw new NotFoundException(`Remark #${id} 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`);
|
||||
|
||||
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);
|
||||
|
||||
if (dto.taskId) {
|
||||
const task = await this.taskRepository.findOneById(dto.taskId);
|
||||
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
|
||||
}
|
||||
|
||||
Object.assign(remark, dto);
|
||||
return this.remarkRepository.save(remark);
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.remarkRepository.delete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user