feat: added organ
This commit is contained in:
+3
-2
@@ -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 {}
|
||||
|
||||
@@ -18,4 +18,8 @@ export enum UserRoleMessages {
|
||||
|
||||
export enum AuthMessages {
|
||||
TOKEN_EXPIRED_OR_INVALID = 'توکن منسوخ شده یا نامعتبر است!',
|
||||
}
|
||||
|
||||
export enum OrganMessages {
|
||||
ORGAN_NOT_FOUND = 'ارگان مورد نظر پیدا نشد!'
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateOrganDto } from './create-organ.dto';
|
||||
|
||||
export class UpdateOrganDto extends PartialType(CreateOrganDto) {}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
@@ -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<Organ[]> {
|
||||
return await this.organRepository.find();
|
||||
}
|
||||
|
||||
async findOneOrFail(id: string): Promise<Organ> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Repository } from "typeorm";
|
||||
import { Organ } from "../entities/organ.entity";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
|
||||
export class OrganRepository extends Repository<Organ> {
|
||||
constructor(@InjectRepository(Organ) organRepository: Repository<Organ>) {
|
||||
super(organRepository.target, organRepository.manager, organRepository.queryRunner);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user