From b8d35150a431b2cbfd06185dd5f307f08d4c773e Mon Sep 17 00:00:00 2001 From: "DESKTOP-P9VU163\\admin" Date: Tue, 24 Jun 2025 09:45:41 +0300 Subject: [PATCH] add less 6 herro update --- .../hero-detail/hero-detail.component.html | 7 ++-- src/app/hero-detail/hero-detail.component.ts | 7 ++++ src/app/hero.service.ts | 35 +++++++++++++------ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/app/hero-detail/hero-detail.component.html b/src/app/hero-detail/hero-detail.component.html index d780243..023e31c 100644 --- a/src/app/hero-detail/hero-detail.component.html +++ b/src/app/hero-detail/hero-detail.component.html @@ -1,12 +1,11 @@
- - -

{{hero.name | uppercase}} Details

id: {{hero.id}}
-
+ + + diff --git a/src/app/hero-detail/hero-detail.component.ts b/src/app/hero-detail/hero-detail.component.ts index 78342f4..8713c2f 100644 --- a/src/app/hero-detail/hero-detail.component.ts +++ b/src/app/hero-detail/hero-detail.component.ts @@ -39,4 +39,11 @@ export class HeroDetailComponent { .subscribe(hero => this.hero = hero); } + save(): void { + if (this.hero) { + this.heroService.updateHero(this.hero) + .subscribe(() => this.goBack()); + } + } + } diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index 5462f55..5432051 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -3,7 +3,7 @@ import { Hero } from './hero'; import { HEROES } from './mock-heroes'; import { Observable, of } from 'rxjs'; import { MessageService } from './message.service'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; import { catchError, map, tap } from 'rxjs/operators'; @Injectable({ @@ -13,25 +13,40 @@ import { catchError, map, tap } from 'rxjs/operators'; export class HeroService { private heroesUrl = 'api/heroes'; // URL to web api + + httpOptions = { + headers: new HttpHeaders({ 'Content-Type': 'application/json' }) + }; constructor( private http: HttpClient, private messageService: MessageService) { } + /** GET heroes from the server */ getHeroes(): Observable { - return this.http.get(this.heroesUrl) - .pipe( - catchError(this.handleError('getHeroes', [])) + return this.http.get(this.heroesUrl) + .pipe( + tap(_ => this.log('fetched heroes')), + catchError(this.handleError('getHeroes', [])) + ); + } + + /** GET hero by id. Will 404 if id not found */ + getHero(id: number): Observable { + const url = `${this.heroesUrl}/${id}`; + return this.http.get(url).pipe( + tap(_ => this.log(`fetched hero id=${id}`)), + catchError(this.handleError(`getHero id=${id}`)) ); } - getHero(id: number): Observable { - // For now, assume that a hero with the specified `id` always exists. - // Error handling will be added in the next step of the tutorial. - const hero = HEROES.find(h => h.id === id)!; - this.messageService.add(`HeroService: fetched hero id=${id}`); - return of(hero); + /** 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')) + ); } /**