From df76acceeb3e75ac4d5bbdc939ff9476e496394c Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Tue, 17 Mar 2020 20:55:15 +0100 Subject: [PATCH] US01: Add an initial Angular component, dto and service for the horses --- .../src/app/app-routing.module.ts | 4 +- frontend/wendys-friends/src/app/app.module.ts | 4 +- .../component/header/header.component.html | 2 +- .../app/component/horse/horse.component.html | 17 +++++ .../app/component/horse/horse.component.scss | 0 .../app/component/horse/horse.component.ts | 72 +++++++++++++++++++ frontend/wendys-friends/src/app/dto/horse.ts | 10 +++ .../src/app/service/horse.service.ts | 33 +++++++++ 8 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 frontend/wendys-friends/src/app/component/horse/horse.component.html create mode 100644 frontend/wendys-friends/src/app/component/horse/horse.component.scss create mode 100644 frontend/wendys-friends/src/app/component/horse/horse.component.ts create mode 100644 frontend/wendys-friends/src/app/dto/horse.ts create mode 100644 frontend/wendys-friends/src/app/service/horse.service.ts diff --git a/frontend/wendys-friends/src/app/app-routing.module.ts b/frontend/wendys-friends/src/app/app-routing.module.ts index 13ab9be..c39cb3d 100644 --- a/frontend/wendys-friends/src/app/app-routing.module.ts +++ b/frontend/wendys-friends/src/app/app-routing.module.ts @@ -1,11 +1,13 @@ 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'; const routes: Routes = [ {path: '', redirectTo: 'owner', pathMatch: 'full'}, - {path: 'owner', component: OwnerComponent} + {path: 'owner', component: OwnerComponent}, + {path: 'horse', component: HorseComponent}, ]; @NgModule({ diff --git a/frontend/wendys-friends/src/app/app.module.ts b/frontend/wendys-friends/src/app/app.module.ts index ef1aba5..028f53d 100644 --- a/frontend/wendys-friends/src/app/app.module.ts +++ b/frontend/wendys-friends/src/app/app.module.ts @@ -6,12 +6,14 @@ import {AppComponent} from './app.component'; import {HeaderComponent} from './component/header/header.component'; import {OwnerComponent} from './component/owner/owner.component'; import {HttpClientModule} from '@angular/common/http'; +import { HorseComponent } from './component/horse/horse.component'; @NgModule({ declarations: [ AppComponent, HeaderComponent, - OwnerComponent + OwnerComponent, + HorseComponent ], imports: [ BrowserModule, diff --git a/frontend/wendys-friends/src/app/component/header/header.component.html b/frontend/wendys-friends/src/app/component/header/header.component.html index a91de3f..3190e40 100644 --- a/frontend/wendys-friends/src/app/component/header/header.component.html +++ b/frontend/wendys-friends/src/app/component/header/header.component.html @@ -7,7 +7,7 @@ diff --git a/frontend/wendys-friends/src/app/component/horse/horse.component.html b/frontend/wendys-friends/src/app/component/horse/horse.component.html new file mode 100644 index 0000000..866e867 --- /dev/null +++ b/frontend/wendys-friends/src/app/component/horse/horse.component.html @@ -0,0 +1,17 @@ + + +
+ +
diff --git a/frontend/wendys-friends/src/app/component/horse/horse.component.scss b/frontend/wendys-friends/src/app/component/horse/horse.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/frontend/wendys-friends/src/app/component/horse/horse.component.ts b/frontend/wendys-friends/src/app/component/horse/horse.component.ts new file mode 100644 index 0000000..da82d7f --- /dev/null +++ b/frontend/wendys-friends/src/app/component/horse/horse.component.ts @@ -0,0 +1,72 @@ +import { Component, OnInit } from '@angular/core'; +import { HorseService } from '../../service/horse.service'; +import { Horse } from '../../dto/horse'; + +@Component({ + selector: 'app-horse', + templateUrl: './horse.component.html', + styleUrls: ['./horse.component.scss'] +}) +export class HorseComponent implements OnInit { + + error = false; + errorMessage = ''; + horse: Horse; + + constructor(private horseService: HorseService) { } + + ngOnInit(): void { + this.loadHorse(1); + } + + /** + * Error flag will be deactivated, which clears the error message + */ + vanishError() { + this.error = false; + } + + /** + * Loads the horse for the specified id + * @param id the id of the horse + */ + private loadHorse(id: number) { + this.horseService.getHorseById(id).subscribe( + (horse: Horse) => { + this.horse = horse; + }, + error => { + this.defaultServiceErrorHandling(error); + } + ); + } + + /** + * Creates a new horse and loads it + * @param horse + */ + private addHorse(horse: Horse) { + this.horseService.addHorse(horse).subscribe( + (horse: Horse) => { + this.horse = horse; + }, + 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/dto/horse.ts b/frontend/wendys-friends/src/app/dto/horse.ts new file mode 100644 index 0000000..7caa2cc --- /dev/null +++ b/frontend/wendys-friends/src/app/dto/horse.ts @@ -0,0 +1,10 @@ +export class Horse { + constructor( + public id: number, + public name: string, + public description: string, + public score: number, + public birthday: Date, + public owner: number) { + } +} diff --git a/frontend/wendys-friends/src/app/service/horse.service.ts b/frontend/wendys-friends/src/app/service/horse.service.ts new file mode 100644 index 0000000..1f0871c --- /dev/null +++ b/frontend/wendys-friends/src/app/service/horse.service.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@angular/core'; +import { Horse } from '../dto/horse'; +import { HttpClient } from '@angular/common/http'; +import { Globals } from '../global/globals'; +import { Observable } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class HorseService { + private messageBaseUri: string = this.globals.backendUri + '/horses'; + + constructor(private httpClient: HttpClient, private globals: Globals) { + } + + /** + * Loads specific horse from the backend + * @param id of horse to load + */ + getHorseById(id: number): Observable { + console.log('Load horse details for ' + id); + return this.httpClient.get(this.messageBaseUri + '/' + id); + } + + /** + * Adds a specific horse to the backend and loads it if successful + * @param horse + */ + addHorse(horse: Horse): Observable { + console.log('Add new horse ' + horse); + return this.httpClient.post(this.messageBaseUri, horse); + } +}