From 94dee4a933370cc2284c3bd7b0d6cb58c2884364 Mon Sep 17 00:00:00 2001 From: Yanick Minder Date: Tue, 28 Jan 2025 15:59:45 +0100 Subject: [PATCH] add submit functionality --- .../main/java/ch/puzzle/okr/models/Unit.java | 2 ++ .../V1_0_0__current-db-schema-for-testing.sql | 3 +- .../V3_4_0__updateStringUnitToEntity.sql | 3 +- .../manage-units-dialog.component.html | 5 +-- .../manage-units-dialog.component.ts | 35 +++++++++++++------ frontend/src/app/services/unit.service.ts | 4 +++ 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/backend/src/main/java/ch/puzzle/okr/models/Unit.java b/backend/src/main/java/ch/puzzle/okr/models/Unit.java index afde40cc98..4cef73fdd8 100644 --- a/backend/src/main/java/ch/puzzle/okr/models/Unit.java +++ b/backend/src/main/java/ch/puzzle/okr/models/Unit.java @@ -33,6 +33,8 @@ public int hashCode() { @NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL) @Size(max = 4096, min = 3, message = MessageKey.ATTRIBUTE_SIZE_BETWEEN) + @NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL) + @Column(nullable = false) private String unitName; @CreatedBy diff --git a/backend/src/main/resources/db/h2-db/database-h2-schema/V1_0_0__current-db-schema-for-testing.sql b/backend/src/main/resources/db/h2-db/database-h2-schema/V1_0_0__current-db-schema-for-testing.sql index fc1598cb89..09ab19c645 100644 --- a/backend/src/main/resources/db/h2-db/database-h2-schema/V1_0_0__current-db-schema-for-testing.sql +++ b/backend/src/main/resources/db/h2-db/database-h2-schema/V1_0_0__current-db-schema-for-testing.sql @@ -19,8 +19,7 @@ CREATE TABLE unit unit_name TEXT NOT NULL, created_by_id BIGINT, is_default boolean NOT NULL, - PRIMARY KEY (id), - UNIQUE (unit_name) + PRIMARY KEY (id) ); create table if not exists person diff --git a/backend/src/main/resources/db/migration/V3_4_0__updateStringUnitToEntity.sql b/backend/src/main/resources/db/migration/V3_4_0__updateStringUnitToEntity.sql index 17056c3d00..c24505098c 100644 --- a/backend/src/main/resources/db/migration/V3_4_0__updateStringUnitToEntity.sql +++ b/backend/src/main/resources/db/migration/V3_4_0__updateStringUnitToEntity.sql @@ -5,8 +5,7 @@ CREATE TABLE unit unit_name TEXT NOT NULL, is_default boolean NOT NULL, created_by_id BIGINT, - PRIMARY KEY (id), - UNIQUE (unit_name) + PRIMARY KEY (id) ); CREATE SEQUENCE sequence_unit START 1; diff --git a/frontend/src/app/components/manage-units-dialog/manage-units-dialog.component.html b/frontend/src/app/components/manage-units-dialog/manage-units-dialog.component.html index 15909eaaed..41b12759f2 100644 --- a/frontend/src/app/components/manage-units-dialog/manage-units-dialog.component.html +++ b/frontend/src/app/components/manage-units-dialog/manage-units-dialog.component.html @@ -1,4 +1,4 @@ - +
@@ -18,9 +18,10 @@ diff --git a/frontend/src/app/components/manage-units-dialog/manage-units-dialog.component.ts b/frontend/src/app/components/manage-units-dialog/manage-units-dialog.component.ts index 387faab49b..a04e7166b3 100644 --- a/frontend/src/app/components/manage-units-dialog/manage-units-dialog.component.ts +++ b/frontend/src/app/components/manage-units-dialog/manage-units-dialog.component.ts @@ -1,9 +1,10 @@ import { Component, OnInit } from '@angular/core'; import { UnitService } from '../../services/unit.service'; import { Unit } from '../../shared/types/enums/unit'; -import { map, Observable, ReplaySubject } from 'rxjs'; +import { forkJoin, map, Observable, ReplaySubject } from 'rxjs'; import { FormArray, FormGroup } from '@angular/forms'; import { FormControlsOf, Item } from '../action-plan/action-plan.component'; +import { MatDialogRef } from '@angular/material/dialog'; @Component({ selector: 'app-manage-units-dialog', @@ -20,17 +21,31 @@ export class ManageUnitsDialogComponent implements OnInit { unitFormArray: new FormArray>>([]) }); - constructor(private unitService: UnitService) { + constructor(private unitService: UnitService, private dialogRef: MatDialogRef) { } - saveUnits() { - /* - * this.unitService.checkForNewUnit(this.unitSearchTerm) - * .subscribe((result:Unit)=> this.unitService.createUnit(result) - * .subscribe((unit)=> this.keyResultForm.get('metric') - * ?.get('unit') - * ?.setValue(unit))); - */ + submit() { + const items = this.fg.get('unitFormArray')?.value as Item[]; + const units = items.map((i) => { + return { id: i.id, + unitName: i.item } as Unit; + }); + + forkJoin(this.getNewUnits(units) + .concat(this.getUpdatableUnits(units))) + .subscribe((complete) => { + this.dialogRef.close(); + }); + } + + getUpdatableUnits(units: Unit[]) { + return units.filter((u) => u.id) + .map((u) => this.unitService.updateUnit(u)); + } + + getNewUnits(units: Unit[]) { + return units.filter((u) => !u.id) + .map((u) => this.unitService.createUnit(u)); } ngOnInit(): void { diff --git a/frontend/src/app/services/unit.service.ts b/frontend/src/app/services/unit.service.ts index c07d00e58e..db28087744 100644 --- a/frontend/src/app/services/unit.service.ts +++ b/frontend/src/app/services/unit.service.ts @@ -24,6 +24,10 @@ export class UnitService { return this.httpClient.post(this.API_URL, unit); } + updateUnit(unit: Unit) { + return this.httpClient.put(this.API_URL + '/' + unit.id, unit); + } + checkForNewUnit(unitName: string): Observable { const newUnit = { unitName: unitName, isDefault: false } as Unit;