X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fapp%2Fhero.service.ts;h=349d043a10ee94158ddb3260ea18c6227f8329cc;hb=5c525c7b08b4ea57e94f1c8644297de8e443e2da;hp=75a002b12d73f9bd7180590dfd9835b254ac8bc9;hpb=806998ac75abe1082f666c141e0bd3f1a8ae7af1;p=examples%2Fangular-tour-of-heroes diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index 75a002b..349d043 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -13,6 +13,9 @@ import { catchError, tap } from 'rxjs/operators'; export class HeroService { heroesUrl = 'api/heroes'; // URL to web api + httpOptions = { + headers: new HttpHeaders({ 'Content-Type': 'application/json' }) + }; constructor( private http: HttpClient, @@ -28,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}`); @@ -38,6 +49,14 @@ export class HeroService { ); } + /** PUT: update the hero on the server */ + updateHero(hero: Hero): Observable { + return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe( + tap(_ => this.log(`updated hero id=${hero.id}`)), + catchError(this.handleError('updateHero')) + ); + } + /** * Handle Http operation that failed. * Let the app continue.