list-horses.component.ts 2.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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<Horse>;
  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<Horse>) => {
        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<Horse>) => {
        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;
    }
  }
}