|
|
|
@@ -1,24 +1,79 @@ |
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
import {Injectable} from '@angular/core'; |
|
|
|
|
|
|
|
import { Hero } from './hero'; |
|
|
|
import { HEROES } from './mock-heroes'; |
|
|
|
import { Observable, of } from 'rxjs'; |
|
|
|
import { MessageService } from './message.service'; |
|
|
|
import {Hero} from './hero'; |
|
|
|
import {Observable, of} from 'rxjs'; |
|
|
|
import {MessageService} from './message.service'; |
|
|
|
import {HttpClient, HttpHeaders} from "@angular/common/http"; |
|
|
|
import {catchError, tap} from "rxjs/operators"; |
|
|
|
|
|
|
|
@Injectable({ |
|
|
|
providedIn: 'root' |
|
|
|
}) |
|
|
|
export class HeroService { |
|
|
|
|
|
|
|
constructor(private messageService: MessageService) { } |
|
|
|
private heroesUrl = 'api/heroes'; |
|
|
|
|
|
|
|
httpOptions = { |
|
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }) |
|
|
|
}; |
|
|
|
|
|
|
|
constructor( |
|
|
|
private http: HttpClient, |
|
|
|
private messageService: MessageService) { |
|
|
|
} |
|
|
|
|
|
|
|
getHeroes(): Observable<Hero[]> { |
|
|
|
this.messageService.add('HeroService: fetched heroes') |
|
|
|
return of(HEROES); |
|
|
|
return this.http.get<Hero[]>(this.heroesUrl) |
|
|
|
.pipe( |
|
|
|
tap(_ => this.log('feteched heroes')), |
|
|
|
catchError(this.handleError<Hero[]>('getHeroes', [])) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
getHero(id: number): Observable<Hero> { |
|
|
|
this.messageService.add(`HeroService: fetched hero id=${id}`); |
|
|
|
return of(HEROES.find(hero => 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}`)) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
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')) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
deleteHero (hero: Hero | number): Observable<Hero> { |
|
|
|
const id = typeof hero === 'number' ? hero : hero.id; |
|
|
|
const url = `${this.heroesUrl}/${id}`; |
|
|
|
|
|
|
|
return this.http.delete<Hero>(url, this.httpOptions).pipe( |
|
|
|
tap(_ => this.log(`deleted hero id=${id}`)), |
|
|
|
catchError(this.handleError<Hero>('deleteHero')) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private log(message: string) { |
|
|
|
this.messageService.add(`HeroService: ${message}`); |
|
|
|
} |
|
|
|
|
|
|
|
handleError<T>(operation: string = 'operation', result?: T) { |
|
|
|
return (error: any): Observable<T> => { |
|
|
|
console.error(error); |
|
|
|
|
|
|
|
this.log(`${operation} failed: ${error.message}`); |
|
|
|
|
|
|
|
return of(result as T); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
udpateHero(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')) |
|
|
|
); |
|
|
|
} |
|
|
|
} |