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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using LiveGameFeed.Controllers;
|
using LiveGameFeed.Controllers;
|
||||||
using LiveGameFeed.Core.MvcTimer;
|
using LiveGameFeed.Core.MvcTimer;
|
||||||
@@ -55,56 +56,35 @@ namespace LiveGameFeed.Controllers
|
|||||||
|
|
||||||
foreach (var match in matches)
|
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++;
|
Description = "2 points for " + match.Host + "!",
|
||||||
match.Feeds.Add(new Feed()
|
CreatedAt = DateTime.Now,
|
||||||
{
|
MatchId = match.Id
|
||||||
Description = "Goal for " + match.Host + "!",
|
};
|
||||||
CreatedAt = DateTime.Now,
|
|
||||||
MatchId = match.Id
|
match.Feeds.Add(feed);
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
match.GuestScore++;
|
|
||||||
match.Feeds.Add(new Feed()
|
|
||||||
{
|
|
||||||
Description = "Goal for " + match.Guest + "!",
|
|
||||||
CreatedAt = DateTime.Now,
|
|
||||||
MatchId = match.Id
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (match.Type == MatchTypeEnums.Basketball)
|
else
|
||||||
{
|
{
|
||||||
if (updateHost)
|
match.GuestScore = match.GuestScore + 2;
|
||||||
|
Feed feed = new Feed()
|
||||||
{
|
{
|
||||||
match.HostScore = match.HostScore + 2;
|
Description = "2 points for " + match.Guest + "!",
|
||||||
match.Feeds.Add(new Feed()
|
CreatedAt = DateTime.Now,
|
||||||
{
|
MatchId = match.Id
|
||||||
Description = "2 points for " + match.Host + "!",
|
};
|
||||||
CreatedAt = DateTime.Now,
|
match.Feeds.Add(feed);
|
||||||
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
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_matchRepository.Commit();
|
_matchRepository.Commit();
|
||||||
MatchViewModel _matchVM = Mapper.Map<Match, MatchViewModel>(match);
|
MatchViewModel _matchVM = Mapper.Map<Match, MatchViewModel>(match);
|
||||||
|
FeedViewModel _feedVM = Mapper.Map<Feed, FeedViewModel>(match.Feeds.Last());
|
||||||
await Clients.All.updateMatch(_matchVM);
|
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;
|
using LiveGameFeed.Models;
|
||||||
|
|
||||||
namespace LiveGameFeed.Core.Mappings
|
namespace LiveGameFeed.Core.Mappings
|
||||||
@@ -8,7 +9,9 @@ namespace LiveGameFeed.Core.Mappings
|
|||||||
protected override void Configure()
|
protected override void Configure()
|
||||||
{
|
{
|
||||||
Mapper.CreateMap<Match, MatchViewModel>()
|
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>();
|
Mapper.CreateMap<Feed, FeedViewModel>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace LiveGameFeed.Data
|
|||||||
HostScore = 0,
|
HostScore = 0,
|
||||||
GuestScore = 0,
|
GuestScore = 0,
|
||||||
MatchDate = DateTime.Now,
|
MatchDate = DateTime.Now,
|
||||||
Type = MatchTypeEnums.Football,
|
Type = MatchTypeEnums.Basketball,
|
||||||
Feeds = new List<Feed>
|
Feeds = new List<Feed>
|
||||||
{
|
{
|
||||||
new Feed()
|
new Feed()
|
||||||
@@ -45,7 +45,7 @@ namespace LiveGameFeed.Data
|
|||||||
HostScore = 0,
|
HostScore = 0,
|
||||||
GuestScore = 0,
|
GuestScore = 0,
|
||||||
MatchDate = DateTime.Now,
|
MatchDate = DateTime.Now,
|
||||||
Type = MatchTypeEnums.Football,
|
Type = MatchTypeEnums.Basketball,
|
||||||
Feeds = new List<Feed>
|
Feeds = new List<Feed>
|
||||||
{
|
{
|
||||||
new Feed()
|
new Feed()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace LiveGameFeed.Models
|
namespace LiveGameFeed.Models
|
||||||
{
|
{
|
||||||
@@ -11,5 +12,7 @@ namespace LiveGameFeed.Models
|
|||||||
public int GuestScore { get; set; }
|
public int GuestScore { get; set; }
|
||||||
public DateTime MatchDate { get; set; }
|
public DateTime MatchDate { get; set; }
|
||||||
public string Type { get; set; }
|
public string Type { get; set; }
|
||||||
|
|
||||||
|
public ICollection<FeedViewModel> Feeds {get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
|
||||||
import { FeedService } from '../shared/services/feed.service';
|
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 { DataService } from '../shared/services/data.service';
|
||||||
import { ConnectionState } from '../shared/interfaces';
|
import { ConnectionState } from '../shared/interfaces';
|
||||||
|
|
||||||
@@ -41,12 +41,29 @@ export class HomeComponent implements OnInit {
|
|||||||
this.dataService.getMatches()
|
this.dataService.getMatches()
|
||||||
.subscribe((res: Match[]) => {
|
.subscribe((res: Match[]) => {
|
||||||
self.matches = res;
|
self.matches = res;
|
||||||
|
// Listen for match score updates...
|
||||||
self.feedService.updateMatch.subscribe(
|
self.feedService.updateMatch.subscribe(
|
||||||
match => {
|
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) {
|
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">
|
<table class="table table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Feed</th>
|
<th></th>
|
||||||
|
<th>Update</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr *ngFor="let feed of match.Feeds">
|
||||||
<td class="filterable-cell">Ford</td>
|
<td>
|
||||||
</tr>
|
<span class="feed-time">{{feed.CreatedAt | date:'shortTime' }}</span>
|
||||||
<tr>
|
</td>
|
||||||
<td class="filterable-cell">Ford</td>
|
<td class="filterable-cell">
|
||||||
</tr>
|
<span class="feed-update"> {{feed.Description}} </span>
|
||||||
<tr>
|
</td>
|
||||||
<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>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export class MatchComponent implements OnInit {
|
|||||||
|
|
||||||
constructor(private feedService: FeedService) { }
|
constructor(private feedService: FeedService) { }
|
||||||
|
|
||||||
ngOnInit() { }
|
ngOnInit() { }
|
||||||
|
|
||||||
subscribe() {
|
subscribe() {
|
||||||
console.log(this.match.Id);
|
console.log(this.match.Id);
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ export interface FeedClient {
|
|||||||
userDisconnected: (id: string) => void;
|
userDisconnected: (id: string) => void;
|
||||||
|
|
||||||
updateMatch: (match: Match) => void;
|
updateMatch: (match: Match) => void;
|
||||||
|
addFeed: (feed: Feed) => void;
|
||||||
messageReceived: (message: string) => void;
|
messageReceived: (message: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,4 +36,12 @@ export interface Match {
|
|||||||
GuestScore: number;
|
GuestScore: number;
|
||||||
MatchDate: Date;
|
MatchDate: Date;
|
||||||
Type: string;
|
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 { Observable } from "rxjs/Observable";
|
||||||
import { Subject } from "rxjs/Subject";
|
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()
|
@Injectable()
|
||||||
export class FeedService {
|
export class FeedService {
|
||||||
@@ -15,12 +15,14 @@ export class FeedService {
|
|||||||
userConnected: Observable<any>;
|
userConnected: Observable<any>;
|
||||||
|
|
||||||
updateMatch: Observable<Match>;
|
updateMatch: Observable<Match>;
|
||||||
|
addFeed: Observable<Feed>;
|
||||||
messageReceived: Observable<string>;
|
messageReceived: Observable<string>;
|
||||||
|
|
||||||
private connectionStateSubject = new Subject<ConnectionState>();
|
private connectionStateSubject = new Subject<ConnectionState>();
|
||||||
private userConnectedSubject = new Subject<any>();
|
private userConnectedSubject = new Subject<any>();
|
||||||
|
|
||||||
private updateMatchSubject = new Subject<Match>();
|
private updateMatchSubject = new Subject<Match>();
|
||||||
|
private addFeedSubject = new Subject<Feed>();
|
||||||
private messageReceivedSubject = new Subject<string>();
|
private messageReceivedSubject = new Subject<string>();
|
||||||
|
|
||||||
private server: FeedServer;
|
private server: FeedServer;
|
||||||
@@ -30,6 +32,7 @@ export class FeedService {
|
|||||||
this.userConnected = this.userConnectedSubject.asObservable();
|
this.userConnected = this.userConnectedSubject.asObservable();
|
||||||
|
|
||||||
this.updateMatch = this.updateMatchSubject.asObservable();
|
this.updateMatch = this.updateMatchSubject.asObservable();
|
||||||
|
this.addFeed = this.addFeedSubject.asObservable();
|
||||||
this.messageReceived = this.messageReceivedSubject.asObservable();
|
this.messageReceived = this.messageReceivedSubject.asObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,12 +51,15 @@ export class FeedService {
|
|||||||
feedHub.client.userConnected = user => this.onUserConnected(user);
|
feedHub.client.userConnected = user => this.onUserConnected(user);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @desc callback when a message is received
|
* @desc callback when match score is updated
|
||||||
* @param String to, the conversation id
|
|
||||||
* @param Message data, the message
|
|
||||||
*/
|
*/
|
||||||
feedHub.client.updateMatch = match => this.onUpdateMatch(match);
|
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
|
* @desc callback when a message is received
|
||||||
* @param String to, the conversation id
|
* @param String to, the conversation id
|
||||||
@@ -103,6 +109,10 @@ export class FeedService {
|
|||||||
this.updateMatchSubject.next(match);
|
this.updateMatchSubject.next(match);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private onAddFeed(feed: Feed) {
|
||||||
|
this.addFeedSubject.next(feed);
|
||||||
|
}
|
||||||
|
|
||||||
private onMessageReceived(message: string) {
|
private onMessageReceived(message: string) {
|
||||||
console.log(message);
|
console.log(message);
|
||||||
this.messageReceivedSubject.next(message);
|
this.messageReceivedSubject.next(message);
|
||||||
|
|||||||
@@ -26,4 +26,13 @@ th, td {
|
|||||||
.feed-table {
|
.feed-table {
|
||||||
height: 190px;
|
height: 190px;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.feed-time {
|
||||||
|
color:brown;
|
||||||
|
font-size: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.feed-update {
|
||||||
|
color: #337ab7;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user