From 366e2e2a9248ea3c42becd1844455449feb96a3a Mon Sep 17 00:00:00 2001 From: realrafi Date: Mon, 27 Jul 2026 18:34:36 +0330 Subject: [PATCH] feat: added attachment section --- src/app.module.ts | 4 +- src/attachment/attachment.controller.ts | 34 ++++++++++++++++ src/attachment/attachment.module.ts | 13 +++++++ src/attachment/attachment.service.ts | 39 +++++++++++++++++++ src/attachment/dto/create-attachment.dto.ts | 26 +++++++++++++ src/attachment/dto/update-attachment.dto.ts | 4 ++ src/attachment/entities/attachment.entity.ts | 23 +++++++++++ src/attachment/enums/attachment-type.enum.ts | 7 ++++ .../repositories/attachment.repository.ts | 11 ++++++ src/common/enums/messages.enum.ts | 4 ++ src/complaint/complaint.service.ts | 2 +- src/organ/entities/organ.entity.ts | 2 +- 12 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 src/attachment/attachment.controller.ts create mode 100644 src/attachment/attachment.module.ts create mode 100644 src/attachment/attachment.service.ts create mode 100644 src/attachment/dto/create-attachment.dto.ts create mode 100644 src/attachment/dto/update-attachment.dto.ts create mode 100644 src/attachment/entities/attachment.entity.ts create mode 100644 src/attachment/enums/attachment-type.enum.ts create mode 100644 src/attachment/repositories/attachment.repository.ts diff --git a/src/app.module.ts b/src/app.module.ts index 9f36292..0ece03d 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -8,6 +8,7 @@ import { HttpModule } from '@nestjs/axios'; import { AuthModule } from './auth/auth.module'; import { OrganModule } from './organ/organ.module'; import { ComplaintModule } from './complaint/complaint.module'; +import { AttachmentModule } from './attachment/attachment.module'; @Module({ imports: [ @@ -20,7 +21,8 @@ import { ComplaintModule } from './complaint/complaint.module'; UsersModule, AuthModule, OrganModule, - ComplaintModule + ComplaintModule, + AttachmentModule ], }) export class AppModule {} diff --git a/src/attachment/attachment.controller.ts b/src/attachment/attachment.controller.ts new file mode 100644 index 0000000..9c2194e --- /dev/null +++ b/src/attachment/attachment.controller.ts @@ -0,0 +1,34 @@ +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { AttachmentService } from './attachment.service'; +import { CreateAttachmentDto } from './dto/create-attachment.dto'; +import { UpdateAttachmentDto } from './dto/update-attachment.dto'; + +@Controller('attachment') +export class AttachmentController { + constructor(private readonly attachmentService: AttachmentService) {} + + @Post() + create(@Body() createAttachmentDto: CreateAttachmentDto) { + return this.attachmentService.create(createAttachmentDto); + } + + @Get() + findAll() { + return this.attachmentService.findAll(); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.attachmentService.findOneOrFail(id); + } + + @Patch(':id') + update(@Param('id') id: string, @Body() updateAttachmentDto: UpdateAttachmentDto) { + return this.attachmentService.update(id, updateAttachmentDto); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.attachmentService.remove(id); + } +} diff --git a/src/attachment/attachment.module.ts b/src/attachment/attachment.module.ts new file mode 100644 index 0000000..798c836 --- /dev/null +++ b/src/attachment/attachment.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { AttachmentService } from './attachment.service'; +import { AttachmentController } from './attachment.controller'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Attachment } from './entities/attachment.entity'; +import { AttachmentRepository } from './repositories/attachment.repository'; + +@Module({ + imports: [TypeOrmModule.forFeature([Attachment])], + controllers: [AttachmentController], + providers: [AttachmentService, AttachmentRepository], +}) +export class AttachmentModule {} diff --git a/src/attachment/attachment.service.ts b/src/attachment/attachment.service.ts new file mode 100644 index 0000000..0c56c41 --- /dev/null +++ b/src/attachment/attachment.service.ts @@ -0,0 +1,39 @@ +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; + } +} diff --git a/src/attachment/dto/create-attachment.dto.ts b/src/attachment/dto/create-attachment.dto.ts new file mode 100644 index 0000000..3eddca0 --- /dev/null +++ b/src/attachment/dto/create-attachment.dto.ts @@ -0,0 +1,26 @@ +import { ApiProperty } from "@nestjs/swagger"; +import { IsEnum, IsNotEmpty, IsString } from "class-validator"; +import { AttachmentType } from "../enums/attachment-type.enum"; + +export class CreateAttachmentDto { + @ApiProperty({ + example: 'فایل شکایت', + }) + @IsString() + @IsNotEmpty() + title: string; + + @ApiProperty({ + example: AttachmentType.IMAGE, + }) + @IsEnum(AttachmentType) + @IsNotEmpty() + type: AttachmentType; + + @ApiProperty({ + example: '/images/file.pdf' + }) + @IsString() + @IsNotEmpty() + filepath: string; +} diff --git a/src/attachment/dto/update-attachment.dto.ts b/src/attachment/dto/update-attachment.dto.ts new file mode 100644 index 0000000..3171fa5 --- /dev/null +++ b/src/attachment/dto/update-attachment.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/swagger'; +import { CreateAttachmentDto } from './create-attachment.dto'; + +export class UpdateAttachmentDto extends PartialType(CreateAttachmentDto) {} diff --git a/src/attachment/entities/attachment.entity.ts b/src/attachment/entities/attachment.entity.ts new file mode 100644 index 0000000..371d0d3 --- /dev/null +++ b/src/attachment/entities/attachment.entity.ts @@ -0,0 +1,23 @@ +import { BaseEntity } from "src/common/entities/base.entity"; +import { Column, Entity } from "typeorm"; +import { AttachmentType } from "../enums/attachment-type.enum"; + +@Entity('attachments') +export class Attachment extends BaseEntity{ + @Column({ + type: 'varchar', + length: 50 + }) + title: string; + + @Column({ + type: 'enum', + enum: AttachmentType, + }) + type: AttachmentType; + + @Column({ + type: 'text' + }) + filepath: string; +} diff --git a/src/attachment/enums/attachment-type.enum.ts b/src/attachment/enums/attachment-type.enum.ts new file mode 100644 index 0000000..3fc27dc --- /dev/null +++ b/src/attachment/enums/attachment-type.enum.ts @@ -0,0 +1,7 @@ +export enum AttachmentType { + FILE = "file", + LINK = "link", + IMAGE = "image", + VIDEO = "video", + } + \ No newline at end of file diff --git a/src/attachment/repositories/attachment.repository.ts b/src/attachment/repositories/attachment.repository.ts new file mode 100644 index 0000000..71d8d7d --- /dev/null +++ b/src/attachment/repositories/attachment.repository.ts @@ -0,0 +1,11 @@ +import { Repository } from "typeorm"; +import { Attachment } from "../entities/attachment.entity"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Injectable } from "@nestjs/common"; + +@Injectable() +export class AttachmentRepository extends Repository { + constructor(@InjectRepository(Attachment) attachmentRepository: Repository) { + super(attachmentRepository.target, attachmentRepository.manager, attachmentRepository.queryRunner); + } +} \ No newline at end of file diff --git a/src/common/enums/messages.enum.ts b/src/common/enums/messages.enum.ts index d6b3081..b11c09e 100644 --- a/src/common/enums/messages.enum.ts +++ b/src/common/enums/messages.enum.ts @@ -26,4 +26,8 @@ export enum OrganMessages { export enum ComplaintMessages { COMPLAINT_NOT_FOUND = 'شکایت مورد نظر پیدا نشد!' +} + +export enum AttachmentMessages { + ATTACHMENT_NOT_FOUND = 'ضمیمه مورد نظر پیدا نشد!' } \ No newline at end of file diff --git a/src/complaint/complaint.service.ts b/src/complaint/complaint.service.ts index 0c08a37..e66c969 100644 --- a/src/complaint/complaint.service.ts +++ b/src/complaint/complaint.service.ts @@ -30,7 +30,7 @@ export class ComplaintService { async update(id: string, updateComplaintDto: UpdateComplaintDto): Promise { const complaint = await this.findOneOrFail(id); Object.assign(complaint, updateComplaintDto); - return complaint; + return this.complaintRepository.save(complaint); } async remove(id: string): Promise { diff --git a/src/organ/entities/organ.entity.ts b/src/organ/entities/organ.entity.ts index 37c4fb9..d8d06f0 100644 --- a/src/organ/entities/organ.entity.ts +++ b/src/organ/entities/organ.entity.ts @@ -11,7 +11,7 @@ export class Organ extends BaseEntity { name: string; @Column({ - type: 'varchar', + type: 'text', }) logo: string;