mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-22 17:27:48 +00:00
added data service
This commit is contained in:
19
app/shared/config.service.ts
Normal file
19
app/shared/config.service.ts
Normal 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/','');
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user