import { Injectable } from '@angular/core';
-import { Observable, of } from 'rxjs';
+import { Observable, of, EMPTY } from 'rxjs';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
+import { HttpClient, HttpHeaders } from '@angular/common/http';
import { MessageService } from './message.service';
+import { catchError, tap } from 'rxjs/operators';
@Injectable({
})
export class HeroService {
- constructor(private messageService : MessageService) { }
+ heroesUrl = 'api/heroes'; // URL to web api
+ httpOptions = {
+ headers: new HttpHeaders({ 'Content-Type': 'application/json' })
+ };
- getHeroes() : Observable<Hero[]> {
- this.messageService.add('HeroService: fetching heroes...');
- return of(HEROES);
+ constructor(
+ private http: HttpClient,
+ private messageService: MessageService) { }
+
+ getHeroes(): Observable<Hero[]> {
+ this.log('fetching heroes...');
+ return this.http
+ .get<Hero[]>(this.heroesUrl)
+ .pipe(
+ tap((heroes: Hero[]) => this.log(`fetched ${heroes.length} heroes`)),
+ catchError(this.handleError<Hero[]>('getHeroes', []))
+ );
+ }
+
+ /** POST: add a new hero to the server */
+ addHero(hero: Hero): Observable<Hero> {
+ return this.http.post<Hero>(this.heroesUrl, hero, this.httpOptions).pipe(
+ tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),
+ catchError(this.handleError<Hero>('addHero'))
+ );
+ }
+
+ /** GET hero by id. Will 404 if id not found */
+ getHero(id: number): Observable<Hero> {
+ this.log(`requested hero id=${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}`))
+ );
+ }
+
+ /** PUT: update the hero on the server */
+ updateHero(hero: Hero): Observable<any> {
+ return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(
+ tap(_ => this.log(`updated hero id=${hero.id}`)),
+ catchError(this.handleError<any>('updateHero'))
+ );
+ }
+
+ /* GET heroes whose name contains search term */
+ searchHeroes(term: string): Observable<Hero[]> {
+ if (!term.trim()) {
+ // if not search term, return empty hero array.
+ return of([]);
+ }
+ return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe(
+ tap(x => x.length ?
+ this.log(`found heroes matching "${term}"`) :
+ this.log(`no heroes matching "${term}"`)),
+ catchError(this.handleError<Hero[]>('searchHeroes', []))
+ );
+ }
+
+ /**
+ * Handle Http operation that failed.
+ * Let the app continue.
+ * @param operation - name of the operation that failed
+ * @param result - optional value to return as the observable result
+ */
+ private handleError<T>(operation = 'operation', result?: T) {
+ return (error: any): Observable<T> => {
+
+ // TODO: send the error to remote logging infrastructure
+ console.error(error); // log to console instead
+
+ // TODO: better job of transforming error for user consumption
+ this.log(`${operation} failed: ${error.message}`);
+
+ // Let the app keep running by returning an empty result.
+ if (result === undefined) {
+ return EMPTY as Observable<T>;
+ } else {
+ return of(result as T);
+ }
+ };
+ }
+
+ /** Log a HeroService message with the MessageService */
+ private log(message: string) {
+ this.messageService.add(`HeroService: ${message}`);
}
}