import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable , of } from 'rxjs' ; import { catchError , map , tap } from 'rxjs/operators' ; import { Hero } from './hero' ; import { MessageService } from './message.service' ; const httpOptions = { headers : new HttpHeaders ( { 'Content-Type' : 'application/json' } ) } ; @ Injectable ( { providedIn : 'root' } ) export class HeroService { private heroesUrl = 'api/heroes' ; constructor ( private http , private messageService ) { } /** GET heroes from the server */ getHeroes ( ) { return this . http . get < Hero [ ] > ( this . heroesUrl ) . pipe ( tap ( heroes => this . log ( `fetched heroes` ) ) , catchError ( this . handleError ( 'getHeroes' , [ ] ) ) ) ; } /** GET hero by id. Return `undefined` when id not found */ getHeroNo404 < Data > ( id ) { const url = `${ this . heroesUrl }/?id=${ id }` ; return this . http . get < Hero [ ] > ( url ) . pipe ( map ( heroes => heroes [ 0 ] ) , tap ( h => { const outcome = h ? `fetched` : `did not find` ; this . log ( `${ outcome } hero id=${ id }` ) ; } ) , catchError ( this . handleError < Hero > ( `getHero id=${ id }` ) ) ) ; } /** GET hero by id. Will 404 if id not found */ getHero ( id ) { const url = `${ this . heroesUrl }/${ id }` ; return this . http . get < Hero > ( url ) . pipe ( tap ( _ => this . log ( `fetched hero id=${ id }` ) ) , catchError ( this . handleError < Hero > ( `getHero id=${ id }` ) ) ) ; } searchHeroes ( term ) { if ( ! term . trim ( ) ) { return of ( [ ] ) ; } return this . http . get < Hero [ ] > ( `api/heroes/?name=${ term }` ) . pipe ( tap ( _ => this . log ( `found heroes matching "${ term }"` ) ) , catchError ( this . handleError < Hero [ ] > ( 'searchHeroes' , [ ] ) ) ) ; } /** POST: add a new hero to the server */ addHero ( hero ) { return this . http . post < Hero > ( this . heroesUrl , hero , httpOptions ) . pipe ( tap ( ( h ) => this . log ( `added hero w/ id=${ h . id }` ) ) , catchError ( this . handleError < Hero > ( 'addHero' ) ) ) ; } /** DELETE: delete the hero from the server */ deleteHero ( hero ) { const id = typeof hero === 'number' ? hero : hero . id ; const url = `${ this . heroesUrl }/${ id }` ; return this . http . delete < Hero > ( url , httpOptions ) . pipe ( tap ( _ => this . log ( `deleted hero id=${ id }` ) ) , catchError ( this . handleError < Hero > ( 'deleteHero' ) ) ) ; } /** PUT: update the hero on the server */ updateHero ( hero ) { return this . http . put ( this . heroesUrl , hero , httpOptions ) . pipe ( tap ( _ => this . log ( `updated hero id=${ hero . id }` ) ) , catchError ( this . handleError < any > ( 'updateHero' ) ) ) ; } param operation param result private handleError < T > ( operation = 'operation' , result ? ) { return ( error ) : Observable < T > => { console . error ( error ) ; this . log ( `${ operation } failed: ${ error . message }` ) ; return of ( result as T ) ; } ; } /** Log a HeroService message with the MessageService */ private log ( message ) { this . messageService . add ( 'HeroService: ' + message ) ; } }