X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fapp%2Fhero.service.ts;h=b794b8ad00b27c0a98801cee73a672aeda301a5f;hb=2221767df6737334e1a4c69b46722bb842f94823;hp=6df713db1826ca67060f377bc78cd97e373fd973;hpb=3efe152e8e2be6f2dc172cf1ac476e2cb01be54b;p=examples%2Fangular-tour-of-heroes diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index 6df713d..b794b8a 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -31,6 +31,14 @@ export class HeroService { ); } + /** POST: add a new hero to the server */ + addHero(hero: Hero): Observable { + return this.http.post(this.heroesUrl, hero, this.httpOptions).pipe( + tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)), + catchError(this.handleError('addHero')) + ); + } + /** GET hero by id. Will 404 if id not found */ getHero(id: number): Observable { this.log(`requested hero id=${id}`); @@ -49,6 +57,20 @@ export class HeroService { ); } + /* GET heroes whose name contains search term */ + searchHeroes(term: string): Observable { + if (!term.trim()) { + // if not search term, return empty hero array. + return of([]); + } + return this.http.get(`${this.heroesUrl}/?name=${term}`).pipe( + tap(x => x.length ? + this.log(`found heroes matching "${term}"`) : + this.log(`no heroes matching "${term}"`)), + catchError(this.handleError('searchHeroes', [])) + ); + } + /** * Handle Http operation that failed. * Let the app continue.