horse.service.ts 970 Bytes
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
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<Horse> {
    console.log('Load horse details for ' + id);
    return this.httpClient.get<Horse>(this.messageBaseUri + '/' + id);
  }

  /**
   * Adds a specific horse to the backend and loads it if successful
   * @param horse
   */
  addHorse(horse: Horse): Observable<Horse> {
30
    console.log('Add new horse ' + JSON.stringify(horse));
31 32 33
    return this.httpClient.post<Horse>(this.messageBaseUri, horse);
  }
}