From 9c36a93dd5f9d8455a70f66fb83fdace0ed207f2 Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Tue, 24 Mar 2020 15:25:09 +0100 Subject: [PATCH] US07: Make it possible to edit owners through the UI --- .../src/app/app-routing.module.ts | 2 + frontend/wendys-friends/src/app/app.module.ts | 4 +- .../update-owner/update-owner.component.html | 26 +++++++ .../update-owner/update-owner.component.scss | 0 .../update-owner/update-owner.component.ts | 78 +++++++++++++++++++ .../src/app/service/owner.service.ts | 9 +++ 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 frontend/wendys-friends/src/app/component/update-owner/update-owner.component.html create mode 100644 frontend/wendys-friends/src/app/component/update-owner/update-owner.component.scss create mode 100644 frontend/wendys-friends/src/app/component/update-owner/update-owner.component.ts diff --git a/frontend/wendys-friends/src/app/app-routing.module.ts b/frontend/wendys-friends/src/app/app-routing.module.ts index a553566..2a1bc74 100644 --- a/frontend/wendys-friends/src/app/app-routing.module.ts +++ b/frontend/wendys-friends/src/app/app-routing.module.ts @@ -6,12 +6,14 @@ import { ListHorsesComponent } from './component/list-horses/list-horses.compone import { AddHorseComponent } from './component/add-horse/add-horse.component'; import { UpdateHorseComponent } from './component/update-horse/update-horse.component'; import { AddOwnerComponent } from './component/add-owner/add-owner.component'; +import { UpdateOwnerComponent } from './component/update-owner/update-owner.component'; const routes: Routes = [ {path: '', redirectTo: 'owner', pathMatch: 'full'}, {path: 'owner', component: OwnerComponent}, {path: 'owner/add', component: AddOwnerComponent}, + {path: 'owner/:id/edit', component: UpdateOwnerComponent}, {path: 'horse', component: ListHorsesComponent}, {path: 'horse/add', component: AddHorseComponent}, {path: 'horse/:id/edit', component: UpdateHorseComponent}, diff --git a/frontend/wendys-friends/src/app/app.module.ts b/frontend/wendys-friends/src/app/app.module.ts index e7ca9c6..e83ed1b 100644 --- a/frontend/wendys-friends/src/app/app.module.ts +++ b/frontend/wendys-friends/src/app/app.module.ts @@ -12,6 +12,7 @@ import { AddHorseComponent } from './component/add-horse/add-horse.component'; import { UpdateHorseComponent } from './component/update-horse/update-horse.component'; import { ListHorsesComponent } from './component/list-horses/list-horses.component'; import { AddOwnerComponent } from './component/add-owner/add-owner.component'; +import { UpdateOwnerComponent } from './component/update-owner/update-owner.component'; @NgModule({ declarations: [ @@ -22,7 +23,8 @@ import { AddOwnerComponent } from './component/add-owner/add-owner.component'; AddHorseComponent, UpdateHorseComponent, ListHorsesComponent, - AddOwnerComponent + AddOwnerComponent, + UpdateOwnerComponent ], imports: [ BrowserModule, diff --git a/frontend/wendys-friends/src/app/component/update-owner/update-owner.component.html b/frontend/wendys-friends/src/app/component/update-owner/update-owner.component.html new file mode 100644 index 0000000..39b6f41 --- /dev/null +++ b/frontend/wendys-friends/src/app/component/update-owner/update-owner.component.html @@ -0,0 +1,26 @@ + + + + +
+

Update owner

+
+
+ + +
+ + + Cancel +
+
\ No newline at end of file diff --git a/frontend/wendys-friends/src/app/component/update-owner/update-owner.component.scss b/frontend/wendys-friends/src/app/component/update-owner/update-owner.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/frontend/wendys-friends/src/app/component/update-owner/update-owner.component.ts b/frontend/wendys-friends/src/app/component/update-owner/update-owner.component.ts new file mode 100644 index 0000000..4638d76 --- /dev/null +++ b/frontend/wendys-friends/src/app/component/update-owner/update-owner.component.ts @@ -0,0 +1,78 @@ +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute, Params} from '@angular/router'; +import { Owner } from '../../dto/owner'; +import { OwnerService } from '../../service/owner.service'; + +@Component({ + selector: 'app-update-owner', + templateUrl: './update-owner.component.html', + styleUrls: ['./update-owner.component.scss'] +}) +export class UpdateOwnerComponent implements OnInit { + error = false; + success: string; + errorMessage = ''; + owner: Owner = new Owner(null, null); + + constructor(private ownerService: OwnerService, private route: ActivatedRoute) { } + + ngOnInit(): void { + // Extract id from url + const ownerId: string = this.route.snapshot.paramMap.get('id'); + + // Get current value of the owner and save it + this.ownerService.getOwnerById(parseInt(ownerId, 10)).subscribe( + (owner: Owner) => { + this.owner = owner; + }, + error => { + this.defaultServiceErrorHandling(error); + } + ); + } + + /** + * Will be called on a click on the success alert close button + */ + public vanishAlert() { + this.success = null; + } + + /** + * Will be called on a click on the error alert close button + */ + public vanishError() { + this.errorMessage = null; + } + + /** + * Updates a owner and loads it + * @param owner + */ + public updateOwner() { + console.log("PUT owner to API") + console.log(this.owner); + this.ownerService.updateOwner(this.owner).subscribe( + (result: Owner) => { + this.success = result.name; + }, + error => { + this.defaultServiceErrorHandling(error); + } + ); + } + + private defaultServiceErrorHandling(error: any) { + console.log(error); + this.error = true; + if (error.status === 0) { + // If status is 0, the backend is probably down + this.errorMessage = 'The backend seems not to be reachable'; + } else if (error.error.message === 'No message available') { + // If no detailed error message is provided, fall back to the simple error name + this.errorMessage = error.error.error; + } else { + this.errorMessage = error.error.message; + } + } +} diff --git a/frontend/wendys-friends/src/app/service/owner.service.ts b/frontend/wendys-friends/src/app/service/owner.service.ts index c8ee159..efd3a1b 100644 --- a/frontend/wendys-friends/src/app/service/owner.service.ts +++ b/frontend/wendys-friends/src/app/service/owner.service.ts @@ -31,4 +31,13 @@ export class OwnerService { console.log('Add new owner ' + JSON.stringify(owner)); return this.httpClient.post(this.messageBaseUri, owner); } + + /** + * Update a specific owner with the supplied values + * @param owner + */ + updateOwner(owner: Owner): Observable { + console.log('Update owner with id ' + owner.id + ': '+ JSON.stringify(owner)); + return this.httpClient.put(this.messageBaseUri + '/' + owner.id, owner); + } }