From ac613432aa048931b949ee661186a4e59e65d33c Mon Sep 17 00:00:00 2001 From: chsakell Date: Thu, 29 Sep 2016 15:50:28 +0300 Subject: [PATCH] added data service --- Controllers/MatchesController.cs | 59 ++++++++++++++++++++++++++++++++ app/app.module.ts | 8 ++++- app/home/home.component.ts | 28 ++++++++++----- app/shared/config.service.ts | 19 ++++++++++ app/shared/data.service.ts | 41 ++++++++++++++++++++++ app/shared/interfaces.ts | 12 +++++++ 6 files changed, 158 insertions(+), 9 deletions(-) create mode 100644 Controllers/MatchesController.cs create mode 100644 app/shared/config.service.ts create mode 100644 app/shared/data.service.ts diff --git a/Controllers/MatchesController.cs b/Controllers/MatchesController.cs new file mode 100644 index 0000000..f5eca1a --- /dev/null +++ b/Controllers/MatchesController.cs @@ -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 + { + IMatchRepository _matchRepository; + public MatchesController( + IConnectionManager signalRConnectionManager, + IMatchRepository matchRepository) + : base(signalRConnectionManager) + { + _matchRepository = matchRepository; + } + + // GET api/values + [HttpGet] + public IEnumerable Get() + { + IEnumerable _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) + { + } + } +} diff --git a/app/app.module.ts b/app/app.module.ts index 9d1f7e9..be3fd0d 100644 --- a/app/app.module.ts +++ b/app/app.module.ts @@ -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 { } diff --git a/app/home/home.component.ts b/app/home/home.component.ts index 5da5a32..f87ab06 100644 --- a/app/home/home.component.ts +++ b/app/home/home.component.ts @@ -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); + }); } } \ No newline at end of file diff --git a/app/shared/config.service.ts b/app/shared/config.service.ts new file mode 100644 index 0000000..fd006f7 --- /dev/null +++ b/app/shared/config.service.ts @@ -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/',''); + } +} \ No newline at end of file diff --git a/app/shared/data.service.ts b/app/shared/data.service.ts new file mode 100644 index 0000000..d58cafd --- /dev/null +++ b/app/shared/data.service.ts @@ -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 { + 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); + } +} \ No newline at end of file diff --git a/app/shared/interfaces.ts b/app/shared/interfaces.ts index c69b3f3..9fa649d 100644 --- a/app/shared/interfaces.ts +++ b/app/shared/interfaces.ts @@ -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 } \ No newline at end of file