Skip to content

Commit

Permalink
add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jan 30, 2025
1 parent 8658c8d commit f31f734
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 6 deletions.
1 change: 1 addition & 0 deletions backend/src/main/java/ch/puzzle/okr/ErrorKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum ErrorKey {
ATTRIBUTE_NOT_SET,
ATTRIBUTE_CANNOT_CHANGE,
ATTRIBUTE_MUST_BE_DRAFT,
ATTRIBUTE_MUST_BE_UNIQUE,
KEY_RESULT_CONVERSION,
ALREADY_EXISTS_SAME_NAME,
CONVERT_TOKEN,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/ch/puzzle/okr/models/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public int hashCode() {
private int version;

@NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL)
@Size(max = 4096, min = 3, message = MessageKey.ATTRIBUTE_SIZE_BETWEEN)
@Size(max = 4096, min = 2, message = MessageKey.ATTRIBUTE_SIZE_BETWEEN)
@NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL)
@Column(nullable = false)
private String unitName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public interface UnitRepository extends CrudRepository<Unit, Long> {

List<Unit> findAllByCreatedById(Long userId);

boolean existsUnitByUnitName(String unitName);

boolean existsUnitByUnitNameAndIdNot(String unitName, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public Unit findUnitByUnitName(String unitName) {
List.of(Constants.UNIT, "unit name", unitName)));
}

public boolean existsUnitByUnitName(String unitName) {
return getRepository().existsUnitByUnitName(unitName);
}

public boolean existsUnitByUnitNameAndIdNot(String unitName, Long id) {
return getRepository().existsUnitByUnitNameAndIdNot(unitName, id);
}

public List<Unit> findUnitsByUser(Long userId) {
return getRepository().findAllByCreatedById(userId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package ch.puzzle.okr.service.validation;

import ch.puzzle.okr.ErrorKey;
import ch.puzzle.okr.exception.OkrResponseStatusException;
import ch.puzzle.okr.models.Unit;
import ch.puzzle.okr.repository.UnitRepository;
import ch.puzzle.okr.service.persistence.UnitPersistenceService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UnitValidationService extends ValidationBase<Unit, Long, UnitRepository, UnitPersistenceService> {

Expand All @@ -15,6 +21,7 @@ public UnitValidationService(UnitPersistenceService unitPersistenceService) {
@Override
public void validateOnCreate(Unit model) {
throwExceptionWhenModelIsNull(model);
throwErrorWhenUnitIsNotUnique(model);
validate(model);
}

Expand All @@ -23,6 +30,17 @@ public void validateOnUpdate(Long id, Unit model) {
throwExceptionWhenModelIsNull(model);
throwExceptionWhenIdIsNull(model.getId());
throwExceptionWhenIdHasChanged(id, model.getId());
if (!Objects.equals(this.getPersistenceService().findById(id).getUnitName(), model.getUnitName())) {
throwErrorWhenUnitIsNotUnique(model);
}
validate(model);
}

private void throwErrorWhenUnitIsNotUnique(Unit model) {
if (this.getPersistenceService().existsUnitByUnitName(model.getUnitName())) {
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST,
ErrorKey.ATTRIBUTE_MUST_BE_UNIQUE,
List.of("unitname",model.getUnitName(), this.getPersistenceService().getModelName()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,26 @@ export class ManageUnitsDialogComponent implements OnInit {
constructor(private unitService: UnitService, private dialogRef: MatDialogRef<ManageUnitsDialogComponent>, private unitPipe: UnitTransformationPipe) {
}

private getChangedItems() {
const itemControls = (this.fg.get('unitFormArray') as FormArray)?.controls as FormGroup<FormControlsOf<Item>>[];
return itemControls.filter((c) => c.dirty)
.map((c) => c.getRawValue() as Item);
}

submit() {
const items = this.fg.get('unitFormArray')?.value as Item[];
const items = this.getChangedItems();

const units = items.map((i) => {
return { id: i.id,
unitName: i.item } as Unit;
});

forkJoin(this.getNewUnits(units)
.concat(this.getUpdatableUnits(units)))
const allObservables = this.getNewUnits(units)
.concat(this.getUpdatableUnits(units));
if (allObservables.length === 0) {
this.dialogRef.close();
return;
}
forkJoin(allObservables)
.subscribe((complete) => {
this.dialogRef.close();
});
Expand Down
13 changes: 12 additions & 1 deletion frontend/src/app/shared/constant-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ export function getKeyResultForm(): FormGroup {
actionList: new FormArray<FormGroup<FormControlsOf<Item>>>([]),
keyResultType: new FormControl('metric'),
metric: new FormGroup({
unit: new FormControl<Unit>({ unitName: 'NUMBER' } as Unit, [Validators.required]),
unit: new FormControl<Unit>({ unitName: 'NUMBER' } as Unit, [Validators.required,
unitValidator()]),
baseline: new FormControl(0, [Validators.required,
numberValidator(),
Validators.maxLength(20)]),
Expand Down Expand Up @@ -136,6 +137,16 @@ function ownerValidator(): ValidatorFn {
};
}

function unitValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const unit = control.value as Unit;
if (unit?.unitName && unit.unitName.length > 3) {
return null;
}
return { invalid_unit: { value: control.value } };
};
}

export function numberValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const isAllowed = !Number.isNaN(+control.value);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"ATTRIBUTE_SIZE_BETWEEN": "Das Attribut {0} auf dem Objekt {1} muss folgende Länge haben: {2}-{3}.",
"ATTRIBUTE_SET_FORBIDDEN": "Das Attribut {0} darf nicht während des Erstellens gesetzt sein.",
"ATTRIBUTE_NOT_SET": "Das Attribut {0} ist nicht gesetzt.",
"ATTRIBUTE_MUST_BE_UNIQUE": "Das Attribut '{0}' mit dem Wert '{1}' auf dem Model {2} existiert bereits.",
"ATTRIBUTE_CANNOT_CHANGE": "Das Attribut {0} auf dem Objekt {1} kann nicht geändert werden.",
"ATTRIBUTE_MIN_VALUE": "Das Attribut {0} auf dem Objekt {1} muss mindestens einen Wert von {2} haben.",
"ATTRIBUTE_MAX_VALUE": "Das Attribut {0} auf dem Objekt {1} darf maximal einen Wert von {2} haben.",
Expand Down

0 comments on commit f31f734

Please sign in to comment.