diff --git a/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs b/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs new file mode 100644 index 0000000..84da7f0 --- /dev/null +++ b/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json; + +namespace Geekbot.Bot.Commands.Utils.Corona +{ + public record CoronaApiCountryResponseDto + { + [JsonProperty("country")] + public string Country { get; init; } + + [JsonProperty("cases")] + public decimal Cases { get; init; } + + [JsonProperty("deaths")] + public decimal Deaths { get; init; } + + [JsonProperty("recovered")] + public decimal Recovered { get; init; } + } +} \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaStats.cs b/src/Bot/Commands/Utils/Corona/CoronaStats.cs index 3f9f3ea..5880030 100644 --- a/src/Bot/Commands/Utils/Corona/CoronaStats.cs +++ b/src/Bot/Commands/Utils/Corona/CoronaStats.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -11,7 +13,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona public class CoronaStats : ModuleBase { private readonly IErrorHandler _errorHandler; - + public CoronaStats(IErrorHandler errorHandler) { _errorHandler = errorHandler; @@ -19,24 +21,30 @@ namespace Geekbot.Bot.Commands.Utils.Corona [Command("corona", RunMode = RunMode.Async)] [Summary("Get the latest worldwide corona statistics")] - public async Task Summary() + public async Task Summary([Summary("CountryCode")] string countryCode = null) { try { - var summary = await HttpAbstractions.Get(new Uri("https://api.covid19api.com/world/total")); - var activeCases = summary.TotalConfirmed - (summary.TotalRecovered + summary.TotalDeaths); + var summary = await GetCoronaInfo(countryCode); + if (summary == null) + { + await Context.Channel.SendMessageAsync($"`{countryCode}` is not a valid country code"); + return; + } + + var activeCases = summary.Cases - (summary.Recovered + summary.Deaths); - string CalculatePercentage(decimal i) => (i / summary.TotalConfirmed).ToString("#0.##%"); + string CalculatePercentage(decimal i) => (i / summary.Cases).ToString("#0.##%"); var activePercent = CalculatePercentage(activeCases); - var recoveredPercentage = CalculatePercentage(summary.TotalRecovered); - var deathsPercentage = CalculatePercentage(summary.TotalDeaths); + var recoveredPercentage = CalculatePercentage(summary.Recovered); + var deathsPercentage = CalculatePercentage(summary.Deaths); var numberFormat = "#,#"; - var totalFormatted = summary.TotalConfirmed.ToString(numberFormat); + var totalFormatted = summary.Cases.ToString(numberFormat); var activeFormatted = activeCases.ToString(numberFormat); - var recoveredFormatted = summary.TotalRecovered.ToString(numberFormat); - var deathsFormatted = summary.TotalDeaths.ToString(numberFormat); - + var recoveredFormatted = summary.Recovered.ToString(numberFormat); + var deathsFormatted = summary.Deaths.ToString(numberFormat); + var eb = new EmbedBuilder { Author = new EmbedAuthorBuilder @@ -46,7 +54,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona }, Footer = new EmbedFooterBuilder { - Text = "Source: covid19api.com", + Text = "Source: covid19-api.org", }, Color = Color.Red }; @@ -54,7 +62,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona eb.AddInlineField("Active", $"{activeFormatted} ({activePercent})"); eb.AddInlineField("Recovered", $"{recoveredFormatted} ({recoveredPercentage})"); eb.AddInlineField("Deaths", $"{deathsFormatted} ({deathsPercentage})"); - + await Context.Channel.SendMessageAsync(String.Empty, false, eb.Build()); } catch (Exception e) @@ -62,5 +70,43 @@ namespace Geekbot.Bot.Commands.Utils.Corona await _errorHandler.HandleCommandException(e, Context); } } + + private async Task GetCoronaInfo(string countryCode = null) + { + var allCountries = await HttpAbstractions.Get>(new Uri("https://covid19-api.org/api/status")); + + if (string.IsNullOrEmpty(countryCode)) + { + return allCountries.Aggregate( + new CoronaTotalDto(), + (accumulate, source) => + { + accumulate.Cases += source.Cases; + accumulate.Deaths += source.Deaths; + accumulate.Recovered += source.Recovered; + return accumulate; + } + ); + } + + if (countryCode.Length != 2) + { + return null; + } + + var upcasedCountryCode = countryCode.ToUpper(); + var countryStats = allCountries.Find(x => x.Country == upcasedCountryCode); + if (countryStats == null) + { + return null; + } + + return new CoronaTotalDto() + { + Cases = countryStats.Cases, + Deaths = countryStats.Deaths, + Recovered = countryStats.Recovered, + }; + } } } \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs b/src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs deleted file mode 100644 index 3f6a820..0000000 --- a/src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Geekbot.Bot.Commands.Utils.Corona -{ - public class CoronaSummaryDto - { - public decimal TotalConfirmed { get; set; } - public decimal TotalDeaths { get; set; } - public decimal TotalRecovered { get; set; } - } -} \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs b/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs new file mode 100644 index 0000000..c135927 --- /dev/null +++ b/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.Bot.Commands.Utils.Corona +{ + public record CoronaTotalDto + { + public decimal Cases { get; set; } + public decimal Deaths { get; set; } + public decimal Recovered { get; set; } + } +} \ No newline at end of file