diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 4f0658a..7173441 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,15 +1,16 @@ import { ApplicationConfig, importProvidersFrom, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection } from '@angular/core'; import { provideRouter } from '@angular/router'; - import { routes } from './app.routes'; import { InMemoryWebApiModule } from 'angular-in-memory-web-api'; import { InMemoryDataService } from './in-memory-data.service'; +import { provideHttpClient } from '@angular/common/http'; export const appConfig: ApplicationConfig = { providers: [ - provideBrowserGlobalErrorListeners(), + //provideBrowserGlobalErrorListeners(), provideZonelessChangeDetection(), provideRouter(routes), - importProvidersFrom(InMemoryWebApiModule.forRoot(InMemoryDataService, {delay:150})) + importProvidersFrom(InMemoryWebApiModule.forRoot(InMemoryDataService, {delay:150})), + provideHttpClient() ] }; \ No newline at end of file diff --git a/src/app/dashboard/dashboard.component.ts b/src/app/dashboard/dashboard.component.ts index f78c40a..ce9692c 100644 --- a/src/app/dashboard/dashboard.component.ts +++ b/src/app/dashboard/dashboard.component.ts @@ -17,6 +17,7 @@ export class DashboardComponent implements OnInit { ngOnInit(): void { this.getHeroes(); + console.log(this.heroes) } getHeroes(): void { diff --git a/src/app/hero-detail/hero-detail.component.html b/src/app/hero-detail/hero-detail.component.html index d780243..0ea41e1 100644 --- a/src/app/hero-detail/hero-detail.component.html +++ b/src/app/hero-detail/hero-detail.component.html @@ -1,7 +1,4 @@
- - -

{{hero.name | uppercase}} Details

id: {{hero.id}}
@@ -10,3 +7,5 @@
+ + 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 003dca2..0554db9 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -1,6 +1,5 @@ import { Injectable } from '@angular/core'; import { Hero } from './hero'; -import { HEROES } from './mock-heroes'; import { catchError, Observable, of, tap } from 'rxjs'; import { MessageService } from './message.service'; import { HttpClient, HttpHeaders } from '@angular/common/http'; @@ -10,29 +9,25 @@ import { HttpClient, HttpHeaders } from '@angular/common/http'; }) export class HeroService { + +httpOptions = { + headers: new HttpHeaders({ 'Content-Type': 'application/json' }) +}; constructor( private http: HttpClient, private messageService: MessageService) { } - /*getHeroes(): Observable { - const heroes = of(HEROES); - this.messageService.add('HeroService: fetched heroes'); - return heroes; - } */ - /** GET heroes from the server */ getHeroes(): Observable { return this.http.get(this.heroesUrl) .pipe( - tap(_ => this.log('fetched heroes')), + tap(h => this.log(JSON.stringify(h))), catchError(this.handleError('getHeroes', [])) ); } - - /** GET hero by id. Will 404 if id not found */ getHero(id: number): Observable { const url = `${this.heroesUrl}/${id}`; @@ -46,6 +41,7 @@ export class HeroService { private log(message: string) { this.messageService.add(`HeroService: ${message}`); } + private heroesUrl = 'api/heroes'; // URL to web api /** @@ -55,19 +51,28 @@ export class HeroService { * @param operation - name of the operation that failed * @param result - optional value to return as the observable result */ -private handleError(operation = 'operation', result?: T) { - return (error: any): Observable => { + private handleError(operation = 'operation', result?: T) { + return (error: any): Observable => { - // TODO: send the error to remote logging infrastructure - console.error(error); // log to console instead + // TODO: send the error to remote logging infrastructure + console.error(error); // log to console instead - // TODO: better job of transforming error for user consumption - this.log(`${operation} failed: ${error.message}`); + // TODO: better job of transforming error for user consumption + this.log(`${operation} failed: ${error.message}`); - // Let the app keep running by returning an empty result. - return of(result as T); - }; + // Let the app keep running by returning an empty result. + return of(result as T); + }; + + } + + /** 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')) + ); } - + } diff --git a/src/app/heroes/heroes.component.ts b/src/app/heroes/heroes.component.ts index 37a6802..8180f3d 100644 --- a/src/app/heroes/heroes.component.ts +++ b/src/app/heroes/heroes.component.ts @@ -3,7 +3,6 @@ import { Hero } from '../hero'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { HeroService } from '../hero.service'; //новая библиотека добавлена вместо HEROES -import { HeroDetailComponent } from '../hero-detail/hero-detail.component'; import { MessageService } from '../message.service'; import { RouterLink } from '@angular/router'; @@ -26,8 +25,6 @@ export class HeroesComponent implements OnInit { this.getHeroes(); } - - getHeroes(): void { this.heroService.getHeroes() .subscribe(heroes => this.heroes = heroes);