This repository has been archived on 2021-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
wendys-racing-horses/frontend/wendys-friends/src/app/component/owner/owner.component.ts

80 lines
2.0 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {OwnerService} from '../../service/owner.service';
import {Owner} from '../../dto/owner';
import { ActivatedRoute } from '@angular/router';
import { Horse } from 'src/app/dto/horse';
@Component({
selector: 'app-owner',
templateUrl: './owner.component.html',
styleUrls: ['./owner.component.scss']
})
export class OwnerComponent implements OnInit {
error = false;
errorMessage = '';
owner: Owner;
horses: Array<Horse>
constructor(private ownerService: OwnerService, private route: ActivatedRoute) {
}
ngOnInit() {
const ownerId: string = this.route.snapshot.paramMap.get('id');
this.loadOwner(parseInt(ownerId));
this.loadOwnerHorses(parseInt(ownerId));
}
/**
* Error flag will be deactivated, which clears the error message
*/
vanishError() {
this.error = false;
}
/**
* Loads the owner for the specified id
* @param id the id of the owner
*/
private loadOwner(id: number) {
this.ownerService.getOwnerById(id).subscribe(
(owner: Owner) => {
this.owner = owner;
},
error => {
this.defaultServiceErrorHandling(error);
}
);
}
/**
* Load the horses of the owner
* @param id of the owner
*/
private loadOwnerHorses(id: number) {
this.ownerService.getOwnerHorses(id).subscribe(
(horses: Array<Horse>) => {
this.horses = horses;
},
error => {
console.log(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;
}
}
}