mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-23 01:37:59 +00:00
added data service
This commit is contained in:
41
app/shared/data.service.ts
Normal file
41
app/shared/data.service.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Http, Response, Headers } from '@angular/http';
|
||||
//Grab everything with import 'rxjs/Rx';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import {Observer} from 'rxjs/Observer';
|
||||
import 'rxjs/add/operator/map';
|
||||
import 'rxjs/add/operator/catch';
|
||||
|
||||
import { Match } from './interfaces';
|
||||
import { ConfigService } from './config.service';
|
||||
|
||||
@Injectable()
|
||||
export class DataService {
|
||||
|
||||
_baseUrl: string = '';
|
||||
|
||||
constructor(private http: Http,
|
||||
private configService: ConfigService) {
|
||||
this._baseUrl = configService.getApiURI();
|
||||
}
|
||||
|
||||
getMatches(): Observable<Match[]> {
|
||||
return this.http.get(this._baseUrl + 'matches')
|
||||
.map(this.extractData)
|
||||
.catch(this.handleError);
|
||||
}
|
||||
|
||||
private extractData(res: Response) {
|
||||
let body = res.json();
|
||||
return body || {};
|
||||
}
|
||||
|
||||
private handleError(error: any) {
|
||||
// In a real world app, we might use a remote logging infrastructure
|
||||
// We'd also dig deeper into the error to get a better message
|
||||
let errMsg = (error.message) ? error.message :
|
||||
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
||||
console.error(errMsg); // log to console instead
|
||||
return Observable.throw(errMsg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user