diff --git a/frontend/wendys-friends/src/app/app-routing.module.ts b/frontend/wendys-friends/src/app/app-routing.module.ts index 2a1bc74..76657df 100644 --- a/frontend/wendys-friends/src/app/app-routing.module.ts +++ b/frontend/wendys-friends/src/app/app-routing.module.ts @@ -7,13 +7,15 @@ 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'; +import { ListOwnersComponent } from './component/list-owners/list-owners.component'; const routes: Routes = [ {path: '', redirectTo: 'owner', pathMatch: 'full'}, - {path: 'owner', component: OwnerComponent}, + {path: 'owner', component: ListOwnersComponent}, {path: 'owner/add', component: AddOwnerComponent}, {path: 'owner/:id/edit', component: UpdateOwnerComponent}, + {path: 'owner/:id', component: OwnerComponent}, {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 e83ed1b..10a21e8 100644 --- a/frontend/wendys-friends/src/app/app.module.ts +++ b/frontend/wendys-friends/src/app/app.module.ts @@ -13,6 +13,7 @@ import { UpdateHorseComponent } from './component/update-horse/update-horse.comp 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'; +import { ListOwnersComponent } from './component/list-owners/list-owners.component'; @NgModule({ declarations: [ @@ -24,7 +25,8 @@ import { UpdateOwnerComponent } from './component/update-owner/update-owner.comp UpdateHorseComponent, ListHorsesComponent, AddOwnerComponent, - UpdateOwnerComponent + UpdateOwnerComponent, + ListOwnersComponent ], imports: [ BrowserModule, diff --git a/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.html b/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.html new file mode 100644 index 0000000..a210f6f --- /dev/null +++ b/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.html @@ -0,0 +1,38 @@ + + +
+

Horse Owners List

+
+
+
+ +
+ + +
+
+ + + + + + + + + + + + + + + +
#NameActions
{{ id + 1 }}{{ owner.name }} + + +
+
diff --git a/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.scss b/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.scss new file mode 100644 index 0000000..c7f82fa --- /dev/null +++ b/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.scss @@ -0,0 +1,8 @@ +.search-form{ + margin-top: 1%; + margin-bottom: 3%; +} + +.btn { + margin-left: 0.5%; +} diff --git a/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.ts b/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.ts new file mode 100644 index 0000000..2cc2926 --- /dev/null +++ b/frontend/wendys-friends/src/app/component/list-owners/list-owners.component.ts @@ -0,0 +1,88 @@ +import { Component, OnInit } from '@angular/core'; +import { Owner } from '../../dto/owner'; +import { OwnerService } from '../../service/owner.service'; +import { HttpParams } from '@angular/common/http'; + +@Component({ + selector: 'app-list-owners', + templateUrl: './list-owners.component.html', + styleUrls: ['./list-owners.component.scss'] +}) +export class ListOwnersComponent implements OnInit { + error = false; + errorMessage = ''; + owners: Array; + filters: { + name: string + }; + + constructor(private ownerService: OwnerService) { } + + ngOnInit(): void { + // Nullify queries + this.filters = { + name: "" + }; + this.loadAllOwners(); + } + + /** + * Error flag will be deactivated, which clears the error message + */ + vanishError() { + this.error = false; + } + + /** + * Loads all owners from the database + */ + private loadAllOwners() { + console.log("GET all owners from API"); + this.ownerService.getAllOwners().subscribe( + (owners: Array) => { + this.owners = owners; + }, + error => { + this.defaultServiceErrorHandling(error); + } + ); + } + + /** + * Load filtered owners from the database + */ + public loadFilteredOwners() { + console.log("GET filtered owners from API"); + + console.log(this.filters); + // Create HttpParams for the sorting + let params: HttpParams = new HttpParams(); + if(this.filters.name != null && this.filters.name.length !== 0) + params = params.set('name', this.filters.name); + + this.ownerService.getFilteredOwners(params).subscribe( + (owners: Array) => { + this.owners = owners; + }, + error => { + this.owners = null; + 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 efd3a1b..08091ec 100644 --- a/frontend/wendys-friends/src/app/service/owner.service.ts +++ b/frontend/wendys-friends/src/app/service/owner.service.ts @@ -1,5 +1,5 @@ import {Injectable} from '@angular/core'; -import {HttpClient} from '@angular/common/http'; +import {HttpClient, HttpParams} from '@angular/common/http'; import {Globals} from '../global/globals'; import {Observable} from 'rxjs'; import {Owner} from '../dto/owner'; @@ -23,6 +23,23 @@ export class OwnerService { return this.httpClient.get(this.messageBaseUri + '/' + id); } + /** + * Loads all owners from the backend + */ + getAllOwners(): Observable> { + console.log('Load all owners'); + return this.httpClient.get>(this.messageBaseUri); + } + + /** + * Load all owners from the backend that correspond to a filter + * @param filter to use + */ + getFilteredOwners(filter: HttpParams): Observable> { + console.log('Load all filtered owners'); + return this.httpClient.get>(this.messageBaseUri, { params: filter }); + } + /** * Adds a specific owner to the backend and loads it if successful * @param owner