From 286898f2eb49426b5ed3bc904608eb8ad3029fd3 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Sun, 27 Oct 2019 11:54:33 +0100 Subject: [PATCH] =?UTF-8?q?Angular=20Tour=20of=20heroes:=20ai=20continu?= =?UTF-8?q?=C3=A9=20sur=20HTTP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ajout d'un héro * suppression d'un héro * mise à jour d'un héro --- .../hero-detail/hero-detail.component.html | 1 + .../app/hero-detail/hero-detail.component.ts | 4 ++ .../src/app/hero.service.ts | 31 ++++++++++++++ .../src/app/heroes/heroes.component.css | 42 +++++++++++++++++++ .../src/app/heroes/heroes.component.html | 11 +++++ .../src/app/heroes/heroes.component.ts | 13 ++++++ 6 files changed, 102 insertions(+) diff --git a/angular-tour-of-heroes/src/app/hero-detail/hero-detail.component.html b/angular-tour-of-heroes/src/app/hero-detail/hero-detail.component.html index 78cba67..5ad8a71 100644 --- a/angular-tour-of-heroes/src/app/hero-detail/hero-detail.component.html +++ b/angular-tour-of-heroes/src/app/hero-detail/hero-detail.component.html @@ -10,6 +10,7 @@ + diff --git a/angular-tour-of-heroes/src/app/hero-detail/hero-detail.component.ts b/angular-tour-of-heroes/src/app/hero-detail/hero-detail.component.ts index b7124a9..c32f3d0 100644 --- a/angular-tour-of-heroes/src/app/hero-detail/hero-detail.component.ts +++ b/angular-tour-of-heroes/src/app/hero-detail/hero-detail.component.ts @@ -33,4 +33,8 @@ export class HeroDetailComponent implements OnInit { this.location.back(); } + save(): void { + this.heroService.updateHero(this.hero) + .subscribe(() => this.goBack()); + } } diff --git a/angular-tour-of-heroes/src/app/hero.service.ts b/angular-tour-of-heroes/src/app/hero.service.ts index 431085d..3e0e88f 100644 --- a/angular-tour-of-heroes/src/app/hero.service.ts +++ b/angular-tour-of-heroes/src/app/hero.service.ts @@ -10,10 +10,15 @@ import { catchError, map, tap } from 'rxjs/operators'; }) export class HeroService { + httpOptions = { + headers: new HttpHeaders({ 'Content-Type': 'application/json' }) + }; + constructor( private http: HttpClient, private messageService: MessageService) { } + /** Log a HeroService message with the MessageService */ private log(message: string) { this.messageService.add(`HeroService: ${message}`) } @@ -36,6 +41,32 @@ 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')) + ); + } + + 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')) + ); + } + + /** DELETE: delete the hero from the server */ + deleteHero (hero: Hero | number): Observable { + const id = typeof hero === 'number' ? hero : hero.id; + const url = `${this.heroesUrl}/${id}`; + + return this.http.delete(url, this.httpOptions).pipe( + tap(_ => this.log(`deleted hero id=${id}`)), + catchError(this.handleError(`deleteHero id=${id}`)) + ); + } + /** * Handle Http operation that failed. * Let the app continue. diff --git a/angular-tour-of-heroes/src/app/heroes/heroes.component.css b/angular-tour-of-heroes/src/app/heroes/heroes.component.css index 94a91be..6d1344b 100644 --- a/angular-tour-of-heroes/src/app/heroes/heroes.component.css +++ b/angular-tour-of-heroes/src/app/heroes/heroes.component.css @@ -29,3 +29,45 @@ width: 250px; } +.heroes a:hover { + color:#607D8B; +} + +.heroes .badge { + display: inline-block; + font-size: small; + color: white; + padding: 0.8em 0.7em 0 0.7em; + background-color:#405061; + line-height: 1em; + position: relative; + left: -1px; + top: -4px; + height: 1.8em; + min-width: 16px; + text-align: right; + margin-right: .8em; + border-radius: 4px 0 0 4px; +} + +button { + background-color: #eee; + border: none; + padding: 5px 10px; + border-radius: 4px; + cursor: pointer; + cursor: hand; + font-family: Arial; +} + +button:hover { + background-color: #cfd8dc; +} + +button.delete { + position: relative; + left: 194px; + top: -32px; + background-color: gray !important; + color: white; +} diff --git a/angular-tour-of-heroes/src/app/heroes/heroes.component.html b/angular-tour-of-heroes/src/app/heroes/heroes.component.html index 7cdbc43..6b96c37 100644 --- a/angular-tour-of-heroes/src/app/heroes/heroes.component.html +++ b/angular-tour-of-heroes/src/app/heroes/heroes.component.html @@ -1,10 +1,21 @@

My Heroes

+
+ + + +
diff --git a/angular-tour-of-heroes/src/app/heroes/heroes.component.ts b/angular-tour-of-heroes/src/app/heroes/heroes.component.ts index cbd51b4..4ffb35e 100644 --- a/angular-tour-of-heroes/src/app/heroes/heroes.component.ts +++ b/angular-tour-of-heroes/src/app/heroes/heroes.component.ts @@ -21,4 +21,17 @@ export class HeroesComponent implements OnInit { .subscribe(heroes => this.heroes = heroes); } + add(name: string): void { + name = name.trim(); + if (!name) { return; } + this.heroService.addHero({ name } as Hero) + .subscribe(hero => { + this.heroes.push(hero); + }); + } + + delete(hero: Hero): void { + this.heroes = this.heroes.filter(h => h !== hero); + this.heroService.deleteHero(hero).subscribe(); + } }