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,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR.Infrastructure;
using LiveGameFeed.Hubs;
using LiveGameFeed.Data.Abstract;
using LiveGameFeed.Models;
namespace LiveGameFeed.Controllers
{
[Route("api/[controller]")]
public class MatchesController : ApiHubController<Broadcaster>
{
IMatchRepository _matchRepository;
public MatchesController(
IConnectionManager signalRConnectionManager,
IMatchRepository matchRepository)
: base(signalRConnectionManager)
{
_matchRepository = matchRepository;
}
// GET api/values
[HttpGet]
public IEnumerable<Match> Get()
{
IEnumerable<Match> _matches = _matchRepository.GetAll();
return _matches;
}
// GET api/values/5
[HttpGet("{id}")]
public Match Get(int id)
{
return _matchRepository.GetSingle(id);
}
// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

View File

@@ -4,6 +4,8 @@ import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { ConfigService } from './shared/config.service';
import { DataService } from './shared/data.service';
import { HomeComponent } from './home/home.component';
import { routing } from './app.routes';
@@ -18,6 +20,10 @@ import { routing } from './app.routes';
AppComponent,
HomeComponent
],
bootstrap: [AppComponent]
bootstrap: [AppComponent],
providers: [
ConfigService,
DataService
]
})
export class AppModule { }

View File

@@ -1,6 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { FeedService } from '../shared/feed.service';
import { Match } from '../shared/interfaces';
import { DataService } from '../shared/data.service';
import { ConnectionState } from '../shared/interfaces';
@Component({
@@ -9,16 +11,21 @@ import { ConnectionState } from '../shared/interfaces';
})
export class HomeComponent implements OnInit {
matches: Match[];
error: any;
constructor(private service: FeedService) { }
constructor(private dataService: DataService,
private feedService: FeedService) { }
ngOnInit() {
this.service.connectionState
let self = this;
self.feedService.connectionState
.subscribe(
connectionState => {
if (connectionState == ConnectionState.Connected) {
console.log('Connected!');
self.loadMatches();
} else {
console.log(connectionState.toString());
}
@@ -27,12 +34,17 @@ export class HomeComponent implements OnInit {
this.error = error;
console.log(error);
});
}
if (this.service.currentState === ConnectionState.Connected) {
console.log(' connected....');
}
else {
console.log('not connected');
}
loadMatches(): void {
let self = this;
this.dataService.getMatches()
.subscribe((res: Match[]) => {
self.matches = res;
console.log(self.matches);
},
error => {
console.log(error);
});
}
}

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

@@ -18,3 +18,15 @@ export enum ConnectionState {
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
}