Files
ostan-complain-api/src/modules/attachment/attachment.service.ts
T

40 lines
1.5 KiB
TypeScript

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<Attachment> {
const attachment = this.attachmentRepository.create(createAttachmentDto);
await this.attachmentRepository.save(attachment);
return attachment;
}
async findAll(): Promise<Attachment[]> {
return await this.attachmentRepository.find();
}
async findOneOrFail(id: string): Promise<Attachment> {
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<Attachment> {
const attachment = await this.findOneOrFail(id);
Object.assign(attachment, updateAttachmentDto);
return await this.attachmentRepository.save(attachment);
}
async remove(id: string): Promise<Attachment> {
const attachment = await this.findOneOrFail(id);
await this.attachmentRepository.remove(attachment);
return attachment;
}
}