add less 6 Get hero by id

This commit is contained in:
Костя 2025-06-23 19:55:58 +03:00
parent 01299fd1e9
commit 299aeb7d14
1 changed files with 8 additions and 5 deletions

View File

@ -31,12 +31,15 @@ export class HeroService {
); );
} }
/** GET hero by id. Will 404 if id not found */
getHero(id: number): Observable<Hero> { getHero(id: number): Observable<Hero> {
// For now, assume that a hero with the specified `id` always exists. const url = `${this.heroesUrl}/${id}`;
// Error handling will be added in the next step of the tutorial. return this.http.get<Hero>(url).pipe(
const hero = HEROES.find(h => h.id === id)!; tap(_ => this.log(`fetched hero id=${id}`)),
this.messageService.add(`HeroService: fetched hero id=${id}`); catchError(this.handleError<Hero>(`getHero id=${id}`))
return of(hero); );
} }
/** Log a HeroService message with the MessageService */ /** Log a HeroService message with the MessageService */