added data service

This commit is contained in:
chsakell
2016-09-29 15:50:28 +03:00
parent e6aed79fa9
commit ac613432aa
6 changed files with 158 additions and 9 deletions

View File

@@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
@Injectable()
export class ConfigService {
_apiURI : string;
constructor() {
this._apiURI = 'http://localhost:5000/api/';
}
getApiURI() {
return this._apiURI;
}
getApiHost() {
return this._apiURI.replace('api/','');
}
}

View 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);
}
}

View File

@@ -17,4 +17,16 @@ export enum ConnectionState {
Connected = 1,
Disconnected = 2,
Error = 3
}
/* LiveGameFeed related interfaces */
export interface Match {
id: number;
host: string;
guest: string;
hostScore: number;
guestScore: number;
matchDate: Date;
league: string;
feeds: any
}