US10: Add an owner input to both horse forms, make it possible to open the horse page through the list

This commit is contained in:
Ivaylo Ivanov 2020-03-25 14:29:22 +01:00
parent 6c4fa0df90
commit 9b1f7bcfa0
6 changed files with 61 additions and 12 deletions

View File

@ -56,6 +56,17 @@
<input type="file" class="form-control-file" name="image" id="image" (change)="handleImageInput($event.target.files)" required> <input type="file" class="form-control-file" name="image" id="image" (change)="handleImageInput($event.target.files)" required>
</div> </div>
<div class="form-group">
<label for="race">Owner</label>
<select class="form-control" name="owner" [(ngModel)]="horse.owner">
<option *ngFor="let owner of owners"
[value]="owner.id">
{{ owner.name }}
</option>
</select>
</div>
<button type="submit" class="btn btn-success" [disabled]="!horseForm.form.valid" (submit)="addHorse()">Submit</button> <button type="submit" class="btn btn-success" [disabled]="!horseForm.form.valid" (submit)="addHorse()">Submit</button>
<a class="btn btn-light float-right" href="horse">Cancel</a>
</form> </form>
</div> </div>

View File

@ -1,6 +1,8 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Horse } from '../../dto/horse'; import { Horse } from '../../dto/horse';
import { Owner } from '../../dto/owner';
import { HorseService } from '../../service/horse.service'; import { HorseService } from '../../service/horse.service';
import { OwnerService } from '../../service/owner.service';
@Component({ @Component({
selector: 'app-add-horse', selector: 'app-add-horse',
@ -12,11 +14,22 @@ export class AddHorseComponent implements OnInit {
success: string; success: string;
errorMessage = ''; errorMessage = '';
horse: Horse = new Horse(null, null, null, null, null, null, null, null); horse: Horse = new Horse(null, null, null, null, null, null, null, null);
owners: Array<Owner>;
imageToUpload: File = null; 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<Owner>) => {
this.owners = owners;
},
error => {
this.owners = null;
}
);
}
/** /**
* Will be called on a click on the success alert close button * Will be called on a click on the success alert close button
@ -39,7 +52,6 @@ export class AddHorseComponent implements OnInit {
public addHorse() { public addHorse() {
console.log("POST horse to API") console.log("POST horse to API")
console.log(this.horse); console.log(this.horse);
// TODO: Make it possible for the horse to be added with an owner
this.horseService.addHorse(this.horse).subscribe( this.horseService.addHorse(this.horse).subscribe(
(result: Horse) => { (result: Horse) => {
this.success = result.name; this.success = result.name;

View File

@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { HorseService } from '../../service/horse.service'; import { HorseService } from '../../service/horse.service';
import { Horse } from '../../dto/horse'; import { Horse } from '../../dto/horse';
import { ActivatedRoute } from '@angular/router';
@Component({ @Component({
selector: 'app-horse', selector: 'app-horse',
@ -13,10 +14,12 @@ export class HorseComponent implements OnInit {
errorMessage = ''; errorMessage = '';
horse: Horse; horse: Horse;
constructor(private horseService: HorseService) { } constructor(private horseService: HorseService, private route: ActivatedRoute) { }
ngOnInit(): void { ngOnInit(): void {
this.loadHorse(1); // Extract id from url
const horseId: string = this.route.snapshot.paramMap.get('id');
this.loadHorse(parseInt(horseId));
} }
/** /**

View File

@ -55,8 +55,7 @@
<th scope="col">Score</th> <th scope="col">Score</th>
<th scope="col">Birthday</th> <th scope="col">Birthday</th>
<th scope="col">Race</th> <th scope="col">Race</th>
<th scope="col">Edit</th> <th scope="col">Actions</th>
<th scope="col">Delete</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -68,9 +67,8 @@
<td>{{ horse.birthday }}</td> <td>{{ horse.birthday }}</td>
<td>{{ horse.race }}</td> <td>{{ horse.race }}</td>
<td> <td>
<a class="btn btn-primary" href="horse/{{ horse.id }}"><i class="fas fa-external-link-alt"></i></a>
<a class="btn btn-success" href="horse/{{ horse.id }}/edit"><i class="fas fa-edit"></i></a> <a class="btn btn-success" href="horse/{{ horse.id }}/edit"><i class="fas fa-edit"></i></a>
</td>
<td>
<button class="btn btn-danger" (click)="deleteHorse(horse.id, horse.name)"><i class="fas fa-trash"></i></button> <button class="btn btn-danger" (click)="deleteHorse(horse.id, horse.name)"><i class="fas fa-trash"></i></button>
</td> </td>
</tr> </tr>

View File

@ -6,14 +6,14 @@
</div> </div>
<div *ngIf="success" class="alert alert-success alert-dismissible fade show" role="alert"> <div *ngIf="success" class="alert alert-success alert-dismissible fade show" role="alert">
<p><strong>Success! </strong> Horse {{ success }} has been successfully added. </p> <p><strong>Success! </strong> Horse {{ success }} has been successfully updated. </p>
<button type="button" (click)="vanishAlert()" class="close" data-dismiss="alert" aria-label="Close"> <button type="button" (click)="vanishAlert()" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<div class="container"> <div class="container">
<h1>Add new horse</h1> <h1>Update horse</h1>
<form (ngSubmit)="updateHorse()" #horseForm="ngForm"> <form (ngSubmit)="updateHorse()" #horseForm="ngForm">
<div class="form-group"> <div class="form-group">
<label for="name">Name</label> <label for="name">Name</label>
@ -56,6 +56,18 @@
<input type="file" class="form-control-file" name="image" id="image" (change)="handleImageInput($event.target.files)" required> <input type="file" class="form-control-file" name="image" id="image" (change)="handleImageInput($event.target.files)" required>
</div> </div>
<div class="form-group">
<label for="race">Owner</label>
<select class="form-control" name="owner" [(ngModel)]="horse.owner">
<option value="null">No Owner</option>
<option *ngFor="let owner of owners"
[value]="owner.id"
[attr.selected]="horse.owner==owner.id ? true : null">
{{ owner.name }}
</option>
</select>
</div>
<button type="submit" class="btn btn-success float-left" [disabled]="!horseForm.form.valid" (submit)="updateHorse()">Submit</button> <button type="submit" class="btn btn-success float-left" [disabled]="!horseForm.form.valid" (submit)="updateHorse()">Submit</button>
<a class="btn btn-light float-right" href="horse">Cancel</a> <a class="btn btn-light float-right" href="horse">Cancel</a>
</form> </form>

View File

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