Skip to content

Commit 24f3573

Browse files
committed
Module 10: Retrieving Data Using HTTP.
1 parent dd6ec4b commit 24f3573

File tree

3 files changed

+48
-49
lines changed

3 files changed

+48
-49
lines changed

APM/src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33
import { FormsModule} from '@angular/forms';
4+
import { HttpClientModule } from '@angular/common/http';
45

56
import { AppComponent } from './app.component';
67
import { ProductListComponent } from './products/product-list.component';
@@ -17,6 +18,7 @@ import { StarComponent } from './shared/star.component';
1718
imports: [
1819
BrowserModule,
1920
FormsModule,
21+
HttpClientModule,
2022
],
2123
bootstrap: [AppComponent]
2224
})

APM/src/app/products/product-list.component.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ export class ProductListComponent implements OnInit {
1212
imageWidth: number=50;
1313
imageMargin: number=2;
1414
showImage: boolean=false;
15+
errorMessage: string = '';
1516

16-
_listFilter: string;
17+
_listFilter: string = '';
1718
get listFilter(): string {
1819
return this._listFilter;
1920
}
@@ -46,9 +47,13 @@ export class ProductListComponent implements OnInit {
4647
this.showImage = !this.showImage;
4748
}
4849

49-
ngOnInit(): void
50-
{
51-
this.products = this.productService.getProducts();
52-
this.filteredProducts = this.products;
50+
ngOnInit(): void {
51+
this.productService.getProducts().subscribe({
52+
next: products => {
53+
this.products = products;
54+
this.filteredProducts = this.products;
55+
},
56+
error: err => this.errorMessage = err
57+
});
5358
}
5459
}
Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,47 @@
11
import { Injectable } from '@angular/core';
2+
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
3+
import { Observable, throwError } from 'rxjs';
4+
import { catchError, tap, map } from 'rxjs/operators';
5+
26
import { IProduct } from './products';
37

48
@Injectable({
59
providedIn: 'root'
610
})
711
export class ProductService {
12+
private productUrl = 'api/products/products.json';
13+
14+
constructor(private http: HttpClient) { }
15+
16+
getProducts(): Observable<IProduct[]> {
17+
return this.http.get<IProduct[]>(this.productUrl)
18+
.pipe(
19+
tap(data => console.log('All: ' + JSON.stringify(data))),
20+
catchError(this.handleError)
21+
);
22+
}
823

9-
constructor() { }
24+
getProduct(id: number): Observable<IProduct | undefined> {
25+
return this.getProducts()
26+
.pipe(
27+
map((products: IProduct[]) => products.find(p => p.productId === id))
28+
);
29+
}
1030

11-
getProducts(): IProduct[]{
12-
return [
13-
{
14-
"productId": 1,
15-
"productName": "Leaf Rake",
16-
"productCode": "GDN-0011",
17-
"releaseDate": "March 19, 2019",
18-
"description": "Leaf rake with 48-inch wooden handle.",
19-
"price": 19.95,
20-
"starRating": 3.2,
21-
"imageUrl": "assets/images/leaf_rake.png"
22-
},
23-
{
24-
"productId": 2,
25-
"productName": "Garden Cart",
26-
"productCode": "GDN-0023",
27-
"releaseDate": "March 18, 2019",
28-
"description": "15 gallon capacity rolling garden cart",
29-
"price": 32.99,
30-
"starRating": 4.2,
31-
"imageUrl": "assets/images/garden_cart.png"
32-
},
33-
{
34-
"productId": 5,
35-
"productName": "Hammer",
36-
"productCode": "TBX-0048",
37-
"releaseDate": "May 21, 2019",
38-
"description": "Curved claw steel hammer",
39-
"price": 8.9,
40-
"starRating": 4.8,
41-
"imageUrl": "assets/images/hammer.png"
42-
},
43-
{
44-
"productId": 8,
45-
"productName": "Saw",
46-
"productCode": "TBX-0022",
47-
"releaseDate": "May 15, 2019",
48-
"description": "15-inch steel blade hand saw",
49-
"price": 11.55,
50-
"starRating": 3.7,
51-
"imageUrl": "assets/images/saw.png"
52-
},
53-
]
31+
private handleError(err: HttpErrorResponse) {
32+
// in a real world app, we may send the server to some remote logging infrastructure
33+
// instead of just logging it to the console
34+
let errorMessage = '';
35+
if (err.error instanceof ErrorEvent) {
36+
// A client-side or network error occurred. Handle it accordingly.
37+
errorMessage = `An error occurred: ${err.error.message}`;
38+
} else {
39+
// The backend returned an unsuccessful response code.
40+
// The response body may contain clues as to what went wrong,
41+
errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
42+
}
43+
console.error(errorMessage);
44+
return throwError(errorMessage);
5445
}
46+
5547
}

0 commit comments

Comments
 (0)