import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { LineItem } from '@state/line-item/line-item.model' ; import { Observable } from 'rxjs/Observable' ; import { of } from 'rxjs/observable/of' ; import { map , switchMap } from 'rxjs/operators' ; @ Injectable ( ) export class LineItemService { private lineItemsUrl = 'app/lineItems' ; constructor ( private httpClient ) { } getLineItems ( ) { return this . httpClient . get < LineItem [ ] > ( this . lineItemsUrl ) ; } getLineItem ( id ) { return this . getLineItems ( ) . pipe ( map ( lineItems => lineItems . find ( lineItem => lineItem . id === id ) ) ) ; } save ( lineItem ) { if ( lineItem . id ) { return this . put ( lineItem ) ; } return this . post ( lineItem ) ; } delete ( id ) { return this . httpClient . delete < void > ( `${ this . lineItemsUrl }/${ id }` ) . pipe ( switchMap ( ( ) => of ( id ) ) ) ; } private post ( lineItem ) { return this . httpClient . post < LineItem > ( this . lineItemsUrl , lineItem ) ; } private put ( lineItem ) { return this . httpClient . put < void > ( `${ this . lineItemsUrl }/${ lineItem . id }` , lineItem ) . pipe ( switchMap ( ( ) => of ( lineItem ) ) ) ; } }