Skip to content

Instantly share code, notes, and snippets.

Created January 19, 2018 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/bc1782fb2eabd209f423e419a536515e to your computer and use it in GitHub Desktop.
Save anonymous/bc1782fb2eabd209f423e419a536515e to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, tap } from 'rxjs/operators';
export interface Cluster {
id: number;
ext_id: number;
name: string
}
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class ClusterService {
private clustersUrl = 'http://localhost:8000/clusters'; // URL to web api
constructor(
private http: HttpClient
) { }
/** GET obtenemos todos los clusters */
getClusters (): Observable<Cluster[]> {
return this.http.get<Cluster[]>(this.clustersUrl)
.pipe(
tap(clusters => this.log(`fetched clusters`)),
catchError(this.handleError('getClusters', []))
);
}
/** GET obtenemos un cluster por su id. Devolvemos `undefined` cuando no exista */
getClusterNo404<Data>(id: number): Observable<Cluster> {
const url = `${this.clustersUrl}/${id}`;
return this.http.get<Cluster[]>(url)
.pipe(
map(clusters => clusters[0]), // returns a {0|1} element array
tap(h => {
const outcome = h ? `fetched` : `did not find`;
this.log(`${outcome} cluster id=${id}`);
}),
catchError(this.handleError<Cluster>(`getCluster id=${id}`))
);
}
/** POST: añadimos un nuevo cluster */
addCluster (cluster: Cluster): Observable<Cluster> {
return this.http.post<Cluster>(this.clustersUrl, cluster, httpOptions).pipe(
tap((cluster: Cluster) => this.log(`added cluster w/ id=${cluster.id}`)),
catchError(this.handleError<Cluster>('addCluster'))
);
}
/** DELETE: eliminamos un cluster */
deleteCluster (cluster: Cluster | number): Observable<Cluster> {
const id = typeof cluster === 'number' ? cluster : cluster.id;
const url = `${this.clustersUrl}/${id}`;
return this.http.delete<Cluster>(url, httpOptions).pipe(
tap(_ => this.log(`deleted cluster id=${id}`)),
catchError(this.handleError<Cluster>('deleteCluster'))
);
}
/** PUT: actualizamos un cluster */
updateCluster (cluster: Cluster): Observable<any> {
const url = `${this.clustersUrl}/${cluster.id}`;
return this.http.put(url, cluster, httpOptions).pipe(
tap(_ => this.log(`updated cluster id=${cluster.id}`)),
catchError(this.handleError<any>('updateCluster'))
);
}
/**
* Handle Http operation that failed.
* Let the app continue.
* @param operation - name of the operation that failed
* @param result - optional value to return as the observable result
*/
private handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
// 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}`);
// Let the app keep running by returning an empty result.
return of(result as T);
};
}
private log(message: string) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment