Skip to content

Commit

Permalink
Modified Suppliers to Match
Browse files Browse the repository at this point in the history
  • Loading branch information
DeborahK committed Mar 18, 2019
1 parent a033344 commit 1cc44a0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ProductService } from '../product.service';
export class ProductListComponent implements OnInit {
pageTitle = 'Products';
errorMessage: string;

products$ = this.productService.productsWithCategory$.pipe(
catchError(error => {
this.errorMessage = error;
Expand Down
4 changes: 1 addition & 3 deletions APM/src/app/products/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export class ProductService {
// Expose the selectedProduct as an observable for use by any components
selectedProductChanges$ = this.selectedProductSource.asObservable();

// LIST OF STREAMS

// All products
// Instead of defining the http.get in a method in the service,
// set the observable directly
Expand Down Expand Up @@ -66,7 +64,7 @@ export class ProductService {
products.find(product => product.id === selectedProductId)
),
tap(product => console.log('changeSelectedProduct', product)),
shareReplay({ bufferSize: 1, refCount: false })
shareReplay()
);

// filter(Boolean) checks for nulls, which casts anything it gets to a Boolean.
Expand Down
95 changes: 47 additions & 48 deletions APM/src/app/suppliers/supplier.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,60 @@ import { catchError, tap } from 'rxjs/operators';
import { Supplier } from './supplier';

@Injectable({
providedIn: 'root'
providedIn: 'root'
})
export class SupplierService {
private suppliersUrl = 'api/suppliers';
private suppliersUrl = 'api/suppliers';

constructor(private http: HttpClient) { }
constructor(private http: HttpClient) { }

// Gets all suppliers
private getSuppliers(): Observable<Supplier[]> {
return this.http.get<Supplier[]>(this.suppliersUrl)
.pipe(
tap(data => console.log('getSuppliers: ', JSON.stringify(data))),
catchError(this.handleError)
);
}
// All Suppliers
suppliers$ = this.http.get<Supplier[]>(this.suppliersUrl)
.pipe(
tap(data => console.log('getSuppliers: ', JSON.stringify(data))),
catchError(this.handleError)
);

// Gets set of suppliers given a set of ids
getSuppliersByIds(ids: number[]): Observable<Supplier[]> {
// Build the list of http calls
const calls: Observable<Supplier>[] = [];
ids.map(id => {
const url = `${this.suppliersUrl}/${id}`;
calls.push(this.http.get<Supplier>(url));
});
// Join the calls
return forkJoin(calls).pipe(
tap(data => console.log('getSuppliersByIds: ', JSON.stringify(data))),
catchError(this.handleError)
);
}
// Gets set of suppliers given a set of ids
// This has to be a method because it has a parameter.
getSuppliersByIds(ids: number[]): Observable<Supplier[]> {
// Build the list of http calls
const calls: Observable<Supplier>[] = [];
ids.map(id => {
const url = `${this.suppliersUrl}/${id}`;
calls.push(this.http.get<Supplier>(url));
});
// Join the calls
return forkJoin(calls).pipe(
tap(data => console.log('getSuppliersByIds: ', JSON.stringify(data))),
catchError(this.handleError)
);
}

// Gets a single supplier by id
private getSupplier(id: number): Observable<Supplier> {
const url = `${this.suppliersUrl}/${id}`;
return this.http.get<Supplier>(url)
.pipe(
tap(data => console.log('getSupplier: ', JSON.stringify(data))),
catchError(this.handleError)
);
}
// Gets a single supplier by id
private getSupplier(id: number): Observable<Supplier> {
const url = `${this.suppliersUrl}/${id}`;
return this.http.get<Supplier>(url)
.pipe(
tap(data => console.log('getSupplier: ', JSON.stringify(data))),
catchError(this.handleError)
);
}

private handleError(err) {
// in a real world app, we may send the server to some remote logging infrastructure
// instead of just logging it to the console
let errorMessage: string;
if (err.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
errorMessage = `An error occurred: ${err.error.message}`;
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
errorMessage = `Backend returned code ${err.status}: ${err.body.error}`;
}
console.error(err);
return throwError(errorMessage);
private handleError(err) {
// in a real world app, we may send the server to some remote logging infrastructure
// instead of just logging it to the console
let errorMessage: string;
if (err.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
errorMessage = `An error occurred: ${err.error.message}`;
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
errorMessage = `Backend returned code ${err.status}: ${err.body.error}`;
}
console.error(err);
return throwError(errorMessage);
}

}

0 comments on commit 1cc44a0

Please sign in to comment.