From 182d2d205f88e34399982545a77297f12b4ec068 Mon Sep 17 00:00:00 2001 From: chsakell Date: Tue, 4 Oct 2016 13:02:22 +0300 Subject: [PATCH] move update score to matches api --- Controllers/MatchesController.cs | 20 +++++++----------- Core/FeedEngine.cs | 36 +++++++++++++++++++++++++++++++- Models/MatchScore.cs | 3 --- project.json | 1 + 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Controllers/MatchesController.cs b/Controllers/MatchesController.cs index a18d3f7..74ddf32 100644 --- a/Controllers/MatchesController.cs +++ b/Controllers/MatchesController.cs @@ -42,23 +42,17 @@ namespace LiveGameFeed.Controllers return _matchVM; } - // POST api/values - [HttpPost("update", Name = "UpdateScore")] - public void UpdateScore([FromBody]MatchScore matchScore) - { - - } - // PUT api/values/5 [HttpPut("{id}")] - public void Put(int id, [FromBody]string value) + public async void Put(int id, [FromBody]MatchScore score) { - } + Match _match = _matchRepository.GetSingle(id); + _match.HostScore = score.HostScore; + _match.GuestScore = score.GuestScore; + _matchRepository.Commit(); - // DELETE api/values/5 - [HttpDelete("{id}")] - public void Delete(int id) - { + MatchViewModel _matchVM = Mapper.Map(_match); + await Clients.All.updateMatch(_matchVM); } } } diff --git a/Core/FeedEngine.cs b/Core/FeedEngine.cs index 545f961..f3f30fe 100644 --- a/Core/FeedEngine.cs +++ b/Core/FeedEngine.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using LiveGameFeed.Data.Abstract; using LiveGameFeed.Models; using Microsoft.Extensions.Logging; using RecurrentTasks; @@ -9,15 +12,46 @@ namespace LiveGameFeed.Core public class FeedEngine : IRunnable { private ILogger logger; + IMatchRepository _matchRepository; + private string _apiURI = "http://localhost:5000/api/"; - public FeedEngine(ILogger logger) + public FeedEngine(IMatchRepository matchRepository, + ILogger logger) { this.logger = logger; + this._matchRepository = matchRepository; } public void Run(TaskRunStatus taskRunStatus) { var msg = string.Format("Run at: {0}", DateTimeOffset.Now); logger.LogDebug(msg); + UpdateScore(); + } + + private async void UpdateScore() + { + IEnumerable _matches = _matchRepository.GetAll(); + + foreach (var match in _matches) + { + Random r = new Random(); + bool updateHost = r.Next(0, 2) == 1; + if (updateHost) + match.HostScore += 2; + else + match.GuestScore += 2; + + MatchScore score = new MatchScore() + { + HostScore = match.HostScore, + GuestScore = match.GuestScore + }; + + using (var client = new HttpClient()) + { + await client.PutAsJsonAsync(_apiURI + "matches/" + match.Id, score); + } + } } } } \ No newline at end of file diff --git a/Models/MatchScore.cs b/Models/MatchScore.cs index affe27d..d698481 100644 --- a/Models/MatchScore.cs +++ b/Models/MatchScore.cs @@ -1,10 +1,7 @@ -using System; - namespace LiveGameFeed.Models { public class MatchScore { - public int MatchId { get; set; } public int HostScore { get; set; } public int GuestScore {get; set;} } diff --git a/project.json b/project.json index 723d6e5..d564208 100644 --- a/project.json +++ b/project.json @@ -5,6 +5,7 @@ "version": "1.0.0-*", "type": "platform" }, + "Microsoft.AspNet.WebApi.Client": "5.1.1", "Microsoft.AspNetCore.Mvc": "1.0.1", "Microsoft.AspNetCore.Routing": "1.0.1", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",