Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 64 lines (48 sloc) 1.49 KB
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { catchError } from 'rxjs/operators';
import { Passenger } from './models/passenger.interface';
const PASSENGER_API: string = 'http://localhost:3004/passengers';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
@Injectable()
export class PassengerDashboardService {
constructor(private http: HttpClient) {}
getPassengers(): Observable<Passenger[]> {
return this.http
.get(PASSENGER_API)
.pipe(
catchError(this._handleError)
);
}
private _handleError(err: HttpErrorResponse | any) {
const errorMsg = err.message || 'Unable to retrieve data';
return Observable.throw(errorMsg);
}
getPassenger(id: number): Observable<Passenger> {
return this.http
.get(`${PASSENGER_API}/${id}`)
.pipe(
catchError(this._handleError)
);
}
updatePassenger(passenger: Passenger): Observable<Passenger> {
return this.http
.put(`${PASSENGER_API}/${passenger.id}`, passenger, httpOptions)
.pipe(
catchError(this._handleError)
);
}
removePassenger(passenger: Passenger): Observable<Passenger> {
return this.http
.delete(`${PASSENGER_API}/${passenger.id}`)
.pipe(
catchError(this._handleError)
);
}
}