import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Order } from '@state/order/order.model' ; import { Observable } from 'rxjs/Observable' ; import { of } from 'rxjs/observable/of' ; import { map , switchMap } from 'rxjs/operators' ; @ Injectable ( ) export class OrderService { private ordersUrl = 'app/orders' ; constructor ( private httpClient ) { } getOrders ( ) { return this . httpClient . get < Order [ ] > ( this . ordersUrl ) ; } getOrder ( id ) { return this . getOrders ( ) . pipe ( map ( orders => orders . find ( order => order . id === id ) ) ) ; } save ( order ) { if ( order . id ) { return this . put ( order ) ; } return this . post ( order ) ; } delete ( id ) { return this . httpClient . delete < void > ( `${ this . ordersUrl }/${ id }` ) . pipe ( switchMap ( ( ) => of ( id ) ) ) ; } private post ( order ) { return this . httpClient . post < Order > ( this . ordersUrl , order ) ; } private put ( order ) { return this . httpClient . put < void > ( `${ this . ordersUrl }/${ order . id }` , order ) . pipe ( switchMap ( ( ) => of ( order ) ) ) ; } }