Skip to content

Instantly share code, notes, and snippets.

@krishna-acondy
Last active February 14, 2018 08:02
Show Gist options
  • Save krishna-acondy/6385657dc8d3423ba10a12c60d64f0f4 to your computer and use it in GitHub Desktop.
Save krishna-acondy/6385657dc8d3423ba10a12c60d64f0f4 to your computer and use it in GitHub Desktop.
PizzaService example for a service using the HttpClient
export class PizzaService {
private url='http://pizzaService';
private endpoint = 'pizzas';
constructor(
protected httpClient: HttpClient) {}
public create(pizza: Pizza): Observable<Pizza> {
return this.httpClient
.post<Pizza>(`${this.url}/${this.endpoint}`, pizza);
}
public update(pizza: Pizza): Observable<Pizza> {
return this.httpClient
.put<Pizza>(`${this.url}/${this.endpoint}/${pizza.id}`, pizza);
}
read(id: number): Observable<Pizza> {
return this.httpClient
.get<Pizza>(`${this.url}/${this.endpoint}/${id}`);
}
list(): Observable<Pizza[]> {
return this.httpClient
.get<Pizza[]>(`${this.url}/${this.endpoint}`);
}
delete(id: number) {
return this.httpClient
.delete(`${this.url}/${this.endpoint}/${id}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment