feat: added attachment section

This commit is contained in:
2026-07-27 18:34:36 +03:30
parent eb8d87bd48
commit 366e2e2a92
12 changed files with 166 additions and 3 deletions
+3 -1
View File
@@ -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 {}
+34
View File
@@ -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);
}
}
+13
View File
@@ -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 {}
+39
View File
@@ -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<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;
}
}
@@ -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;
}
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateAttachmentDto } from './create-attachment.dto';
export class UpdateAttachmentDto extends PartialType(CreateAttachmentDto) {}
@@ -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;
}
@@ -0,0 +1,7 @@
export enum AttachmentType {
FILE = "file",
LINK = "link",
IMAGE = "image",
VIDEO = "video",
}
@@ -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<Attachment> {
constructor(@InjectRepository(Attachment) attachmentRepository: Repository<Attachment>) {
super(attachmentRepository.target, attachmentRepository.manager, attachmentRepository.queryRunner);
}
}
+4
View File
@@ -27,3 +27,7 @@ export enum OrganMessages {
export enum ComplaintMessages {
COMPLAINT_NOT_FOUND = 'شکایت مورد نظر پیدا نشد!'
}
export enum AttachmentMessages {
ATTACHMENT_NOT_FOUND = 'ضمیمه مورد نظر پیدا نشد!'
}
+1 -1
View File
@@ -30,7 +30,7 @@ export class ComplaintService {
async update(id: string, updateComplaintDto: UpdateComplaintDto): Promise<Complaint> {
const complaint = await this.findOneOrFail(id);
Object.assign(complaint, updateComplaintDto);
return complaint;
return this.complaintRepository.save(complaint);
}
async remove(id: string): Promise<Complaint> {
+1 -1
View File
@@ -11,7 +11,7 @@ export class Organ extends BaseEntity {
name: string;
@Column({
type: 'varchar',
type: 'text',
})
logo: string;