feat: added attachment service
This commit is contained in:
@@ -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<TMAttachment[]> {
|
||||||
|
return this.attachmentRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
findByTask(taskId: string): Promise<TMAttachment[]> {
|
||||||
|
return this.attachmentRepository.findByTask(taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async findOne(id: string): Promise<TMAttachment> {
|
||||||
|
const attachment = await this.attachmentRepository.findOneById(id);
|
||||||
|
if (!attachment) throw new NotFoundException(`Attachment #${id} not found`);
|
||||||
|
return attachment;
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(dto: CreateAttachmentDto): Promise<TMAttachment> {
|
||||||
|
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<TMAttachment> {
|
||||||
|
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<void> {
|
||||||
|
await this.findOne(id);
|
||||||
|
await this.attachmentRepository.delete(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user