update score feed moved to API controller

This commit is contained in:
chsakell
2016-10-04 13:19:55 +03:00
parent 182d2d205f
commit e6669deb25
2 changed files with 41 additions and 17 deletions

View File

@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.SignalR.Infrastructure;
using LiveGameFeed.Hubs;
using LiveGameFeed.Data.Abstract;
using LiveGameFeed.Models;
using AutoMapper;
namespace LiveGameFeed.Controllers
{
@@ -14,12 +15,15 @@ namespace LiveGameFeed.Controllers
public class FeedsController : ApiHubController<Broadcaster>
{
IFeedRepository _feedRepository;
IMatchRepository _matchRepository;
public FeedsController(
IConnectionManager signalRConnectionManager,
IFeedRepository feedRepository)
IFeedRepository feedRepository,
IMatchRepository matchRepository)
: base(signalRConnectionManager)
{
_feedRepository = feedRepository;
_matchRepository = matchRepository;
}
// GET api/values
@@ -31,29 +35,33 @@ namespace LiveGameFeed.Controllers
return _feeds;
}
// GET api/values/5
// GET api/feeds/5
[HttpGet("{id}")]
public Feed Get(int id)
{
return _feedRepository.GetSingle(id);
}
// POST api/values
// POST api/feeds
[HttpPost]
public void Post([FromBody]string value)
public async void Post([FromBody]FeedViewModel feed)
{
Match _match = _matchRepository.GetSingle(feed.MatchId);
Feed _matchFeed = new Feed()
{
Description = feed.Description,
CreatedAt = feed.CreatedAt,
MatchId = feed.MatchId
};
_match.Feeds.Add(_matchFeed);
_matchRepository.Commit();
FeedViewModel _feedVM = Mapper.Map<Feed, FeedViewModel>(_matchFeed);
await Clients.Group(feed.MatchId.ToString()).addFeed(_feedVM);
}
// 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

@@ -36,10 +36,12 @@ namespace LiveGameFeed.Core
{
Random r = new Random();
bool updateHost = r.Next(0, 2) == 1;
int points = r.Next(2,4);
if (updateHost)
match.HostScore += 2;
match.HostScore += points;
else
match.GuestScore += 2;
match.GuestScore += points;
MatchScore score = new MatchScore()
{
@@ -47,10 +49,24 @@ namespace LiveGameFeed.Core
GuestScore = match.GuestScore
};
// Update Score for all clients
using (var client = new HttpClient())
{
await client.PutAsJsonAsync<MatchScore>(_apiURI + "matches/" + match.Id, score);
}
// Update Feed for subscribed only clients
FeedViewModel _feed = new FeedViewModel()
{
MatchId = match.Id,
Description = points + " points for " + (updateHost == true ? match.Host : match.Guest + "!"),
CreatedAt = DateTime.Now
};
using (var client = new HttpClient())
{
await client.PostAsJsonAsync<FeedViewModel>(_apiURI + "feeds", _feed);
}
}
}
}