This commit is contained in:
Bastian Wagner
2024-10-25 12:32:26 +02:00
parent d4f1fbbf39
commit b4e264eda9
40 changed files with 538 additions and 66 deletions

View File

@@ -0,0 +1,8 @@
import { IsNotEmpty, MaxLength, MinLength } from 'class-validator';
export class CreateSystemDto {
@IsNotEmpty()
@MinLength(2)
@MaxLength(255)
name: string;
}

View File

@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateSystemDto } from './create-system.dto';
export class UpdateSystemDto extends PartialType(CreateSystemDto) {}

View File

@@ -0,0 +1,50 @@
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Req,
UseGuards,
} from '@nestjs/common';
import { SystemService } from './system.service';
import { CreateSystemDto } from './dto/create-system.dto';
import { UpdateSystemDto } from './dto/update-system.dto';
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
import { AuthGuard } from 'src/core/guards/auth.guard';
@UseGuards(AuthGuard)
@Controller('system')
export class SystemController {
constructor(private readonly systemService: SystemService) {}
@Post()
create(
@Req() req: AuthenticatedRequest,
@Body() createSystemDto: CreateSystemDto,
) {
return this.systemService.create(req.user, createSystemDto);
}
@Get()
findAll(@Req() req: AuthenticatedRequest) {
return this.systemService.findAll(req.user);
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.systemService.findOne(id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateSystemDto: UpdateSystemDto) {
return this.systemService.update(id, updateSystemDto);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.systemService.remove(id);
}
}

View File

@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { SystemService } from './system.service';
import { SystemController } from './system.controller';
import { AuthModule } from '../auth/auth.module';
import { DatabaseModule } from 'src/shared/database/database.module';
@Module({
controllers: [SystemController],
providers: [SystemService],
imports: [AuthModule, DatabaseModule],
})
export class SystemModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { SystemService } from './system.service';
describe('SystemService', () => {
let service: SystemService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [SystemService],
}).compile();
service = module.get<SystemService>(SystemService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -0,0 +1,42 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSystemDto } from './dto/create-system.dto';
import { UpdateSystemDto } from './dto/update-system.dto';
import { KeySystemRepository } from 'src/model/repositories';
import { User } from 'src/model/entitites';
@Injectable()
export class SystemService {
constructor(private systemRepo: KeySystemRepository) {}
async create(user: User, createSystemDto: CreateSystemDto) {
const sys = this.systemRepo.create(createSystemDto);
sys.managers = [user];
try {
const res = await this.systemRepo.save(sys);
return res;
} catch (e) {
throw new HttpException(e.code, HttpStatus.UNPROCESSABLE_ENTITY);
}
}
findAll(user: User) {
return this.systemRepo.find({
where: { managers: { id: user.id } },
order: { name: { direction: 'ASC' } },
});
}
findOne(id: string) {
return `This action returns a #${id} system`;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
update(id: string, updateSystemDto: UpdateSystemDto) {
return `This action updates a #${id} system`;
}
async remove(id: string) {
const system = await this.systemRepo.findOne({ where: { id } });
return this.systemRepo.softRemove(system);
}
}