Untitled

 avatar
unknown
java
2 years ago
1.8 kB
4
Indexable
import {HttpClient, HttpParams} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {environment} from 'src/environments/environment';
import {Horse} from '../dto/horse';

const baseUri = environment.backendUrl + '/horses';

@Injectable({
  providedIn: 'root'
})
export class HorseService {
  constructor(private http: HttpClient) { }

  /**
   * Get all horses stored in the system
   *
   * @return observable list of found horses.
   */
  getAll(): Observable<Horse[]> {
    return this.http.get<Horse[]>(baseUri);
  }


  /**
   * Create a new horse in the system.
   *
   * @param horse the data for the horse that should be created
   * @return an Observable for the created horse
   */
  create(horse: Horse): Observable<Horse> {
    console.log('Create horse ' + horse.name + 'with ID: ' + horse.id);
    return this.http.post<Horse>(
      baseUri,
      horse
    );
  }

  /**
   * Edit an existing horse in the system.
   *
   * @param horse the data for the horse that should be edited
   * @return an Observable for the created horse
   */
  update(horse: Horse): Observable<Horse> {
    console.log('Edit horse ' + horse.name + 'with ID: ' + horse.id);
    return this.http.put<Horse>(
      baseUri,
      horse
    );
  }


  /**
   * Get a horse stored in the system.
   *
   * @param id the id of the stored horse.
   * @return horse the horse with 'id'
   */
  getHorse(id: string | null | undefined) {
    console.log('Retrieve ' + id);
    return this.http.get<Horse>(baseUri + '/' + id);
  }

  /**
   * Delete an existing horse.
   *
   * @param id of the horse to delete
   */
  deleteHorse(id: string | null | undefined): Observable<Horse> {
    console.log('Delete  ' + id);
    return this.http.delete<Horse>(baseUri + '/' + id);
  }
}
Editor is loading...