Zylinder angefangen

This commit is contained in:
Bastian Wagner
2024-10-24 16:57:30 +02:00
parent 03ae75c83d
commit d4f1fbbf39
15 changed files with 167 additions and 33 deletions

View File

@@ -0,0 +1,21 @@
import { Controller, Delete, Get, Param, Req, UseGuards } from '@nestjs/common';
import { AuthGuard } from 'src/core/guards/auth.guard';
import { CylinderService } from './cylinder.service';
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
import { Cylinder } from 'src/model/entitites';
@UseGuards(AuthGuard)
@Controller('cylinder')
export class CylinderController {
constructor(private service: CylinderService) {}
@Get()
getCylinders(@Req() req: AuthenticatedRequest): Promise<Cylinder[]> {
return this.service.getCylinders(req.user);
}
@Delete(':id')
deleteKey(@Req() req: AuthenticatedRequest, @Param() id: string) {
return this.service.deleteKey(req.user, id);
}
}

View File

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

View File

@@ -0,0 +1,30 @@
import { Injectable } from '@nestjs/common';
import { Cylinder, User } from 'src/model/entitites';
import { CylinderRepository, KeyRepository } from 'src/model/repositories';
@Injectable()
export class CylinderService {
constructor(
private readonly cylinderRepo: CylinderRepository,
private readonly keyRepo: KeyRepository,
) {}
async getCylinders(user: User): Promise<Cylinder[]> {
const c = await this.cylinderRepo.find({
where: { system: { managers: { id: user.id } } },
order: { name: { direction: 'ASC' } },
relations: ['system', 'keys'],
});
c.map((cc) => (cc.keys = []));
return c;
}
async deleteKey(user: User, id: string) {
const cylinder = await this.cylinderRepo.findOneOrFail({
where: { id: id, system: { managers: { id: user.id } } },
relations: ['keys'],
});
await this.keyRepo.softRemove(cylinder.keys);
return this.cylinderRepo.softRemove(cylinder);
}
}

View File

@@ -45,11 +45,6 @@ export class KeyController {
return this.service.deleteKey(req.user, id);
}
@Get('cylinder')
getCylinders(@Req() req: AuthenticatedRequest) {
return this.service.getUsersCylinders(req.user);
}
@Post('system')
createKeySystem(
@Req() req: AuthenticatedRequest,

View File

@@ -10,7 +10,6 @@ import {
} from 'src/model/repositories';
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
import { IsNull, Not } from 'typeorm';
import { faker } from '@faker-js/faker';
@Injectable()
export class KeyService {
@@ -20,27 +19,7 @@ export class KeyService {
private readonly systemRepo: KeySystemRepository,
private activityRepo: KeyActivityRepository,
private handoverRepo: KeyHandoutRepository,
) {
this.create()
}
async create() {
const c = await this.cylinderRepository.findOneBy({ name: 'DevCylinder1' });
const keys = [];
for (let x = 0; x < 1000; x++) {
keys.push(
this.keyrepository.create({
name: faker.commerce.productName(),
nr: faker.commerce.isbn(),
cylinder: c,
createdAt: new Date(faker.date.past())
})
);
}
await this.keyrepository.save(keys);
console.log("edn")
}
) {}
async getUsersKeys(user: User): Promise<Key[]> {
return this.keyrepository.find({