feat(environment): add environment CRUD module and wire into auth login
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsObject, IsString } from 'class-validator';
|
||||
import { EnvironmentUrls } from '../environment.entity';
|
||||
|
||||
export class CreateEnvironmentDto {
|
||||
@ApiProperty({ example: 'liquio-diia-stg' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Map of URL identifiers to URL strings',
|
||||
example: {
|
||||
id_url: 'https://id-liquio-diia-stg.kitsoft.ua/',
|
||||
cabinet_url: 'https://cabinet-liquio-diia-stg.kitsoft.ua/',
|
||||
admin_url: 'https://admin-liquio-diia-stg.kitsoft.ua/',
|
||||
},
|
||||
})
|
||||
@IsObject()
|
||||
urls: EnvironmentUrls;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateEnvironmentDto } from './create-environment.dto';
|
||||
|
||||
export class UpdateEnvironmentDto extends PartialType(CreateEnvironmentDto) {}
|
||||
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
HttpCode,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Patch,
|
||||
Post,
|
||||
} from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { EnvironmentService } from './environment.service';
|
||||
import { CreateEnvironmentDto } from './dto/create-environment.dto';
|
||||
import { UpdateEnvironmentDto } from './dto/update-environment.dto';
|
||||
|
||||
@ApiTags('environments')
|
||||
@Controller('environments')
|
||||
export class EnvironmentController {
|
||||
constructor(private readonly environmentService: EnvironmentService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create a new environment' })
|
||||
@ApiResponse({ status: 201, description: 'Environment created' })
|
||||
@ApiResponse({ status: 409, description: 'Environment name already exists' })
|
||||
create(@Body() dto: CreateEnvironmentDto) {
|
||||
return this.environmentService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all environments' })
|
||||
@ApiResponse({ status: 200, description: 'Array of environments' })
|
||||
findAll() {
|
||||
return this.environmentService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get environment by ID' })
|
||||
@ApiResponse({ status: 200, description: 'Environment record' })
|
||||
@ApiResponse({ status: 404, description: 'Environment not found' })
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.environmentService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Update an environment' })
|
||||
@ApiResponse({ status: 200, description: 'Environment updated' })
|
||||
@ApiResponse({ status: 404, description: 'Environment not found' })
|
||||
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateEnvironmentDto) {
|
||||
return this.environmentService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(204)
|
||||
@ApiOperation({ summary: 'Delete an environment' })
|
||||
@ApiResponse({ status: 204, description: 'Environment deleted' })
|
||||
@ApiResponse({ status: 404, description: 'Environment not found' })
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.environmentService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
export interface EnvironmentUrls {
|
||||
id_url?: string;
|
||||
cabinet_url?: string;
|
||||
admin_url?: string;
|
||||
[key: string]: string | undefined;
|
||||
}
|
||||
|
||||
@Entity('environments')
|
||||
export class EnvironmentEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
|
||||
@Column('simple-json')
|
||||
urls: EnvironmentUrls;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { EnvironmentEntity } from './environment.entity';
|
||||
import { EnvironmentService } from './environment.service';
|
||||
import { EnvironmentController } from './environment.controller';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([EnvironmentEntity])],
|
||||
controllers: [EnvironmentController],
|
||||
providers: [EnvironmentService],
|
||||
exports: [EnvironmentService],
|
||||
})
|
||||
export class EnvironmentModule {}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { EnvironmentEntity } from './environment.entity';
|
||||
import { CreateEnvironmentDto } from './dto/create-environment.dto';
|
||||
import { UpdateEnvironmentDto } from './dto/update-environment.dto';
|
||||
|
||||
@Injectable()
|
||||
export class EnvironmentService {
|
||||
constructor(
|
||||
@InjectRepository(EnvironmentEntity)
|
||||
private readonly repo: Repository<EnvironmentEntity>,
|
||||
) {}
|
||||
|
||||
async create(dto: CreateEnvironmentDto): Promise<EnvironmentEntity> {
|
||||
const existing = await this.repo.findOneBy({ name: dto.name });
|
||||
if (existing) {
|
||||
throw new ConflictException(`Environment "${dto.name}" already exists`);
|
||||
}
|
||||
return this.repo.save(this.repo.create(dto));
|
||||
}
|
||||
|
||||
findAll(): Promise<EnvironmentEntity[]> {
|
||||
return this.repo.find();
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<EnvironmentEntity> {
|
||||
const env = await this.repo.findOneBy({ id });
|
||||
if (!env) throw new NotFoundException(`Environment ${id} not found`);
|
||||
return env;
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateEnvironmentDto): Promise<EnvironmentEntity> {
|
||||
const env = await this.findOne(id);
|
||||
Object.assign(env, dto);
|
||||
return this.repo.save(env);
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.repo.delete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user