diff --git a/frontend/wendys-friends/src/app/app-routing.module.ts b/frontend/wendys-friends/src/app/app-routing.module.ts index ac49a94..d46925c 100644 --- a/frontend/wendys-friends/src/app/app-routing.module.ts +++ b/frontend/wendys-friends/src/app/app-routing.module.ts @@ -2,6 +2,7 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { OwnerComponent } from './component/owner/owner.component'; import { HorseComponent } from './component/horse/horse.component'; +import { ListHorsesComponent } from './component/list-horses/list-horses.component' import { AddHorseComponent } from './component/add-horse/add-horse.component'; import { UpdateHorseComponent } from './component/update-horse/update-horse.component'; @@ -9,9 +10,10 @@ import { UpdateHorseComponent } from './component/update-horse/update-horse.comp const routes: Routes = [ {path: '', redirectTo: 'owner', pathMatch: 'full'}, {path: 'owner', component: OwnerComponent}, - {path: 'horse', component: HorseComponent}, + {path: 'horse', component: ListHorsesComponent}, {path: 'horse/add', component: AddHorseComponent}, {path: 'horse/:id/edit', component: UpdateHorseComponent}, + {path: 'horse/:id', component: HorseComponent}, ]; @NgModule({ diff --git a/frontend/wendys-friends/src/app/app.module.ts b/frontend/wendys-friends/src/app/app.module.ts index 47b5baf..ade575d 100644 --- a/frontend/wendys-friends/src/app/app.module.ts +++ b/frontend/wendys-friends/src/app/app.module.ts @@ -3,13 +3,14 @@ import {NgModule} from '@angular/core'; import { FormsModule } from '@angular/forms'; import {HttpClientModule} from '@angular/common/http'; -import {AppRoutingModule} from './app-routing.module'; -import {AppComponent} from './app.component'; -import {HeaderComponent} from './component/header/header.component'; -import {OwnerComponent} from './component/owner/owner.component'; +import { AppRoutingModule } from './app-routing.module'; +import { AppComponent } from './app.component'; +import { HeaderComponent } from './component/header/header.component'; +import { OwnerComponent } from './component/owner/owner.component'; import { HorseComponent } from './component/horse/horse.component'; 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'; @NgModule({ declarations: [ @@ -18,13 +19,14 @@ import { UpdateHorseComponent } from './component/update-horse/update-horse.comp OwnerComponent, HorseComponent, AddHorseComponent, - UpdateHorseComponent + UpdateHorseComponent, + ListHorsesComponent ], imports: [ BrowserModule, AppRoutingModule, HttpClientModule, - FormsModule + FormsModule, ], providers: [], bootstrap: [AppComponent] diff --git a/frontend/wendys-friends/src/app/component/list-horses/list-horses.component.html b/frontend/wendys-friends/src/app/component/list-horses/list-horses.component.html new file mode 100644 index 0000000..3be9fda --- /dev/null +++ b/frontend/wendys-friends/src/app/component/list-horses/list-horses.component.html @@ -0,0 +1,79 @@ + + +
+

Horse List

+
+
+
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
#NameDescriptionScoreBirthdayRaceEditDelete
{{ id + 1 }}{{ horse.name }}{{ horse.description }}{{ horse.score }}{{ horse.birthday }}{{ horse.race }} + + + +
+
diff --git a/frontend/wendys-friends/src/app/component/list-horses/list-horses.component.scss b/frontend/wendys-friends/src/app/component/list-horses/list-horses.component.scss new file mode 100644 index 0000000..c7f82fa --- /dev/null +++ b/frontend/wendys-friends/src/app/component/list-horses/list-horses.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-horses/list-horses.component.ts b/frontend/wendys-friends/src/app/component/list-horses/list-horses.component.ts new file mode 100644 index 0000000..75ce81f --- /dev/null +++ b/frontend/wendys-friends/src/app/component/list-horses/list-horses.component.ts @@ -0,0 +1,107 @@ +import { Component, OnInit } from '@angular/core'; +import { Horse } from '../../dto/horse'; +import { HorseService } from '../../service/horse.service'; +import { HttpParams } from '@angular/common/http'; + +@Component({ + selector: 'app-list-horses', + templateUrl: './list-horses.component.html', + styleUrls: ['./list-horses.component.scss'] +}) +export class ListHorsesComponent implements OnInit { + error = false; + errorMessage = ''; + horses: Array; + filters: { + name: string, + description: string, + birthday: string, + race: string, + score: string + }; + + constructor(private horseService: HorseService) { } + + ngOnInit(): void { + // Nullify queries + this.filters = { + name: "", + description: "", + birthday: null, + race: "", + score: "", + }; + this.loadAllHorses(); + } + + /** + * Error flag will be deactivated, which clears the error message + */ + vanishError() { + this.error = false; + } + + /** + * Loads all horses from the database + */ + private loadAllHorses() { + console.log("GET all horses from API"); + this.horseService.getAllHorses().subscribe( + (horses: Array) => { + this.horses = horses; + }, + error => { + this.defaultServiceErrorHandling(error); + } + ); + } + + /** + * Load filtered horses from the database + */ + public loadFilteredHorses() { + console.log("GET filtered horses 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); + + if(this.filters.description != null && this.filters.description.length !== 0) + params = params.set('description', this.filters.description); + + if(this.filters.score != null && this.filters.score.length !== 0) + params = params.set('score', this.filters.score); + + if(this.filters.birthday !== null) + params = params.set('birthday', this.filters.birthday); + + if(this.filters.race !== null && this.filters.race.length !== 0) + params = params.set('race', this.filters.race); + + this.horseService.getFilteredHorses(params).subscribe( + (horses: Array) => { + this.horses = horses; + }, + error => { + this.horses = 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/component/update-horse/update-horse.component.html b/frontend/wendys-friends/src/app/component/update-horse/update-horse.component.html index 5daabeb..c2deaae 100644 --- a/frontend/wendys-friends/src/app/component/update-horse/update-horse.component.html +++ b/frontend/wendys-friends/src/app/component/update-horse/update-horse.component.html @@ -56,6 +56,7 @@ - + + Cancel \ No newline at end of file diff --git a/frontend/wendys-friends/src/app/service/horse.service.ts b/frontend/wendys-friends/src/app/service/horse.service.ts index 299046f..70b35a2 100644 --- a/frontend/wendys-friends/src/app/service/horse.service.ts +++ b/frontend/wendys-friends/src/app/service/horse.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { Horse } from '../dto/horse'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpParams } from '@angular/common/http'; import { Globals } from '../global/globals'; import { Observable } from 'rxjs'; @@ -22,6 +22,23 @@ export class HorseService { return this.httpClient.get(this.messageBaseUri + '/' + id); } + /** + * Loads all horses from the backend + */ + getAllHorses(): Observable> { + console.log('Load all horses'); + return this.httpClient.get>(this.messageBaseUri); + } + + /** + * Load all horses from the backend that correspond to a filter + * @param filter to use + */ + getFilteredHorses(filter: HttpParams): Observable> { + console.log('Load all filtered horses'); + return this.httpClient.get>(this.messageBaseUri, { params: filter }); + } + /** * Adds a specific horse to the backend and loads it if successful * @param horse diff --git a/frontend/wendys-friends/src/index.html b/frontend/wendys-friends/src/index.html index 3674b22..a62c854 100644 --- a/frontend/wendys-friends/src/index.html +++ b/frontend/wendys-friends/src/index.html @@ -6,6 +6,7 @@ +