import { Observable, of, EMPTY } from 'rxjs';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
+import { HttpClient } from '@angular/common/http';
import { MessageService } from './message.service';
})
export class HeroService {
- constructor(private messageService : MessageService) { }
+ private heroesUrl = 'api/heroes'; // URL to web api
- 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);
}
getHero(id: number): Observable<Hero> {
- this.messageService.add(`HeroService: requested hero id=${id}`);
+ this.log(`requested hero id=${id}`);
const found: Hero | undefined = HEROES.find(hero => hero.id === id);
if (found === undefined) {
return EMPTY;
return of(found);
}
}
+
+ /** Log a HeroService message with the MessageService */
+ private log(message: string) {
+ this.messageService.add(`HeroService: ${message}`);
+ }
}