diff --git a/src/modules/task-manager/providers/attachment.service.ts b/src/modules/task-manager/providers/attachment.service.ts new file mode 100644 index 0000000..a22b76b --- /dev/null +++ b/src/modules/task-manager/providers/attachment.service.ts @@ -0,0 +1,53 @@ +import { Injectable, NotFoundException } from "@nestjs/common"; +import { AttachmentRepository } from "../repositories/attachment.repository"; +import { TaskRepository } from "../repositories/task.repository"; +import { TMAttachment } from "../entities/attachment.entity"; +import { CreateAttachmentDto } from "../dto/attachment/create-attachment.dto"; +import { UpdateAttachmentDto } from "../dto/attachment/update-attachment.dto"; + +@Injectable() +export class AttachmentService { + constructor( + private readonly attachmentRepository: AttachmentRepository, + private readonly taskRepository: TaskRepository, + ) {} + + findAll(): Promise { + return this.attachmentRepository.findAll(); + } + + findByTask(taskId: string): Promise { + return this.attachmentRepository.findByTask(taskId); + } + + async findOne(id: string): Promise { + const attachment = await this.attachmentRepository.findOneById(id); + if (!attachment) throw new NotFoundException(`Attachment #${id} not found`); + return attachment; + } + + async create(dto: CreateAttachmentDto): Promise { + const task = await this.taskRepository.findOneById(dto.taskId); + if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`); + + const attachment = this.attachmentRepository.create(dto); + return this.attachmentRepository.save(attachment); + } + + async update(id: string, dto: UpdateAttachmentDto): Promise { + const attachment = 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(attachment, dto); + return this.attachmentRepository.save(attachment); + } + + async remove(id: string): Promise { + await this.findOne(id); + await this.attachmentRepository.delete(id); + } +}