import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import { of } from 'rxjs/observable/of' ; import { map , switchMap } from 'rxjs/operators' ; import { Customer } from '@state/customer/customer.model' ; @ Injectable ( ) export class CustomerService { private customersUrl = 'app/customers' ; constructor ( private httpClient ) { } getCustomers ( ) { return this . httpClient . get < Customer [ ] > ( this . customersUrl ) ; } getCustomer ( id ) { return this . getCustomers ( ) . pipe ( map ( customers => customers . find ( customer => customer . id === id ) ) ) ; } save ( customer ) { if ( customer . id ) { return this . put ( customer ) ; } return this . post ( customer ) ; } delete ( customer ) { const url = `${ this . customersUrl }/${ customer . id }` ; return this . httpClient . delete < void > ( url ) . pipe ( switchMap ( ( ) => of ( customer ) ) ) ; } private post ( customer ) { return this . httpClient . post < Customer > ( this . customersUrl , customer ) ; } private put ( customer ) { const url = `${ this . customersUrl }/${ customer . id }` ; return this . httpClient . put ( url , customer ) . pipe ( switchMap ( ( ) => of ( customer ) ) ) ; } }