import { Injectable, NotFoundException } from '@nestjs/common'; import { CreateAttachmentDto } from './dto/create-attachment.dto'; import { UpdateAttachmentDto } from './dto/update-attachment.dto'; import { Attachment } from './entities/attachment.entity'; import { AttachmentRepository } from './repositories/attachment.repository'; import { AttachmentMessages } from 'src/common/enums/messages.enum'; @Injectable() export class AttachmentService { constructor(private readonly attachmentRepository: AttachmentRepository) {} async create(createAttachmentDto: CreateAttachmentDto): Promise { const attachment = this.attachmentRepository.create(createAttachmentDto); await this.attachmentRepository.save(attachment); return attachment; } async findAll(): Promise { return await this.attachmentRepository.find(); } async findOneOrFail(id: string): Promise { const attachment = await this.attachmentRepository.findOne({where: {id}}); if(!attachment) throw new NotFoundException(AttachmentMessages.ATTACHMENT_NOT_FOUND); return attachment; } async update(id: string, updateAttachmentDto: UpdateAttachmentDto): Promise { const attachment = await this.findOneOrFail(id); Object.assign(attachment, updateAttachmentDto); return await this.attachmentRepository.save(attachment); } async remove(id: string): Promise { const attachment = await this.findOneOrFail(id); await this.attachmentRepository.remove(attachment); return attachment; } }