Skip to content

Commit 97d0ae2

Browse files
committed
Add MySql result loader
1 parent 985af62 commit 97d0ae2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+71
-23852
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Result.Models;
2+
3+
namespace Result.Data
4+
{
5+
public interface IResultData
6+
{
7+
ResultsModel GetResults();
8+
}
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Linq;
2+
using Dapper;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.Logging;
5+
using MySql.Data.MySqlClient;
6+
using Result.Models;
7+
8+
namespace Result.Data
9+
{
10+
public class MySqlResultData : IResultData
11+
{
12+
private readonly string _connectionString;
13+
private readonly ILogger _logger;
14+
15+
public MySqlResultData(IConfiguration config, ILogger<MySqlResultData> logger)
16+
{
17+
_connectionString = config.GetConnectionString("ResultData");
18+
_logger = logger;
19+
}
20+
21+
public ResultsModel GetResults()
22+
{
23+
var model = new ResultsModel();
24+
using (var connection = new MySqlConnection(_connectionString))
25+
{
26+
var results = connection.Query("SELECT vote, COUNT(id) AS count FROM votes GROUP BY vote ORDER BY vote");
27+
if (results.Any(x => x.vote == "a"))
28+
{
29+
model.OptionA = results.First(x => x.vote == "a").count;
30+
}
31+
if (results.Any(x => x.vote == "b"))
32+
{
33+
model.OptionB = results.First(x => x.vote == "b").count;
34+
}
35+
model.VoteCount = model.OptionA + model.OptionB;
36+
}
37+
return model;
38+
}
39+
}
40+
}

result/dotnet/Result/Pages/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<div id="result">
4040
<span id="totalVotes">No votes yet</span>
4141
</div>
42-
<script src="~/lib/signalr/dist/browser/signalr.js"></script>
42+
<script src="~/lib/signalr/dist/browser/signalr.min.js"></script>
4343
<script src="~/js/results.js"></script>
4444
</body>
4545
</html>

result/dotnet/Result/Result.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Dapper" Version="1.50.5" />
89
<PackageReference Include="Microsoft.AspNetCore.App" />
10+
<PackageReference Include="MySql.Data" Version="8.0.12" />
911
</ItemGroup>
1012

1113
</Project>

result/dotnet/Result/Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
6+
using Result.Data;
67
using Result.Hubs;
78
using Result.Timers;
89

@@ -21,7 +22,9 @@ public void ConfigureServices(IServiceCollection services)
2122
{
2223
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
2324
services.AddSignalR();
24-
services.AddSingleton<PublishResultsTimer>();
25+
26+
services.AddTransient<IResultData, MySqlResultData>()
27+
.AddSingleton<PublishResultsTimer>();
2528
}
2629

2730
public void Configure(IApplicationBuilder app, IHostingEnvironment env)

result/dotnet/Result/Timers/PublishResultsTimer.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
using System;
2-
using System.Timers;
1+
using System.Timers;
32
using Microsoft.AspNetCore.SignalR;
43
using Microsoft.Extensions.Configuration;
4+
using Result.Data;
55
using Result.Hubs;
6-
using Result.Models;
76

87
namespace Result.Timers
98
{
109
public class PublishResultsTimer
1110
{
1211
private readonly IHubContext<ResultsHub> _hubContext;
12+
private readonly IResultData _resultData;
1313
private readonly Timer _timer;
14-
//TODO- temp
15-
private static Random _Random = new Random();
1614

17-
public PublishResultsTimer(IHubContext<ResultsHub> hubContext, IConfiguration configuration)
15+
public PublishResultsTimer(IHubContext<ResultsHub> hubContext, IResultData resultData, IConfiguration configuration)
1816
{
1917
_hubContext = hubContext;
18+
_resultData = resultData;
2019
var publishMilliseconds = configuration.GetValue<int>("ResultsTimer:PublishMilliseconds");
2120
_timer = new Timer(publishMilliseconds)
2221
{
@@ -35,12 +34,7 @@ public void Start()
3534

3635
private void PublishResults(object sender, ElapsedEventArgs e)
3736
{
38-
var model = new ResultsModel
39-
{
40-
OptionA = _Random.Next(0, 100),
41-
OptionB = _Random.Next(0, 100)
42-
};
43-
model.VoteCount = model.OptionA + model.OptionB;
37+
var model = _resultData.GetResults();
4438
_hubContext.Clients.All.SendAsync("UpdateResults", model);
4539
}
4640
}

result/dotnet/Result/appsettings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"OptionB": "Dogs"
55
},
66
"ResultsTimer": {
7-
"PublishMilliseconds": 1500
7+
"PublishMilliseconds": 2500
8+
},
9+
"ConnectionStrings": {
10+
"ResultData": "Server=mysql;Port=4000;Database=votes;User=root;SslMode=None"
811
},
912
"Logging": {
1013
"LogLevel": {
-31.3 KB
Binary file not shown.

result/dotnet/Result/wwwroot/js/results.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ connection.on("UpdateResults", function (results) {
1818

1919
var bg1 = document.getElementById('background-stats-1');
2020
var bg2 = document.getElementById('background-stats-2');
21-
bg1.style.width = percentages.a + "%";
22-
bg2.style.width = percentages.b + "%";
21+
bg1.style.width = (percentages.a-0.2) + "%";
22+
bg2.style.width = (percentages.b-0.2) + "%";
2323
});
2424

2525
connection.start().catch(function (err) {

result/dotnet/Result/wwwroot/lib/bootstrap/.bower.json

Lines changed: 0 additions & 45 deletions
This file was deleted.

result/dotnet/Result/wwwroot/lib/bootstrap/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)