diff --git a/src/common/enums/message.enum.ts b/src/common/enums/message.enum.ts index 2c456d5..467513c 100755 --- a/src/common/enums/message.enum.ts +++ b/src/common/enums/message.enum.ts @@ -945,4 +945,8 @@ export const enum TaskMessage { export const enum remarkMessage { REMARK_NOT_FOUND = "برچسب مورد نظر پیدا نشد!" +} + +export const enum attachmentMessage { + ATTACHMENT_NOT_FOUND = "ضمیمه پیدا نشد!" } \ No newline at end of file diff --git a/src/modules/task-manager/controllers/attachment.controller.ts b/src/modules/task-manager/controllers/attachment.controller.ts index 7c004a7..8a4fe1d 100644 --- a/src/modules/task-manager/controllers/attachment.controller.ts +++ b/src/modules/task-manager/controllers/attachment.controller.ts @@ -4,7 +4,11 @@ import { AttachmentService } from '../providers/attachment.service'; import { CreateAttachmentDto } from '../dto/attachment/create-attachment.dto'; import { UpdateAttachmentDto } from '../dto/attachment/update-attachment.dto'; import { TMAttachment } from '../entities/attachment.entity'; +import { AuthGuards } from '../../../common/decorators/auth-guard.decorator'; +import { AdminRoute } from '../../../common/decorators/admin.decorator'; +@AuthGuards() +@AdminRoute() @ApiTags('Task Manager - Attachments') @Controller('task-manager/attachments') export class AttachmentController { @@ -25,7 +29,7 @@ export class AttachmentController { @ApiResponse({ status: 200, description: 'Attachment found', type: TMAttachment }) @ApiResponse({ status: 404, description: 'Attachment not found' }) findOne(@Param('id') id: string): Promise { - return this.attachmentService.findOne(id); + return this.attachmentService.findOneOrFail(id); } @Post() diff --git a/src/modules/task-manager/providers/attachment.service.ts b/src/modules/task-manager/providers/attachment.service.ts index a22b76b..6854d30 100644 --- a/src/modules/task-manager/providers/attachment.service.ts +++ b/src/modules/task-manager/providers/attachment.service.ts @@ -1,15 +1,16 @@ 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"; +import { attachmentMessage } from "../../../common/enums/message.enum"; +import { TaskService } from './task.service'; @Injectable() export class AttachmentService { constructor( private readonly attachmentRepository: AttachmentRepository, - private readonly taskRepository: TaskRepository, + private readonly taskService: TaskService, ) {} findAll(): Promise { @@ -20,26 +21,24 @@ export class AttachmentService { return this.attachmentRepository.findByTask(taskId); } - async findOne(id: string): Promise { + async findOneOrFail(id: string): Promise { const attachment = await this.attachmentRepository.findOneById(id); - if (!attachment) throw new NotFoundException(`Attachment #${id} not found`); + if (!attachment) throw new NotFoundException(attachmentMessage.ATTACHMENT_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`); + await this.taskService.findOneOrFail(dto.taskId); const attachment = this.attachmentRepository.create(dto); return this.attachmentRepository.save(attachment); } async update(id: string, dto: UpdateAttachmentDto): Promise { - const attachment = await this.findOne(id); + const attachment = await this.findOneOrFail(id); if (dto.taskId) { - const task = await this.taskRepository.findOneById(dto.taskId); - if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`); + await this.taskService.findOneOrFail(dto.taskId); } Object.assign(attachment, dto); @@ -47,7 +46,7 @@ export class AttachmentService { } async remove(id: string): Promise { - await this.findOne(id); + await this.findOneOrFail(id); await this.attachmentRepository.delete(id); } } diff --git a/src/modules/task-manager/repositories/attachment.repository.ts b/src/modules/task-manager/repositories/attachment.repository.ts index 9fd3f26..c8b2c9d 100644 --- a/src/modules/task-manager/repositories/attachment.repository.ts +++ b/src/modules/task-manager/repositories/attachment.repository.ts @@ -4,44 +4,29 @@ import { Repository } from 'typeorm'; import { TMAttachment } from '../entities/attachment.entity'; @Injectable() -export class AttachmentRepository { +export class AttachmentRepository extends Repository{ constructor( - @InjectRepository(TMAttachment) - private readonly repository: Repository, - ) {} + @InjectRepository(TMAttachment) attachmentRepository: Repository, + ) { + super(attachmentRepository.target, attachmentRepository.manager, attachmentRepository.queryRunner); + } findAll() { - return this.repository.find({ + return this.find({ relations: ['task'], }); } findByTask(taskId: string) { - return this.repository.find({ + return this.find({ where: { taskId }, }); } findOneById(id: string) { - return this.repository.findOne({ + return this.findOne({ where: { id }, relations: ['task'], }); } - - create(data: Partial) { - return this.repository.create(data); - } - - save(attachment: TMAttachment) { - return this.repository.save(attachment); - } - - update(id: string, data: Partial) { - return this.repository.update(id, data); - } - - delete(id: string) { - return this.repository.delete(id); - } } \ No newline at end of file