From 66d1a2a07fddf4c93ad1615eaa25a80c3be9987f Mon Sep 17 00:00:00 2001 From: chsakell Date: Mon, 3 Oct 2016 13:09:12 +0300 Subject: [PATCH] feed table added --- Controllers/HomeController.cs | 60 +++++++------------ .../DomainToViewModelMappingProfile.cs | 7 ++- Data/LiveGameDbInitializer.cs | 4 +- Models/MatchViewModel.cs | 3 + app/home/home.component.ts | 25 ++++++-- app/shared/components/match.component.html | 30 +++------- app/shared/components/match.component.ts | 2 +- app/shared/interfaces.ts | 9 +++ app/shared/services/feed.service.ts | 18 ++++-- wwwroot/css/site.css | 9 +++ 10 files changed, 93 insertions(+), 74 deletions(-) diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index c7fa6fc..e75f277 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using AutoMapper; using LiveGameFeed.Controllers; using LiveGameFeed.Core.MvcTimer; @@ -55,56 +56,35 @@ namespace LiveGameFeed.Controllers foreach (var match in matches) { - if (match.Type == MatchTypeEnums.Football) + if (updateHost) { - if (updateHost) + match.HostScore = match.HostScore + 2; + Feed feed = new Feed() { - match.HostScore++; - match.Feeds.Add(new Feed() - { - Description = "Goal for " + match.Host + "!", - CreatedAt = DateTime.Now, - MatchId = match.Id - }); - } - else - { - match.GuestScore++; - match.Feeds.Add(new Feed() - { - Description = "Goal for " + match.Guest + "!", - CreatedAt = DateTime.Now, - MatchId = match.Id - }); - } + Description = "2 points for " + match.Host + "!", + CreatedAt = DateTime.Now, + MatchId = match.Id + }; + + match.Feeds.Add(feed); } - else if (match.Type == MatchTypeEnums.Basketball) + else { - if (updateHost) + match.GuestScore = match.GuestScore + 2; + Feed feed = new Feed() { - match.HostScore = match.HostScore + 2; - match.Feeds.Add(new Feed() - { - Description = "2 points for " + match.Host + "!", - CreatedAt = DateTime.Now, - MatchId = match.Id - }); - } - else - { - match.GuestScore = match.GuestScore + 2; - match.Feeds.Add(new Feed() - { - Description = "2 points for " + match.Guest + "!", - CreatedAt = DateTime.Now, - MatchId = match.Id - }); - } + Description = "2 points for " + match.Guest + "!", + CreatedAt = DateTime.Now, + MatchId = match.Id + }; + match.Feeds.Add(feed); } _matchRepository.Commit(); MatchViewModel _matchVM = Mapper.Map(match); + FeedViewModel _feedVM = Mapper.Map(match.Feeds.Last()); await Clients.All.updateMatch(_matchVM); + await Clients.Group(match.Id.ToString()).addFeed(_feedVM); } } } diff --git a/Core/Mappings/DomainToViewModelMappingProfile.cs b/Core/Mappings/DomainToViewModelMappingProfile.cs index 4c60c49..aa4db2a 100644 --- a/Core/Mappings/DomainToViewModelMappingProfile.cs +++ b/Core/Mappings/DomainToViewModelMappingProfile.cs @@ -1,4 +1,5 @@ -using AutoMapper; +using System.Collections.Generic; +using AutoMapper; using LiveGameFeed.Models; namespace LiveGameFeed.Core.Mappings @@ -8,7 +9,9 @@ namespace LiveGameFeed.Core.Mappings protected override void Configure() { Mapper.CreateMap() - .ForMember(m => m.Type, map => map.MapFrom(m => m.Type.ToString())); + .ForMember(vm => vm.Type, map => map.MapFrom(m => m.Type.ToString())) + .ForMember(vm => vm.Feeds, map => map.MapFrom(m => + Mapper.Map, ICollection>(m.Feeds))); Mapper.CreateMap(); } } diff --git a/Data/LiveGameDbInitializer.cs b/Data/LiveGameDbInitializer.cs index 02f77b0..6d9a0a6 100644 --- a/Data/LiveGameDbInitializer.cs +++ b/Data/LiveGameDbInitializer.cs @@ -27,7 +27,7 @@ namespace LiveGameFeed.Data HostScore = 0, GuestScore = 0, MatchDate = DateTime.Now, - Type = MatchTypeEnums.Football, + Type = MatchTypeEnums.Basketball, Feeds = new List { new Feed() @@ -45,7 +45,7 @@ namespace LiveGameFeed.Data HostScore = 0, GuestScore = 0, MatchDate = DateTime.Now, - Type = MatchTypeEnums.Football, + Type = MatchTypeEnums.Basketball, Feeds = new List { new Feed() diff --git a/Models/MatchViewModel.cs b/Models/MatchViewModel.cs index 97ed78a..a6bc5d9 100644 --- a/Models/MatchViewModel.cs +++ b/Models/MatchViewModel.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace LiveGameFeed.Models { @@ -11,5 +12,7 @@ namespace LiveGameFeed.Models public int GuestScore { get; set; } public DateTime MatchDate { get; set; } public string Type { get; set; } + + public ICollection Feeds {get; set; } } } diff --git a/app/home/home.component.ts b/app/home/home.component.ts index e0eaa52..3eb0fe6 100644 --- a/app/home/home.component.ts +++ b/app/home/home.component.ts @@ -1,7 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { FeedService } from '../shared/services/feed.service'; -import { Match } from '../shared/interfaces'; +import { Match, Feed } from '../shared/interfaces'; import { DataService } from '../shared/services/data.service'; import { ConnectionState } from '../shared/interfaces'; @@ -41,12 +41,29 @@ export class HomeComponent implements OnInit { this.dataService.getMatches() .subscribe((res: Match[]) => { self.matches = res; + // Listen for match score updates... self.feedService.updateMatch.subscribe( match => { - for(var i=0; i< self.matches.length; i++) - { + for (var i = 0; i < self.matches.length; i++) { if (self.matches[i].Id === match.Id) { - self.matches[i] = match; + self.matches[i].HostScore = match.HostScore; + self.matches[i].GuestScore = match.GuestScore; + } + } + } + ); + + // Listen for subscribed feed updates.. + self.feedService.addFeed.subscribe( + feed => { + console.log(feed); + for (var i = 0; i < self.matches.length; i++) { + if (self.matches[i].Id === feed.MatchId) { + if (!self.matches[i].Feeds) { + console.log('initializing for match ' + self.matches[i].Id); + self.matches[i].Feeds = new Array(); + } + self.matches[i].Feeds.unshift(feed); } } } diff --git a/app/shared/components/match.component.html b/app/shared/components/match.component.html index b055f10..e2fdfe9 100644 --- a/app/shared/components/match.component.html +++ b/app/shared/components/match.component.html @@ -34,30 +34,18 @@ - + + - - - - - - - - - - - - - - - - - - - - + + +
FeedUpdate
Ford
Ford
Ford
Ford
Ford
Ford
Ford
+ {{feed.CreatedAt | date:'shortTime' }} + + {{feed.Description}} +
diff --git a/app/shared/components/match.component.ts b/app/shared/components/match.component.ts index 94dfdec..7d957db 100644 --- a/app/shared/components/match.component.ts +++ b/app/shared/components/match.component.ts @@ -13,7 +13,7 @@ export class MatchComponent implements OnInit { constructor(private feedService: FeedService) { } - ngOnInit() { } + ngOnInit() { } subscribe() { console.log(this.match.Id); diff --git a/app/shared/interfaces.ts b/app/shared/interfaces.ts index 4cd9830..d8d4ca7 100644 --- a/app/shared/interfaces.ts +++ b/app/shared/interfaces.ts @@ -13,6 +13,7 @@ export interface FeedClient { userDisconnected: (id: string) => void; updateMatch: (match: Match) => void; + addFeed: (feed: Feed) => void; messageReceived: (message: string) => void; } @@ -35,4 +36,12 @@ export interface Match { GuestScore: number; MatchDate: Date; Type: string; + Feeds: Feed[]; +} + +export interface Feed { + Id: number; + Description: string; + CreatedAt: Date; + MatchId: number; } \ No newline at end of file diff --git a/app/shared/services/feed.service.ts b/app/shared/services/feed.service.ts index 379fc3c..00eeece 100644 --- a/app/shared/services/feed.service.ts +++ b/app/shared/services/feed.service.ts @@ -5,7 +5,7 @@ import 'rxjs/add/operator/toPromise'; import { Observable } from "rxjs/Observable"; import { Subject } from "rxjs/Subject"; -import { FeedSignalR, FeedProxy, FeedClient, FeedServer, ConnectionState, Match } from '../interfaces'; +import { FeedSignalR, FeedProxy, FeedClient, FeedServer, ConnectionState, Match, Feed } from '../interfaces'; @Injectable() export class FeedService { @@ -15,12 +15,14 @@ export class FeedService { userConnected: Observable; updateMatch: Observable; + addFeed: Observable; messageReceived: Observable; private connectionStateSubject = new Subject(); private userConnectedSubject = new Subject(); private updateMatchSubject = new Subject(); + private addFeedSubject = new Subject(); private messageReceivedSubject = new Subject(); private server: FeedServer; @@ -30,6 +32,7 @@ export class FeedService { this.userConnected = this.userConnectedSubject.asObservable(); this.updateMatch = this.updateMatchSubject.asObservable(); + this.addFeed = this.addFeedSubject.asObservable(); this.messageReceived = this.messageReceivedSubject.asObservable(); } @@ -48,12 +51,15 @@ export class FeedService { feedHub.client.userConnected = user => this.onUserConnected(user); /** - * @desc callback when a message is received - * @param String to, the conversation id - * @param Message data, the message + * @desc callback when match score is updated */ feedHub.client.updateMatch = match => this.onUpdateMatch(match); + /** + * @desc callback when a feed is added + */ + feedHub.client.addFeed = feed => this.onAddFeed(feed); + /** * @desc callback when a message is received * @param String to, the conversation id @@ -103,6 +109,10 @@ export class FeedService { this.updateMatchSubject.next(match); } + private onAddFeed(feed: Feed) { + this.addFeedSubject.next(feed); + } + private onMessageReceived(message: string) { console.log(message); this.messageReceivedSubject.next(message); diff --git a/wwwroot/css/site.css b/wwwroot/css/site.css index 2656143..a766d32 100644 --- a/wwwroot/css/site.css +++ b/wwwroot/css/site.css @@ -26,4 +26,13 @@ th, td { .feed-table { height: 190px; overflow: auto; +} + +.feed-time { + color:brown; + font-size: 10px; +} + +.feed-update { + color: #337ab7; } \ No newline at end of file