refactor: refactored attachment entirely

This commit is contained in:
2026-07-07 11:59:05 +03:30
parent 9565eda0fd
commit eb6fbc3ce5
4 changed files with 26 additions and 34 deletions
+4
View File
@@ -946,3 +946,7 @@ export const enum TaskMessage {
export const enum remarkMessage {
REMARK_NOT_FOUND = "برچسب مورد نظر پیدا نشد!"
}
export const enum attachmentMessage {
ATTACHMENT_NOT_FOUND = "ضمیمه پیدا نشد!"
}
@@ -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<TMAttachment> {
return this.attachmentService.findOne(id);
return this.attachmentService.findOneOrFail(id);
}
@Post()
@@ -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<TMAttachment[]> {
@@ -20,26 +21,24 @@ export class AttachmentService {
return this.attachmentRepository.findByTask(taskId);
}
async findOne(id: string): Promise<TMAttachment> {
async findOneOrFail(id: string): Promise<TMAttachment> {
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<TMAttachment> {
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<TMAttachment> {
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<void> {
await this.findOne(id);
await this.findOneOrFail(id);
await this.attachmentRepository.delete(id);
}
}
@@ -4,44 +4,29 @@ import { Repository } from 'typeorm';
import { TMAttachment } from '../entities/attachment.entity';
@Injectable()
export class AttachmentRepository {
export class AttachmentRepository extends Repository<TMAttachment>{
constructor(
@InjectRepository(TMAttachment)
private readonly repository: Repository<TMAttachment>,
) {}
@InjectRepository(TMAttachment) attachmentRepository: Repository<TMAttachment>,
) {
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<TMAttachment>) {
return this.repository.create(data);
}
save(attachment: TMAttachment) {
return this.repository.save(attachment);
}
update(id: string, data: Partial<TMAttachment>) {
return this.repository.update(id, data);
}
delete(id: string) {
return this.repository.delete(id);
}
}