|
1 | 1 | 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 | + |
2 | 6 | import { IProduct } from './products';
|
3 | 7 |
|
4 | 8 | @Injectable({
|
5 | 9 | providedIn: 'root'
|
6 | 10 | })
|
7 | 11 | 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 | + } |
8 | 23 |
|
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 | + } |
10 | 30 |
|
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); |
54 | 45 | }
|
| 46 | + |
55 | 47 | }
|
0 commit comments