mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-22 17:27:48 +00:00
feed table added
This commit is contained in:
@@ -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, MatchViewModel>(match);
|
||||
FeedViewModel _feedVM = Mapper.Map<Feed, FeedViewModel>(match.Feeds.Last());
|
||||
await Clients.All.updateMatch(_matchVM);
|
||||
await Clients.Group(match.Id.ToString()).addFeed(_feedVM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Match, MatchViewModel>()
|
||||
.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<Feed>, ICollection<FeedViewModel>>(m.Feeds)));
|
||||
Mapper.CreateMap<Feed, FeedViewModel>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace LiveGameFeed.Data
|
||||
HostScore = 0,
|
||||
GuestScore = 0,
|
||||
MatchDate = DateTime.Now,
|
||||
Type = MatchTypeEnums.Football,
|
||||
Type = MatchTypeEnums.Basketball,
|
||||
Feeds = new List<Feed>
|
||||
{
|
||||
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<Feed>
|
||||
{
|
||||
new Feed()
|
||||
|
||||
@@ -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<FeedViewModel> Feeds {get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Feed>();
|
||||
}
|
||||
self.matches[i].Feeds.unshift(feed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,30 +34,18 @@
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Feed</th>
|
||||
<th></th>
|
||||
<th>Update</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td class="filterable-cell">Ford</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="filterable-cell">Ford</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="filterable-cell">Ford</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="filterable-cell">Ford</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="filterable-cell">Ford</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="filterable-cell">Ford</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="filterable-cell">Ford</td>
|
||||
<tr *ngFor="let feed of match.Feeds">
|
||||
<td>
|
||||
<span class="feed-time">{{feed.CreatedAt | date:'shortTime' }}</span>
|
||||
</td>
|
||||
<td class="filterable-cell">
|
||||
<span class="feed-update"> {{feed.Description}} </span>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -13,7 +13,7 @@ export class MatchComponent implements OnInit {
|
||||
|
||||
constructor(private feedService: FeedService) { }
|
||||
|
||||
ngOnInit() { }
|
||||
ngOnInit() { }
|
||||
|
||||
subscribe() {
|
||||
console.log(this.match.Id);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<any>;
|
||||
|
||||
updateMatch: Observable<Match>;
|
||||
addFeed: Observable<Feed>;
|
||||
messageReceived: Observable<string>;
|
||||
|
||||
private connectionStateSubject = new Subject<ConnectionState>();
|
||||
private userConnectedSubject = new Subject<any>();
|
||||
|
||||
private updateMatchSubject = new Subject<Match>();
|
||||
private addFeedSubject = new Subject<Feed>();
|
||||
private messageReceivedSubject = new Subject<string>();
|
||||
|
||||
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);
|
||||
|
||||
@@ -27,3 +27,12 @@ th, td {
|
||||
height: 190px;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
.feed-time {
|
||||
color:brown;
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
.feed-update {
|
||||
color: #337ab7;
|
||||
}
|
||||
Reference in New Issue
Block a user