Last active
January 24, 2018 06:01
-
-
Save mazhekin/af7f22d09e4810e06e2b130b7f836ddd to your computer and use it in GitHub Desktop.
Simple Api Services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient } from '@angular/common/http'; | |
import { Observable } from 'rxjs/Observable'; | |
import { catchError } from 'rxjs/operators'; | |
import 'rxjs/add/observable/throw'; | |
import { Pizza } from '../models/pizza.model'; | |
@Injectable() | |
export class PizzasApiService { | |
constructor(private http: HttpClient) {} | |
getPizzas(): Observable<Pizza[]> { | |
return this.http | |
.get<Pizza[]>(`/api/pizzas`) | |
.pipe(catchError((error: any) => Observable.throw(error.json()))); | |
} | |
createPizza(payload: Pizza): Observable<Pizza> { | |
return this.http | |
.post<Pizza>(`/api/pizzas`, payload) | |
.pipe(catchError((error: any) => Observable.throw(error.json()))); | |
} | |
updatePizza(payload: Pizza): Observable<Pizza> { | |
return this.http | |
.put<Pizza>(`/api/pizzas/${payload.id}`, payload) | |
.pipe(catchError((error: any) => Observable.throw(error.json()))); | |
} | |
removePizza(payload: Pizza): Observable<Pizza> { | |
return this.http | |
.delete<any>(`/api/pizzas/${payload.id}`) | |
.pipe(catchError((error: any) => Observable.throw(error.json()))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment