From d4f1fbbf393426030f84023c56b33eb0ed7939a8 Mon Sep 17 00:00:00 2001 From: Bastian Wagner Date: Thu, 24 Oct 2024 16:57:30 +0200 Subject: [PATCH] Zylinder angefangen --- api/src/app.module.ts | 2 + api/src/model/entitites/cylinder.entity.ts | 14 ++++++ api/src/model/repositories/role.repository.ts | 2 +- .../modules/cylinder/cylinder.controller.ts | 21 ++++++++ api/src/modules/cylinder/cylinder.module.ts | 12 +++++ api/src/modules/cylinder/cylinder.service.ts | 30 +++++++++++ api/src/modules/key/key.controller.ts | 5 -- api/src/modules/key/key.service.ts | 23 +-------- client/src/app/app.routes.ts | 4 +- .../src/app/core/layout/layout.component.html | 1 + .../modules/cylinder/cylinder.component.html | 5 ++ .../modules/cylinder/cylinder.component.scss | 0 .../cylinder/cylinder.component.spec.ts | 23 +++++++++ .../modules/cylinder/cylinder.component.ts | 50 +++++++++++++++++++ client/src/app/shared/api.service.ts | 8 +-- 15 files changed, 167 insertions(+), 33 deletions(-) create mode 100644 api/src/modules/cylinder/cylinder.controller.ts create mode 100644 api/src/modules/cylinder/cylinder.module.ts create mode 100644 api/src/modules/cylinder/cylinder.service.ts create mode 100644 client/src/app/modules/cylinder/cylinder.component.html create mode 100644 client/src/app/modules/cylinder/cylinder.component.scss create mode 100644 client/src/app/modules/cylinder/cylinder.component.spec.ts create mode 100644 client/src/app/modules/cylinder/cylinder.component.ts diff --git a/api/src/app.module.ts b/api/src/app.module.ts index 35d7a40..1588b13 100644 --- a/api/src/app.module.ts +++ b/api/src/app.module.ts @@ -9,6 +9,7 @@ import { UserModule } from './modules/user/user.module'; import { RoleModule } from './modules/role/role.module'; import { KeyModule } from './modules/key/key.module'; import { CustomerModule } from './modules/customer/customer.module'; +import { CylinderModule } from './modules/cylinder/cylinder.module'; @Module({ imports: [ @@ -22,6 +23,7 @@ import { CustomerModule } from './modules/customer/customer.module'; RoleModule, KeyModule, CustomerModule, + CylinderModule, ], controllers: [AppController], providers: [AppService, AuthGuard], diff --git a/api/src/model/entitites/cylinder.entity.ts b/api/src/model/entitites/cylinder.entity.ts index b7881be..063dc97 100644 --- a/api/src/model/entitites/cylinder.entity.ts +++ b/api/src/model/entitites/cylinder.entity.ts @@ -1,6 +1,8 @@ import { + AfterLoad, Column, CreateDateColumn, + DeleteDateColumn, Entity, ManyToOne, OneToMany, @@ -29,4 +31,16 @@ export class Cylinder { @UpdateDateColumn({ name: 'updatet_at' }) updatedAt: Date; + + @DeleteDateColumn() + deletedAt: Date; + + keyCount: number; + + @AfterLoad() + countKeys() { + if (this.keys) { + this.keyCount = this.keys.length; + } + } } diff --git a/api/src/model/repositories/role.repository.ts b/api/src/model/repositories/role.repository.ts index cc9b9d4..8f8007e 100644 --- a/api/src/model/repositories/role.repository.ts +++ b/api/src/model/repositories/role.repository.ts @@ -9,6 +9,6 @@ export class RoleRepository extends Repository { } getStandardRole(): Promise { - return this.findOne({ where: { name: 'develop' } }); + return this.findOne({ where: { name: 'user' } }); } } diff --git a/api/src/modules/cylinder/cylinder.controller.ts b/api/src/modules/cylinder/cylinder.controller.ts new file mode 100644 index 0000000..d4ae41c --- /dev/null +++ b/api/src/modules/cylinder/cylinder.controller.ts @@ -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 { + return this.service.getCylinders(req.user); + } + + @Delete(':id') + deleteKey(@Req() req: AuthenticatedRequest, @Param() id: string) { + return this.service.deleteKey(req.user, id); + } +} diff --git a/api/src/modules/cylinder/cylinder.module.ts b/api/src/modules/cylinder/cylinder.module.ts new file mode 100644 index 0000000..7883dc8 --- /dev/null +++ b/api/src/modules/cylinder/cylinder.module.ts @@ -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 {} diff --git a/api/src/modules/cylinder/cylinder.service.ts b/api/src/modules/cylinder/cylinder.service.ts new file mode 100644 index 0000000..20903f8 --- /dev/null +++ b/api/src/modules/cylinder/cylinder.service.ts @@ -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 { + 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); + } +} diff --git a/api/src/modules/key/key.controller.ts b/api/src/modules/key/key.controller.ts index 0bbea48..3da25ec 100644 --- a/api/src/modules/key/key.controller.ts +++ b/api/src/modules/key/key.controller.ts @@ -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, diff --git a/api/src/modules/key/key.service.ts b/api/src/modules/key/key.service.ts index bcd7d96..d3678ac 100644 --- a/api/src/modules/key/key.service.ts +++ b/api/src/modules/key/key.service.ts @@ -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 { return this.keyrepository.find({ diff --git a/client/src/app/app.routes.ts b/client/src/app/app.routes.ts index 6ff15c0..2eef6e2 100644 --- a/client/src/app/app.routes.ts +++ b/client/src/app/app.routes.ts @@ -7,12 +7,14 @@ import { LayoutComponent } from './core/layout/layout.component'; import { DashboardComponent } from './modules/dashboard/dashboard.component'; import { AllUsersComponent } from './modules/admin/all-users/all-users.component'; import { KeysComponent } from './modules/keys/keys.component'; +import { CylinderComponent } from './modules/cylinder/cylinder.component'; export const routes: Routes = [ { path: '', component: LayoutComponent, canActivate: [AuthenticatedGuard], children: [ { path: '', component: DashboardComponent }, { path: 'users', component: AllUsersComponent }, - { path: 'keys', component: KeysComponent } + { path: 'keys', component: KeysComponent }, + { path: 'cylinders', component: CylinderComponent } ]}, { path: 'login', component: LoginComponent}, ]; diff --git a/client/src/app/core/layout/layout.component.html b/client/src/app/core/layout/layout.component.html index 8a6d4ca..c53b9fe 100644 --- a/client/src/app/core/layout/layout.component.html +++ b/client/src/app/core/layout/layout.component.html @@ -14,6 +14,7 @@ + diff --git a/client/src/app/modules/cylinder/cylinder.component.html b/client/src/app/modules/cylinder/cylinder.component.html new file mode 100644 index 0000000..112977c --- /dev/null +++ b/client/src/app/modules/cylinder/cylinder.component.html @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/client/src/app/modules/cylinder/cylinder.component.scss b/client/src/app/modules/cylinder/cylinder.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/client/src/app/modules/cylinder/cylinder.component.spec.ts b/client/src/app/modules/cylinder/cylinder.component.spec.ts new file mode 100644 index 0000000..f51bcc9 --- /dev/null +++ b/client/src/app/modules/cylinder/cylinder.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { CylinderComponent } from './cylinder.component'; + +describe('CylinderComponent', () => { + let component: CylinderComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [CylinderComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(CylinderComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/client/src/app/modules/cylinder/cylinder.component.ts b/client/src/app/modules/cylinder/cylinder.component.ts new file mode 100644 index 0000000..5ba398d --- /dev/null +++ b/client/src/app/modules/cylinder/cylinder.component.ts @@ -0,0 +1,50 @@ +import { Component, inject } from '@angular/core'; +import { HELPER } from '../../shared/helper.service'; +import { GridApi, GridOptions, GridReadyEvent } from 'ag-grid-community'; +import { AgGridAngular } from 'ag-grid-angular'; +import { ApiService } from '../../shared/api.service'; +import { DatePipe } from '@angular/common'; + +@Component({ + selector: 'app-cylinder', + standalone: true, + imports: [AgGridAngular], + providers: [DatePipe], + templateUrl: './cylinder.component.html', + styleUrl: './cylinder.component.scss' +}) +export class CylinderComponent { + private api: ApiService = inject(ApiService); + private datePipe = inject(DatePipe); + + gridApi!: GridApi; + + gridOptions: GridOptions = HELPER.getGridOptions(); + + + constructor() { + + this.gridOptions.columnDefs = [ + { field: 'name', headerName: 'Name', sort: 'asc', flex: 1 }, + { field: 'system.name', headerName: 'System', flex: 1 }, + { field: 'keyCount', headerName: 'Anzahl Schlüssel', flex: 0, type: 'number' }, + { field: 'createdAt', headerName: 'Angelegt', cellRenderer: (data: any) => data.value ? this.datePipe.transform(new Date(data.value)) : '-' }, + { field: 'updatedAt', headerName: 'Upgedated', cellRenderer: (data: any) => data.value ? this.datePipe.transform(new Date(data.value)) : '-' }, + ] + + } + + loadCylinders() { + this.api.getCylinders().subscribe({ + next: n => { + this.gridApi.setGridOption("rowData", n); + this.gridApi.setGridOption("loading", false); + } + }) + } + + onGridReady(params: GridReadyEvent) { + this.gridApi = params.api; + this.loadCylinders(); + } +} diff --git a/client/src/app/shared/api.service.ts b/client/src/app/shared/api.service.ts index 312a9b6..4868d1d 100644 --- a/client/src/app/shared/api.service.ts +++ b/client/src/app/shared/api.service.ts @@ -33,10 +33,6 @@ export class ApiService { return this.http.put('api/key', key); } - getCylinders(): Observable { - return this.http.get('api/key/cylinder'); - } - createKey(key: any) { return this.http.post('api/key', key); } @@ -72,4 +68,8 @@ export class ApiService { restoreKey(id: string) { return this.http.put(`api/key/${id}/restore`, null); } + + getCylinders(): Observable { + return this.http.get('api/cylinder'); + } }