diff --git a/src/app.module.ts b/src/app.module.ts index a742a29..67f4453 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,12 +1,12 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { ConfigModule, ConfigService } from '@nestjs/config'; -import Joi from 'joi'; import { validationSchema } from './config/config.validation'; import { UsersModule } from './users/users.module'; import { databaseConfig } from './config/typeorm.config'; import { HttpModule } from '@nestjs/axios'; import { AuthModule } from './auth/auth.module'; +import { OrganModule } from './organ/organ.module'; @Module({ imports: [ @@ -17,7 +17,8 @@ import { AuthModule } from './auth/auth.module'; TypeOrmModule.forRootAsync(databaseConfig()), HttpModule.register({global: true, timeout: 10000, headers: {"Content-Type": "application/json"}}), UsersModule, - AuthModule + AuthModule, + OrganModule ], }) export class AppModule {} diff --git a/src/common/enums/messages.enum.ts b/src/common/enums/messages.enum.ts index ac544c7..0867abe 100644 --- a/src/common/enums/messages.enum.ts +++ b/src/common/enums/messages.enum.ts @@ -18,4 +18,8 @@ export enum UserRoleMessages { export enum AuthMessages { TOKEN_EXPIRED_OR_INVALID = 'توکن منسوخ شده یا نامعتبر است!', +} + +export enum OrganMessages { + ORGAN_NOT_FOUND = 'ارگان مورد نظر پیدا نشد!' } \ No newline at end of file diff --git a/src/organ/dto/create-organ.dto.ts b/src/organ/dto/create-organ.dto.ts new file mode 100644 index 0000000..262ae2e --- /dev/null +++ b/src/organ/dto/create-organ.dto.ts @@ -0,0 +1,13 @@ +import { ApiProperty } from "@nestjs/swagger"; +import { IsNotEmpty, IsString } from "class-validator"; + +export class CreateOrganDto { + @ApiProperty({example: 'استانداری', type: 'string'}) + @IsString() + @IsNotEmpty() + name: string; + + @ApiProperty({example: '/images/logos/xyz.jpg', type: 'string'}) + @IsString() + logo: string; +} diff --git a/src/organ/dto/update-organ.dto.ts b/src/organ/dto/update-organ.dto.ts new file mode 100644 index 0000000..9f2558b --- /dev/null +++ b/src/organ/dto/update-organ.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/swagger'; +import { CreateOrganDto } from './create-organ.dto'; + +export class UpdateOrganDto extends PartialType(CreateOrganDto) {} diff --git a/src/organ/entities/organ.entity.ts b/src/organ/entities/organ.entity.ts new file mode 100644 index 0000000..0446cda --- /dev/null +++ b/src/organ/entities/organ.entity.ts @@ -0,0 +1,16 @@ +import { BaseEntity } from 'src/common/entities/base.entity'; +import { Column, Entity } from 'typeorm'; + +@Entity('organs') +export class Organ extends BaseEntity { + @Column({ + type: 'varchar', + length: 50, + }) + name: string; + + @Column({ + type: 'varchar', + }) + logo: string; +} diff --git a/src/organ/organ.controller.ts b/src/organ/organ.controller.ts new file mode 100644 index 0000000..6334ca4 --- /dev/null +++ b/src/organ/organ.controller.ts @@ -0,0 +1,34 @@ +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { OrganService } from './organ.service'; +import { CreateOrganDto } from './dto/create-organ.dto'; +import { UpdateOrganDto } from './dto/update-organ.dto'; + +@Controller('organ') +export class OrganController { + constructor(private readonly organService: OrganService) {} + + @Post() + create(@Body() createOrganDto: CreateOrganDto) { + return this.organService.create(createOrganDto); + } + + @Get() + findAll() { + return this.organService.findAll(); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.organService.findOneOrFail(id); + } + + @Patch(':id') + update(@Param('id') id: string, @Body() updateOrganDto: UpdateOrganDto) { + return this.organService.update(id, updateOrganDto); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.organService.remove(id); + } +} diff --git a/src/organ/organ.module.ts b/src/organ/organ.module.ts new file mode 100644 index 0000000..6ae4f55 --- /dev/null +++ b/src/organ/organ.module.ts @@ -0,0 +1,15 @@ +import { Module } from '@nestjs/common'; +import { OrganService } from './organ.service'; +import { OrganController } from './organ.controller'; +import { OrganRepository } from './repositories/organ.repository'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Organ } from './entities/organ.entity'; + +@Module({ + imports: [ + TypeOrmModule.forFeature([Organ]) + ], + controllers: [OrganController], + providers: [OrganService, OrganRepository], +}) +export class OrganModule {} diff --git a/src/organ/organ.service.ts b/src/organ/organ.service.ts new file mode 100644 index 0000000..f27f215 --- /dev/null +++ b/src/organ/organ.service.ts @@ -0,0 +1,39 @@ +import { Injectable, NotFoundException } from '@nestjs/common'; +import { CreateOrganDto } from './dto/create-organ.dto'; +import { UpdateOrganDto } from './dto/update-organ.dto'; +import { OrganRepository } from './repositories/organ.repository'; +import { OrganMessages } from 'src/common/enums/messages.enum'; +import { Organ } from './entities/organ.entity'; + +@Injectable() +export class OrganService { + constructor(private readonly organRepository: OrganRepository) {} + + async create(createOrganDto: CreateOrganDto) { + const organ = this.organRepository.create(createOrganDto); + await this.organRepository.save(organ); + } + + async findAll(): Promise { + return await this.organRepository.find(); + } + + async findOneOrFail(id: string): Promise { + const organ = await this.organRepository.findOne({where: {id}}); + if(!organ) throw new NotFoundException(OrganMessages.ORGAN_NOT_FOUND); + + return organ; + } + + async update(id: string, updateOrganDto: UpdateOrganDto) { + const organ = await this.findOneOrFail(id); + + Object.assign(organ, updateOrganDto); + return await this.organRepository.save(organ); + } + + async remove(id: string) { + const organ = await this.findOneOrFail(id); + await this.organRepository.remove(organ); + } +} diff --git a/src/organ/repositories/organ.repository.ts b/src/organ/repositories/organ.repository.ts new file mode 100644 index 0000000..6256ce3 --- /dev/null +++ b/src/organ/repositories/organ.repository.ts @@ -0,0 +1,9 @@ +import { Repository } from "typeorm"; +import { Organ } from "../entities/organ.entity"; +import { InjectRepository } from "@nestjs/typeorm"; + +export class OrganRepository extends Repository { + constructor(@InjectRepository(Organ) organRepository: Repository) { + super(organRepository.target, organRepository.manager, organRepository.queryRunner); + } +} \ No newline at end of file