move update score to matches api

This commit is contained in:
chsakell
2016-10-04 13:02:22 +03:00
parent cc3b7fbbc5
commit 182d2d205f
4 changed files with 43 additions and 17 deletions

View File

@@ -42,23 +42,17 @@ namespace LiveGameFeed.Controllers
return _matchVM; return _matchVM;
} }
// POST api/values
[HttpPost("update", Name = "UpdateScore")]
public void UpdateScore([FromBody]MatchScore matchScore)
{
}
// PUT api/values/5 // PUT api/values/5
[HttpPut("{id}")] [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 MatchViewModel _matchVM = Mapper.Map<Match, MatchViewModel>(_match);
[HttpDelete("{id}")] await Clients.All.updateMatch(_matchVM);
public void Delete(int id)
{
} }
} }
} }

View File

@@ -1,5 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using LiveGameFeed.Data.Abstract;
using LiveGameFeed.Models; using LiveGameFeed.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using RecurrentTasks; using RecurrentTasks;
@@ -9,15 +12,46 @@ namespace LiveGameFeed.Core
public class FeedEngine : IRunnable public class FeedEngine : IRunnable
{ {
private ILogger logger; private ILogger logger;
IMatchRepository _matchRepository;
private string _apiURI = "http://localhost:5000/api/";
public FeedEngine(ILogger<FeedEngine> logger) public FeedEngine(IMatchRepository matchRepository,
ILogger<FeedEngine> logger)
{ {
this.logger = logger; this.logger = logger;
this._matchRepository = matchRepository;
} }
public void Run(TaskRunStatus taskRunStatus) public void Run(TaskRunStatus taskRunStatus)
{ {
var msg = string.Format("Run at: {0}", DateTimeOffset.Now); var msg = string.Format("Run at: {0}", DateTimeOffset.Now);
logger.LogDebug(msg); logger.LogDebug(msg);
UpdateScore();
}
private async void UpdateScore()
{
IEnumerable<Match> _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<MatchScore>(_apiURI + "matches/" + match.Id, score);
}
}
} }
} }
} }

View File

@@ -1,10 +1,7 @@
using System;
namespace LiveGameFeed.Models namespace LiveGameFeed.Models
{ {
public class MatchScore public class MatchScore
{ {
public int MatchId { get; set; }
public int HostScore { get; set; } public int HostScore { get; set; }
public int GuestScore {get; set;} public int GuestScore {get; set;}
} }

View File

@@ -5,6 +5,7 @@
"version": "1.0.0-*", "version": "1.0.0-*",
"type": "platform" "type": "platform"
}, },
"Microsoft.AspNet.WebApi.Client": "5.1.1",
"Microsoft.AspNetCore.Mvc": "1.0.1", "Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1", "Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",