From 9b1f7bcfa0f08239304ebbdd2b7956f063867f8d Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Wed, 25 Mar 2020 14:29:22 +0100 Subject: [PATCH] US10: Add an owner input to both horse forms, make it possible to open the horse page through the list --- .../add-horse/add-horse.component.html | 11 +++++++++++ .../component/add-horse/add-horse.component.ts | 18 +++++++++++++++--- .../src/app/component/horse/horse.component.ts | 7 +++++-- .../list-horses/list-horses.component.html | 6 ++---- .../update-horse/update-horse.component.html | 16 ++++++++++++++-- .../update-horse/update-horse.component.ts | 15 ++++++++++++++- 6 files changed, 61 insertions(+), 12 deletions(-) diff --git a/frontend/wendys-friends/src/app/component/add-horse/add-horse.component.html b/frontend/wendys-friends/src/app/component/add-horse/add-horse.component.html index db14c5b..5395959 100644 --- a/frontend/wendys-friends/src/app/component/add-horse/add-horse.component.html +++ b/frontend/wendys-friends/src/app/component/add-horse/add-horse.component.html @@ -56,6 +56,17 @@ +
+ + +
+ + Cancel \ No newline at end of file diff --git a/frontend/wendys-friends/src/app/component/add-horse/add-horse.component.ts b/frontend/wendys-friends/src/app/component/add-horse/add-horse.component.ts index cbd27c1..85969cd 100644 --- a/frontend/wendys-friends/src/app/component/add-horse/add-horse.component.ts +++ b/frontend/wendys-friends/src/app/component/add-horse/add-horse.component.ts @@ -1,6 +1,8 @@ import { Component, OnInit } from '@angular/core'; import { Horse } from '../../dto/horse'; +import { Owner } from '../../dto/owner'; import { HorseService } from '../../service/horse.service'; +import { OwnerService } from '../../service/owner.service'; @Component({ selector: 'app-add-horse', @@ -12,11 +14,22 @@ export class AddHorseComponent implements OnInit { success: string; errorMessage = ''; horse: Horse = new Horse(null, null, null, null, null, null, null, null); + owners: Array; imageToUpload: File = null; - constructor(private horseService: HorseService) { } + constructor(private horseService: HorseService, private ownerService: OwnerService) { } - ngOnInit(): void {} + ngOnInit(): void { + // Get all owners and save them + this.ownerService.getAllOwners().subscribe( + (owners: Array) => { + this.owners = owners; + }, + error => { + this.owners = null; + } + ); + } /** * Will be called on a click on the success alert close button @@ -39,7 +52,6 @@ export class AddHorseComponent implements OnInit { public addHorse() { console.log("POST horse to API") console.log(this.horse); - // TODO: Make it possible for the horse to be added with an owner this.horseService.addHorse(this.horse).subscribe( (result: Horse) => { this.success = result.name; diff --git a/frontend/wendys-friends/src/app/component/horse/horse.component.ts b/frontend/wendys-friends/src/app/component/horse/horse.component.ts index 3e92b80..ed47280 100644 --- a/frontend/wendys-friends/src/app/component/horse/horse.component.ts +++ b/frontend/wendys-friends/src/app/component/horse/horse.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { HorseService } from '../../service/horse.service'; import { Horse } from '../../dto/horse'; +import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-horse', @@ -13,10 +14,12 @@ export class HorseComponent implements OnInit { errorMessage = ''; horse: Horse; - constructor(private horseService: HorseService) { } + constructor(private horseService: HorseService, private route: ActivatedRoute) { } ngOnInit(): void { - this.loadHorse(1); + // Extract id from url + const horseId: string = this.route.snapshot.paramMap.get('id'); + this.loadHorse(parseInt(horseId)); } /** 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 index 33f9aae..1a8db06 100644 --- 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 @@ -55,8 +55,7 @@ Score Birthday Race - Edit - Delete + Actions @@ -68,9 +67,8 @@ {{ horse.birthday }} {{ horse.race }} + - - 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 c2deaae..88c52e6 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 @@ -6,14 +6,14 @@
-

Add new horse

+

Update horse

@@ -56,6 +56,18 @@
+
+ + +
+ Cancel
diff --git a/frontend/wendys-friends/src/app/component/update-horse/update-horse.component.ts b/frontend/wendys-friends/src/app/component/update-horse/update-horse.component.ts index 420366a..338d817 100644 --- a/frontend/wendys-friends/src/app/component/update-horse/update-horse.component.ts +++ b/frontend/wendys-friends/src/app/component/update-horse/update-horse.component.ts @@ -1,7 +1,9 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Params} from '@angular/router'; import { Horse } from '../../dto/horse'; +import { Owner } from '../../dto/owner'; import { HorseService } from '../../service/horse.service'; +import { OwnerService } from '../../service/owner.service'; @Component({ selector: 'app-update-horse', @@ -13,9 +15,10 @@ export class UpdateHorseComponent implements OnInit { success: string; errorMessage = ''; horse: Horse = new Horse(null, null, null, null, null, null, null, null); + owners: Array; imageToUpload: File = null; - constructor(private horseService: HorseService, private route: ActivatedRoute) { } + constructor(private horseService: HorseService, private route: ActivatedRoute, private ownerService: OwnerService) { } ngOnInit(): void { // Extract id from url @@ -30,6 +33,16 @@ export class UpdateHorseComponent implements OnInit { this.defaultServiceErrorHandling(error); } ); + + // Get all owners and save them + this.ownerService.getAllOwners().subscribe( + (owners: Array) => { + this.owners = owners; + }, + error => { + this.owners = null; + } + ); } /**