cloudnet-compose/result/dotnet/Result/Data/MySqlResultData.cs

41 lines
1.3 KiB
C#
Raw Permalink Normal View History

2018-09-26 12:32:50 +02:00
using System.Linq;
using Dapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using Result.Models;
namespace Result.Data
{
public class MySqlResultData : IResultData
{
private readonly string _connectionString;
private readonly ILogger _logger;
public MySqlResultData(IConfiguration config, ILogger<MySqlResultData> logger)
{
_connectionString = config.GetConnectionString("ResultData");
_logger = logger;
}
public ResultsModel GetResults()
{
var model = new ResultsModel();
using (var connection = new MySqlConnection(_connectionString))
{
var results = connection.Query("SELECT vote, COUNT(id) AS count FROM votes GROUP BY vote ORDER BY vote");
if (results.Any(x => x.vote == "a"))
{
2018-09-26 12:58:32 +02:00
model.OptionA = (int) results.First(x => x.vote == "a").count;
2018-09-26 12:32:50 +02:00
}
if (results.Any(x => x.vote == "b"))
{
2018-09-26 12:58:32 +02:00
model.OptionB = (int) results.First(x => x.vote == "b").count;
2018-09-26 12:32:50 +02:00
}
model.VoteCount = model.OptionA + model.OptionB;
}
return model;
}
}
}