refactor: refactored attachment entirely
This commit is contained in:
@@ -945,4 +945,8 @@ export const enum TaskMessage {
|
|||||||
|
|
||||||
export const enum remarkMessage {
|
export const enum remarkMessage {
|
||||||
REMARK_NOT_FOUND = "برچسب مورد نظر پیدا نشد!"
|
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 { CreateAttachmentDto } from '../dto/attachment/create-attachment.dto';
|
||||||
import { UpdateAttachmentDto } from '../dto/attachment/update-attachment.dto';
|
import { UpdateAttachmentDto } from '../dto/attachment/update-attachment.dto';
|
||||||
import { TMAttachment } from '../entities/attachment.entity';
|
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')
|
@ApiTags('Task Manager - Attachments')
|
||||||
@Controller('task-manager/attachments')
|
@Controller('task-manager/attachments')
|
||||||
export class AttachmentController {
|
export class AttachmentController {
|
||||||
@@ -25,7 +29,7 @@ export class AttachmentController {
|
|||||||
@ApiResponse({ status: 200, description: 'Attachment found', type: TMAttachment })
|
@ApiResponse({ status: 200, description: 'Attachment found', type: TMAttachment })
|
||||||
@ApiResponse({ status: 404, description: 'Attachment not found' })
|
@ApiResponse({ status: 404, description: 'Attachment not found' })
|
||||||
findOne(@Param('id') id: string): Promise<TMAttachment> {
|
findOne(@Param('id') id: string): Promise<TMAttachment> {
|
||||||
return this.attachmentService.findOne(id);
|
return this.attachmentService.findOneOrFail(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||||
import { AttachmentRepository } from "../repositories/attachment.repository";
|
import { AttachmentRepository } from "../repositories/attachment.repository";
|
||||||
import { TaskRepository } from "../repositories/task.repository";
|
|
||||||
import { TMAttachment } from "../entities/attachment.entity";
|
import { TMAttachment } from "../entities/attachment.entity";
|
||||||
import { CreateAttachmentDto } from "../dto/attachment/create-attachment.dto";
|
import { CreateAttachmentDto } from "../dto/attachment/create-attachment.dto";
|
||||||
import { UpdateAttachmentDto } from "../dto/attachment/update-attachment.dto";
|
import { UpdateAttachmentDto } from "../dto/attachment/update-attachment.dto";
|
||||||
|
import { attachmentMessage } from "../../../common/enums/message.enum";
|
||||||
|
import { TaskService } from './task.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AttachmentService {
|
export class AttachmentService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly attachmentRepository: AttachmentRepository,
|
private readonly attachmentRepository: AttachmentRepository,
|
||||||
private readonly taskRepository: TaskRepository,
|
private readonly taskService: TaskService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
findAll(): Promise<TMAttachment[]> {
|
findAll(): Promise<TMAttachment[]> {
|
||||||
@@ -20,26 +21,24 @@ export class AttachmentService {
|
|||||||
return this.attachmentRepository.findByTask(taskId);
|
return this.attachmentRepository.findByTask(taskId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: string): Promise<TMAttachment> {
|
async findOneOrFail(id: string): Promise<TMAttachment> {
|
||||||
const attachment = await this.attachmentRepository.findOneById(id);
|
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;
|
return attachment;
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(dto: CreateAttachmentDto): Promise<TMAttachment> {
|
async create(dto: CreateAttachmentDto): Promise<TMAttachment> {
|
||||||
const task = await this.taskRepository.findOneById(dto.taskId);
|
await this.taskService.findOneOrFail(dto.taskId);
|
||||||
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
|
|
||||||
|
|
||||||
const attachment = this.attachmentRepository.create(dto);
|
const attachment = this.attachmentRepository.create(dto);
|
||||||
return this.attachmentRepository.save(attachment);
|
return this.attachmentRepository.save(attachment);
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, dto: UpdateAttachmentDto): Promise<TMAttachment> {
|
async update(id: string, dto: UpdateAttachmentDto): Promise<TMAttachment> {
|
||||||
const attachment = await this.findOne(id);
|
const attachment = await this.findOneOrFail(id);
|
||||||
|
|
||||||
if (dto.taskId) {
|
if (dto.taskId) {
|
||||||
const task = await this.taskRepository.findOneById(dto.taskId);
|
await this.taskService.findOneOrFail(dto.taskId);
|
||||||
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(attachment, dto);
|
Object.assign(attachment, dto);
|
||||||
@@ -47,7 +46,7 @@ export class AttachmentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: string): Promise<void> {
|
async remove(id: string): Promise<void> {
|
||||||
await this.findOne(id);
|
await this.findOneOrFail(id);
|
||||||
await this.attachmentRepository.delete(id);
|
await this.attachmentRepository.delete(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,44 +4,29 @@ import { Repository } from 'typeorm';
|
|||||||
import { TMAttachment } from '../entities/attachment.entity';
|
import { TMAttachment } from '../entities/attachment.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AttachmentRepository {
|
export class AttachmentRepository extends Repository<TMAttachment>{
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(TMAttachment)
|
@InjectRepository(TMAttachment) attachmentRepository: Repository<TMAttachment>,
|
||||||
private readonly repository: Repository<TMAttachment>,
|
) {
|
||||||
) {}
|
super(attachmentRepository.target, attachmentRepository.manager, attachmentRepository.queryRunner);
|
||||||
|
}
|
||||||
|
|
||||||
findAll() {
|
findAll() {
|
||||||
return this.repository.find({
|
return this.find({
|
||||||
relations: ['task'],
|
relations: ['task'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
findByTask(taskId: string) {
|
findByTask(taskId: string) {
|
||||||
return this.repository.find({
|
return this.find({
|
||||||
where: { taskId },
|
where: { taskId },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
findOneById(id: string) {
|
findOneById(id: string) {
|
||||||
return this.repository.findOne({
|
return this.findOne({
|
||||||
where: { id },
|
where: { id },
|
||||||
relations: ['task'],
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user