analys
This commit is contained in:
parent
299aeb7d14
commit
34ef92e03f
|
@ -1,15 +1,16 @@
|
||||||
import { ApplicationConfig, importProvidersFrom, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection } from '@angular/core';
|
import { ApplicationConfig, importProvidersFrom, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection } from '@angular/core';
|
||||||
import { provideRouter } from '@angular/router';
|
import { provideRouter } from '@angular/router';
|
||||||
|
|
||||||
import { routes } from './app.routes';
|
import { routes } from './app.routes';
|
||||||
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
|
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
|
||||||
import { InMemoryDataService } from './in-memory-data.service';
|
import { InMemoryDataService } from './in-memory-data.service';
|
||||||
|
import { provideHttpClient } from '@angular/common/http';
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [
|
providers: [
|
||||||
provideBrowserGlobalErrorListeners(),
|
//provideBrowserGlobalErrorListeners(),
|
||||||
provideZonelessChangeDetection(),
|
provideZonelessChangeDetection(),
|
||||||
provideRouter(routes),
|
provideRouter(routes),
|
||||||
importProvidersFrom(InMemoryWebApiModule.forRoot(InMemoryDataService, {delay:150}))
|
importProvidersFrom(InMemoryWebApiModule.forRoot(InMemoryDataService, {delay:150})),
|
||||||
|
provideHttpClient()
|
||||||
]
|
]
|
||||||
};
|
};
|
|
@ -17,6 +17,7 @@ export class DashboardComponent implements OnInit {
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.getHeroes();
|
this.getHeroes();
|
||||||
|
console.log(this.heroes)
|
||||||
}
|
}
|
||||||
|
|
||||||
getHeroes(): void {
|
getHeroes(): void {
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
<div *ngIf="hero">
|
<div *ngIf="hero">
|
||||||
|
|
||||||
<button type="button" (click)="goBack()">go back</button>
|
|
||||||
|
|
||||||
<h2>{{hero.name | uppercase}} Details</h2>
|
<h2>{{hero.name | uppercase}} Details</h2>
|
||||||
<div><span>id: </span>{{hero.id}}</div>
|
<div><span>id: </span>{{hero.id}}</div>
|
||||||
<div>
|
<div>
|
||||||
|
@ -10,3 +7,5 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
<button type="button" (click)="goBack()">go back</button>
|
||||||
|
<button type="button" (click)="save()">save</button>
|
||||||
|
|
|
@ -39,4 +39,11 @@ export class HeroDetailComponent {
|
||||||
.subscribe(hero => this.hero = hero);
|
.subscribe(hero => this.hero = hero);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
save(): void {
|
||||||
|
if (this.hero) {
|
||||||
|
this.heroService.updateHero(this.hero)
|
||||||
|
.subscribe(() => this.goBack());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Hero } from './hero';
|
import { Hero } from './hero';
|
||||||
import { HEROES } from './mock-heroes';
|
|
||||||
import { catchError, Observable, of, tap } from 'rxjs';
|
import { catchError, Observable, of, tap } from 'rxjs';
|
||||||
import { MessageService } from './message.service';
|
import { MessageService } from './message.service';
|
||||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||||
|
@ -10,29 +9,25 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||||
})
|
})
|
||||||
|
|
||||||
export class HeroService {
|
export class HeroService {
|
||||||
|
|
||||||
|
httpOptions = {
|
||||||
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
|
||||||
|
};
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private http: HttpClient,
|
private http: HttpClient,
|
||||||
private messageService: MessageService) { }
|
private messageService: MessageService) { }
|
||||||
|
|
||||||
/*getHeroes(): Observable<Hero[]> {
|
|
||||||
const heroes = of(HEROES);
|
|
||||||
this.messageService.add('HeroService: fetched heroes');
|
|
||||||
return heroes;
|
|
||||||
} */
|
|
||||||
|
|
||||||
|
|
||||||
/** GET heroes from the server */
|
/** GET heroes from the server */
|
||||||
getHeroes(): Observable<Hero[]> {
|
getHeroes(): Observable<Hero[]> {
|
||||||
return this.http.get<Hero[]>(this.heroesUrl)
|
return this.http.get<Hero[]>(this.heroesUrl)
|
||||||
.pipe(
|
.pipe(
|
||||||
tap(_ => this.log('fetched heroes')),
|
tap(h => this.log(JSON.stringify(h))),
|
||||||
catchError(this.handleError<Hero[]>('getHeroes', []))
|
catchError(this.handleError<Hero[]>('getHeroes', []))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** GET hero by id. Will 404 if id not found */
|
/** GET hero by id. Will 404 if id not found */
|
||||||
getHero(id: number): Observable<Hero> {
|
getHero(id: number): Observable<Hero> {
|
||||||
const url = `${this.heroesUrl}/${id}`;
|
const url = `${this.heroesUrl}/${id}`;
|
||||||
|
@ -46,6 +41,7 @@ export class HeroService {
|
||||||
private log(message: string) {
|
private log(message: string) {
|
||||||
this.messageService.add(`HeroService: ${message}`);
|
this.messageService.add(`HeroService: ${message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private heroesUrl = 'api/heroes'; // URL to web api
|
private heroesUrl = 'api/heroes'; // URL to web api
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,19 +51,28 @@ export class HeroService {
|
||||||
* @param operation - name of the operation that failed
|
* @param operation - name of the operation that failed
|
||||||
* @param result - optional value to return as the observable result
|
* @param result - optional value to return as the observable result
|
||||||
*/
|
*/
|
||||||
private handleError<T>(operation = 'operation', result?: T) {
|
private handleError<T>(operation = 'operation', result?: T) {
|
||||||
return (error: any): Observable<T> => {
|
return (error: any): Observable<T> => {
|
||||||
|
|
||||||
// TODO: send the error to remote logging infrastructure
|
// TODO: send the error to remote logging infrastructure
|
||||||
console.error(error); // log to console instead
|
console.error(error); // log to console instead
|
||||||
|
|
||||||
// TODO: better job of transforming error for user consumption
|
// TODO: better job of transforming error for user consumption
|
||||||
this.log(`${operation} failed: ${error.message}`);
|
this.log(`${operation} failed: ${error.message}`);
|
||||||
|
|
||||||
// Let the app keep running by returning an empty result.
|
// Let the app keep running by returning an empty result.
|
||||||
return of(result as T);
|
return of(result as T);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/** PUT: update the hero on the server */
|
||||||
|
updateHero(hero: Hero): Observable<any> {
|
||||||
|
return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(
|
||||||
|
tap(_ => this.log(`updated hero id=${hero.id}`)),
|
||||||
|
catchError(this.handleError<any>('updateHero'))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ import { Hero } from '../hero';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { FormsModule } from '@angular/forms';
|
import { FormsModule } from '@angular/forms';
|
||||||
import { HeroService } from '../hero.service'; //новая библиотека добавлена вместо HEROES
|
import { HeroService } from '../hero.service'; //новая библиотека добавлена вместо HEROES
|
||||||
import { HeroDetailComponent } from '../hero-detail/hero-detail.component';
|
|
||||||
import { MessageService } from '../message.service';
|
import { MessageService } from '../message.service';
|
||||||
import { RouterLink } from '@angular/router';
|
import { RouterLink } from '@angular/router';
|
||||||
|
|
||||||
|
@ -26,8 +25,6 @@ export class HeroesComponent implements OnInit {
|
||||||
this.getHeroes();
|
this.getHeroes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
getHeroes(): void {
|
getHeroes(): void {
|
||||||
this.heroService.getHeroes()
|
this.heroService.getHeroes()
|
||||||
.subscribe(heroes => this.heroes = heroes);
|
.subscribe(heroes => this.heroes = heroes);
|
||||||
|
|
Loading…
Reference in New Issue