Skip to content

Commit

Permalink
feat: use C# 9 records where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
alsami committed Nov 10, 2020
1 parent 2e4e98a commit c6af4d8
Show file tree
Hide file tree
Showing 21 changed files with 28 additions and 163 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Nullable>Enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/Covid19Api.Constants/Urls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ public static class Urls
{
// ReSharper disable once S1075
#pragma warning disable S1075
public const string CovidInfoWorldOmetersUrl = "https://worldometers.info/coronavirus";
public const string Covid19WorldometerUrl = "https://worldometers.info/coronavirus";

public const string RestCountriesApiUrl = "https://restcountries.eu/rest/v2/all";
#pragma warning restore
}
}
8 changes: 0 additions & 8 deletions src/Covid19Api.Domain/Enums/AggregateType.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Covid19Api.IoC/Modules/Covid19ApiDbContextModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override void Load(ContainerBuilder builder)
.AsSelf()
.InstancePerLifetimeScope();

builder.Register<Func<IMongoDatabase>>(componentContext =>
builder.Register<Func<IMongoDatabase>>(_ =>
{
return () =>
{
Expand Down
16 changes: 1 addition & 15 deletions src/Covid19Api.Services.Abstractions/Models/CountryMetaData.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
namespace Covid19Api.Services.Abstractions.Models
{
public class CountryMetaData
{
public CountryMetaData(string name, string alpha2Code, string[] altSpellings)
{
this.Name = name;
this.Alpha2Code = alpha2Code;
this.AltSpellings = altSpellings;
}

public string Name { get; }

public string Alpha2Code { get; }

public string[] AltSpellings { get; }
}
public sealed record CountryMetaData(string Name, string Alpha2Code, string[] AltSpellings);
}
9 changes: 3 additions & 6 deletions src/Covid19Api.Services/Loader/CountryMetaDataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Covid19Api.Constants;
using Covid19Api.Services.Abstractions.Loader;
using Covid19Api.Services.Abstractions.Models;
using Microsoft.Extensions.Logging;
Expand All @@ -10,10 +11,6 @@ namespace Covid19Api.Services.Loader
{
public class CountryMetaDataLoader : ICountryMetaDataLoader
{
#pragma warning disable S1075
private const string ApiUrl = "https://restcountries.eu/rest/v2/all";
#pragma warning restore

private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
Expand All @@ -32,7 +29,7 @@ public async Task<CountryMetaData[]> LoadCountryMetaDataAsync()
{
var client = this.httpClientFactory.CreateClient();

var response = await client.GetAsync(ApiUrl);
var response = await client.GetAsync(Urls.RestCountriesApiUrl);

if (response.IsSuccessStatusCode)
{
Expand All @@ -44,7 +41,7 @@ public async Task<CountryMetaData[]> LoadCountryMetaDataAsync()

var error = await response.Content.ReadAsStringAsync();
this.logger.LogError("Failed load country meta-data from {url}! Status-Code: {statusCode} Error:\n{error}",
ApiUrl, response.StatusCode, error);
Urls.RestCountriesApiUrl, response.StatusCode, error);

return Array.Empty<CountryMetaData>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Covid19Api.Services/Loader/HtmlDocumentLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<HtmlDocument> LoadAsync()
{
var client = this.httpClientFactory.CreateClient();

var response = await client.GetAsync(Urls.CovidInfoWorldOmetersUrl);
var response = await client.GetAsync(Urls.Covid19WorldometerUrl);

var document = new HtmlDocument();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
using System;
using System.Collections.Generic;
using MediatR;

namespace Covid19Api.UseCases.Abstractions.Commands
{
public class AggregateCountryStatisticsCommand : IRequest
{
public AggregateCountryStatisticsCommand(string[] countries, int month, int year)
{
this.Countries = countries ?? throw new ArgumentNullException(nameof(countries));
this.Month = month;
this.Year = year;
}

public string[] Countries { get; }

public int Month { get; }

public int Year { get; }
}
public sealed record AggregateCountryStatisticsCommand(IEnumerable<string> Countries, int Month, int Year) : IRequest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,5 @@

namespace Covid19Api.UseCases.Abstractions.Commands
{
public class AggregateGlobalStatisticsCommand : IRequest
{
public AggregateGlobalStatisticsCommand(int month, int year)
{
this.Month = month;
this.Year = year;
}

public int Month { get; }

public int Year { get; }
}
public sealed record AggregateGlobalStatisticsCommand(int Month, int Year) : IRequest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,5 @@

namespace Covid19Api.UseCases.Abstractions.Commands
{
public class RefreshCountriesStatisticsCommand : IRequest
{
public RefreshCountriesStatisticsCommand(DateTime fetchedAt)
{
this.FetchedAt = fetchedAt;
}

public DateTime FetchedAt { get; }
}
public sealed record RefreshCountriesStatisticsCommand(DateTime FetchedAt) : IRequest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,5 @@

namespace Covid19Api.UseCases.Abstractions.Commands
{
public class RefreshGlobalStatisticsCommand : IRequest
{
public RefreshGlobalStatisticsCommand(DateTime fetchedAt)
{
this.FetchedAt = fetchedAt;
}

public DateTime FetchedAt { get; }
}
public sealed record RefreshGlobalStatisticsCommand(DateTime FetchedAt) : IRequest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@

namespace Covid19Api.UseCases.Abstractions.Queries.CountryStatistics
{
public class LoadHistoricalCountriesStatisticsQuery : ICacheableRequest, IRequest<IEnumerable<CountryStatisticDto>>
public sealed record LoadHistoricalCountriesStatisticsQuery(DateTime MinFetchedAt) : ICacheableRequest, IRequest<IEnumerable<CountryStatisticDto>>
{
public LoadHistoricalCountriesStatisticsQuery(DateTime minFetchedAt)
{
this.MinFetchedAt = minFetchedAt;
}

public DateTime MinFetchedAt { get; }

public CacheConfiguration GetCacheConfiguration()
=> new CacheConfiguration(nameof(LoadHistoricalCountriesStatisticsQuery), TimeSpan.FromMinutes(30));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@

namespace Covid19Api.UseCases.Abstractions.Queries.CountryStatistics
{
public class LoadHistoricalCountryStatisticsForCountryQuery : ICacheableRequest,
public sealed record LoadHistoricalCountryStatisticsForCountryQuery(string Country) : ICacheableRequest,
IRequest<IEnumerable<CountryStatisticDto>>
{
public LoadHistoricalCountryStatisticsForCountryQuery(string country)
{
this.Country = country;
}

public string Country { get; }

public CacheConfiguration GetCacheConfiguration() =>
new CacheConfiguration($"{nameof(LoadHistoricalCountryStatisticsForCountryQuery)}_{this.Country}",
TimeSpan.FromMinutes(30));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Covid19Api.UseCases.Abstractions.Queries.CountryStatistics
{
public class LoadLatestCountriesStatisticsQuery : ICacheableRequest, IRequest<IEnumerable<CountryStatisticDto>>
public sealed record LoadLatestCountriesStatisticsQuery : ICacheableRequest, IRequest<IEnumerable<CountryStatisticDto>>
{
public CacheConfiguration GetCacheConfiguration() =>
new CacheConfiguration(nameof(LoadLatestCountriesStatisticsQuery), TimeSpan.FromMinutes(30));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

namespace Covid19Api.UseCases.Abstractions.Queries.CountryStatistics
{
public class LoadLatestStatisticsForCountryQuery : ICacheableRequest, IRequest<CountryStatisticDto>
public sealed record LoadLatestStatisticsForCountryQuery(string Country) : ICacheableRequest, IRequest<CountryStatisticDto>
{
public LoadLatestStatisticsForCountryQuery(string country)
{
this.Country = country;
}

public string Country { get; }

public CacheConfiguration GetCacheConfiguration() =>
new CacheConfiguration($"{nameof(LoadLatestStatisticsForCountryQuery)}_{this.Country}",
TimeSpan.FromMinutes(30));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@

namespace Covid19Api.UseCases.Abstractions.Queries.CountryStatisticsAggregates
{
public class LoadCountryStatisticsAggregate : IRequest<CountryStatisticAggregateDto?>
{
public LoadCountryStatisticsAggregate(string country, int month, int year)
{
this.Country = country;
this.Month = month;
this.Year = year;
}

public string Country { get; }
public int Month { get; }

public int Year { get; }
}
public sealed record LoadCountryStatisticsAggregate
(string Country, int Month, int Year) : IRequest<CountryStatisticAggregateDto?>;
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
using System;
using System.Collections.Generic;
using Covid19Api.Presentation.Response;
using MediatR;

namespace Covid19Api.UseCases.Abstractions.Queries.CountryStatisticsAggregates
{
public class
LoadCountryStatisticsAggregatesForCountryInYearQuery : IRequest<IEnumerable<CountryStatisticAggregateDto>>
{
public LoadCountryStatisticsAggregatesForCountryInYearQuery(string country, int year)
{
this.Country = country;
this.Year = year;

if (string.IsNullOrWhiteSpace(this.Country)) throw new ArgumentNullException(nameof(country));
}

public string Country { get; }
public int Year { get; }
}
public sealed record LoadCountryStatisticsAggregatesForCountryInYearQuery
(string Country, int Year) : IRequest<IEnumerable<CountryStatisticAggregateDto>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@

namespace Covid19Api.UseCases.Abstractions.Queries.GlobalStatistics
{
public class LoadHistoricalGlobalStatisticsQuery : ICacheableRequest, IRequest<IEnumerable<GlobalStatisticDto>>
public sealed record LoadHistoricalGlobalStatisticsQuery(DateTime MinFetchedAt) : ICacheableRequest, IRequest<IEnumerable<GlobalStatisticDto>>
{
public LoadHistoricalGlobalStatisticsQuery(DateTime minFetchedAt)
{
this.MinFetchedAt = minFetchedAt;
}

public DateTime MinFetchedAt { get; }

public CacheConfiguration GetCacheConfiguration() =>
new CacheConfiguration(nameof(LoadHistoricalGlobalStatisticsQuery), TimeSpan.FromMinutes(30));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Covid19Api.UseCases.Abstractions.Queries.GlobalStatistics
{
public class LoadLatestGlobalStatisticsQuery : ICacheableRequest, IRequest<GlobalStatisticDto>
public sealed record LoadLatestGlobalStatisticsQuery : ICacheableRequest, IRequest<GlobalStatisticDto>
{
public CacheConfiguration GetCacheConfiguration() =>
new CacheConfiguration(nameof(LoadLatestGlobalStatisticsQuery), TimeSpan.FromMinutes(30));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,5 @@

namespace Covid19Api.UseCases.Abstractions.Queries.GlobalStatisticsAggregates
{
public class LoadGlobalStatisticsAggregate : IRequest<GlobalStatisticAggregateDto?>
{
public LoadGlobalStatisticsAggregate(int month, int year)
{
this.Month = month;
this.Year = year;
}

public int Month { get; }

public int Year { get; }
}
public sealed record LoadGlobalStatisticsAggregate(int Month, int Year) : IRequest<GlobalStatisticAggregateDto?>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

namespace Covid19Api.UseCases.Abstractions.Queries.GlobalStatisticsAggregates
{
public class LoadGlobalStatisticsAggregatesForYearQuery : IRequest<IEnumerable<GlobalStatisticAggregateDto>>
{
public LoadGlobalStatisticsAggregatesForYearQuery(int year)
{
this.Year = year;
}

public int Year { get; }
}
public sealed record LoadGlobalStatisticsAggregatesForYearQuery
(int Year) : IRequest<IEnumerable<GlobalStatisticAggregateDto>>;
}

0 comments on commit c6af4d8

Please sign in to comment.