Skip to content

Commit

Permalink
feat: Add possibility to update toolmodel name
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik003 committed Oct 9, 2023
1 parent 9821e51 commit cf6ff9b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
4 changes: 4 additions & 0 deletions backend/capellacollab/projects/toolmodels/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ def update_model(
db: orm.Session,
model: models.DatabaseCapellaModel,
description: str | None,
name: str | None,
version: tools_models.DatabaseVersion,
nature: tools_models.DatabaseNature,
) -> models.DatabaseCapellaModel:
model.version = version
model.nature = nature
if description:
model.description = description
if name:
model.name = name
model.slug = slugify.slugify(name)

Check warning on line 142 in backend/capellacollab/projects/toolmodels/crud.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/crud.py#L141-L142

Added lines #L141 - L142 were not covered by tests
db.commit()
return model

Expand Down
1 change: 1 addition & 0 deletions backend/capellacollab/projects/toolmodels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class PostCapellaModel(pydantic.BaseModel):


class PatchCapellaModel(pydantic.BaseModel):
name: str | None = None
description: str | None = None
version_id: int
nature_id: int
Expand Down
21 changes: 20 additions & 1 deletion backend/capellacollab/projects/toolmodels/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uuid

import fastapi
import slugify
from fastapi import status
from sqlalchemy import exc, orm

Expand Down Expand Up @@ -121,11 +122,27 @@ def create_new_tool_model(
)
def patch_tool_model(
body: models.PatchCapellaModel,
project: projects_models.DatabaseProject = fastapi.Depends(
projects_injectables.get_existing_project
),
model: models.DatabaseCapellaModel = fastapi.Depends(
injectables.get_existing_capella_model
),
db: orm.Session = fastapi.Depends(database.get_db),
) -> models.DatabaseCapellaModel:
if body.name:
new_slug = slugify.slugify(body.name)

Check warning on line 134 in backend/capellacollab/projects/toolmodels/routes.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/routes.py#L134

Added line #L134 was not covered by tests

if model.slug != new_slug and crud.get_model_by_slugs(
db, project.slug, new_slug
):
raise fastapi.HTTPException(

Check warning on line 139 in backend/capellacollab/projects/toolmodels/routes.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/routes.py#L139

Added line #L139 was not covered by tests
status_code=status.HTTP_409_CONFLICT,
detail={
"reason": "A model with a similar name already exists."
},
)

version = get_version_by_id_or_raise(db, body.version_id)
if version.tool != model.tool:
raise fastapi.HTTPException(
Expand All @@ -144,7 +161,9 @@ def patch_tool_model(
},
)

return crud.update_model(db, model, body.description, version, nature)
return crud.update_model(

Check warning on line 164 in backend/capellacollab/projects/toolmodels/routes.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/routes.py#L164

Added line #L164 was not covered by tests
db, model, body.description, body.name, version, nature
)


@router.delete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
<mat-card *ngIf="modelService.model$ | async" class="basis-1 grow">
<mat-card-title>{{ (modelService.model$ | async)!.name }}</mat-card-title>
<form (submit)="onSubmit()" [formGroup]="form">
<fieldset>
<mat-form-field appearance="fill">
<mat-label>Name</mat-label>
<input matInput formControlName="name" />

<mat-error *ngIf="form.controls.name.errors?.uniqueSlug"
>A model with a similar name already exists.</mat-error
>
</mat-form-field>
</fieldset>

<fieldset>
<mat-form-field appearance="fill">
<mat-label>Description</mat-label>
Expand Down Expand Up @@ -62,7 +73,14 @@
Delete
</button>
</span>
<button mat-flat-button color="primary" type="submit">Submit</button>
<button
mat-flat-button
color="primary"
type="submit"
[disabled]="!form.valid"
>
Submit
</button>
</div>
</form>
</mat-card>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ProjectService } from '../../service/project.service';
})
export class ModelDescriptionComponent implements OnInit {
form = new FormGroup({
name: new FormControl<string>(''),
description: new FormControl<string>(''),
nature: new FormControl<number>(-1),
version: new FormControl<number>(-1),
Expand Down Expand Up @@ -57,7 +58,12 @@ export class ModelDescriptionComponent implements OnInit {
model.git_models.length || model.t4c_models.length
);

this.form.controls.name.setAsyncValidators(
this.modelService.asyncSlugValidator(model)
);

this.form.patchValue({
name: model.name,
description: model.description,
nature: model.nature?.id,
version: model.version?.id,
Expand All @@ -83,7 +89,8 @@ export class ModelDescriptionComponent implements OnInit {
onSubmit(): void {
if (this.form.value && this.modelSlug && this.projectSlug) {
this.modelService
.updateModelDescription(this.projectSlug!, this.modelSlug!, {
.updateModelDescription(this.projectSlug, this.modelSlug, {
name: this.form.value.name || undefined,
description: this.form.value.description || '',
nature_id: this.form.value.nature || undefined,
version_id: this.form.value.version || undefined,
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/app/projects/models/service/model.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ export class ModelService {
this._models.next(undefined);
}

asyncSlugValidator(): AsyncValidatorFn {
asyncSlugValidator(ignoreModel?: Model): AsyncValidatorFn {
const ignoreSlug = !!ignoreModel ? ignoreModel.slug : -1;
return (control: AbstractControl): Observable<ValidationErrors | null> => {
const modelSlug = slugify(control.value, { lower: true });
return this.models$.pipe(
take(1),
map((models) => {
return models?.find((model) => model.slug === modelSlug)
return models?.find(
(model) => model.slug === modelSlug && model.slug !== ignoreSlug
)
? { uniqueSlug: { value: modelSlug } }
: null;
})
Expand Down Expand Up @@ -168,6 +171,7 @@ export type Model = {
};

export type PatchModel = {
name?: string;
description?: string;
nature_id?: number;
version_id?: number;
Expand Down

0 comments on commit cf6ff9b

Please sign in to comment.