-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
741 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="10.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Covid19Api.Domain\Covid19Api.Domain.csproj" /> | ||
<ProjectReference Include="..\Covid19Api.Presentation\Covid19Api.Presentation.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
16 changes: 16 additions & 0 deletions
16
src/Covid19Api.AutoMapper/GlobalStatisticsAggregateProfile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using AutoMapper; | ||
using Covid19Api.Domain; | ||
using Covid19Api.Presentation.Response; | ||
|
||
namespace Covid19Api.AutoMapper | ||
{ | ||
public class GlobalStatisticsAggregateProfile : Profile | ||
{ | ||
public GlobalStatisticsAggregateProfile() | ||
{ | ||
this.CreateMap<GlobalStatisticsAggregate, GlobalStatisticsAggregateDto>() | ||
.ConstructUsing(source => new GlobalStatisticsAggregateDto(source.Id, source.Total, source.Recovered, | ||
source.Deaths, source.Month, source.Year)); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Covid19Api.Domain.Enums | ||
{ | ||
public enum AggregateType | ||
{ | ||
Month = 0, | ||
Year = 1, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Covid19Api.Domain | ||
{ | ||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Local | ||
// ReSharper disable UnusedAutoPropertyAccessor.Global | ||
public class GlobalStatisticsAggregate | ||
{ | ||
public GlobalStatisticsAggregate(int total, int recovered, int deaths, int month, int year) | ||
{ | ||
this.Total = total; | ||
this.Recovered = recovered; | ||
this.Deaths = deaths; | ||
this.Month = month; | ||
this.Year = year; | ||
this.Id = this.Generate(); | ||
} | ||
|
||
public Guid Id { get; private set; } | ||
public int Total { get; private set; } | ||
public int Recovered { get; private set; } | ||
public int Deaths { get; private set; } | ||
public int Month { get; private set; } | ||
public int Year { get; private set; } | ||
|
||
private Guid Generate() | ||
{ | ||
using var hasher = MD5.Create(); | ||
|
||
var unhashed = $"{nameof(GlobalStatisticsAggregate)}_{this.Month}.{this.Year}"; | ||
|
||
var hashed = hasher.ComputeHash(Encoding.UTF8.GetBytes(unhashed)); | ||
|
||
return new Guid(hashed); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Covid19Api.Mongo.Migrator/Abstractions/DatabaseMigration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Covid19Api.Mongo.Migrator.Abstractions | ||
{ | ||
public abstract class DatabaseMigration | ||
{ | ||
private readonly ILogger logger; | ||
|
||
protected DatabaseMigration(ILogger logger) | ||
{ | ||
this.logger = logger; | ||
} | ||
public abstract int Number { get; } | ||
|
||
protected abstract string Name { get; } | ||
|
||
public async Task ExecuteUpdateAsync() | ||
{ | ||
this.logger.LogInformation("Executing migration {number}-{migration}", this.Number, this.Name); | ||
await ExecuteAsync(); | ||
this.logger.LogInformation("Executed migration {number}-{migration}", this.Number, this.Name); | ||
} | ||
|
||
protected abstract Task ExecuteAsync(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Covid19Api.Mongo.Migrator/Configuration/GlobalAggregatesStartConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Covid19Api.Mongo.Migrator.Configuration | ||
{ | ||
public class GlobalAggregatesStartConfiguration | ||
{ | ||
public int Month { get; set; } = 9; | ||
|
||
public int Year { get; set; } = 2020; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Covid19Api.Mongo.Migrator/Covid19Api.Mongo.Migrator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Covid19Api.AutoMapper\Covid19Api.AutoMapper.csproj" /> | ||
<ProjectReference Include="..\Covid19Api.IoC\Covid19Api.IoC.csproj" /> | ||
<ProjectReference Include="..\Covid19Api.UseCases\Covid19Api.UseCases.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper.Contrib.Autofac.DependencyInjection" Version="5.1.0" /> | ||
<PackageReference Include="MediatR.Extensions.Autofac.DependencyInjection" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0-rc.2.20475.5" /> | ||
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" /> | ||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" /> | ||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="appsettings.json"> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
</Content> | ||
<Content Include="Properties\launchSettings.json"> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<CopyToPublishDirectory>Never</CopyToPublishDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
</Project> |
55 changes: 55 additions & 0 deletions
55
src/Covid19Api.Mongo.Migrator/Migrations/000000_GlobalAggregatesMigration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Covid19Api.Mongo.Migrator.Abstractions; | ||
using Covid19Api.Mongo.Migrator.Configuration; | ||
using Covid19Api.UseCases.Abstractions.Commands; | ||
using Covid19Api.UseCases.Abstractions.Queries; | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Covid19Api.Mongo.Migrator.Migrations | ||
{ | ||
public class GlobalAggregatesMigration : DatabaseMigration | ||
{ | ||
private readonly GlobalAggregatesStartConfiguration options; | ||
private readonly IMediator mediator; | ||
|
||
// ReSharper disable once SuggestBaseTypeForParameter | ||
public GlobalAggregatesMigration(ILogger<GlobalAggregatesMigration> logger, | ||
IOptions<GlobalAggregatesStartConfiguration> options, IMediator mediator) : base(logger) | ||
{ | ||
this.mediator = mediator; | ||
this.options = options?.Value ?? throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
public override int Number => 0; | ||
protected override string Name => nameof(GlobalAggregatesMigration); | ||
|
||
protected override async Task ExecuteAsync() | ||
{ | ||
var next = new DateTime(this.options.Year, this.options.Month, 1, 0, 0, 0, DateTimeKind.Utc); | ||
var end = DateTime.UtcNow.Date; | ||
|
||
while (true) | ||
{ | ||
if (next.Month > end.Month && next.Year >= end.Year) | ||
break; | ||
|
||
var query = new LoadGlobalStatisticsAggregate(next.Month, next.Year); | ||
var aggregate = await this.mediator.Send(query); | ||
|
||
if (aggregate is {}) | ||
{ | ||
next = next.AddMonths(1); | ||
continue; | ||
} | ||
|
||
var command = new AggregateGlobalStatisticsCommand(next.Month, next.Year); | ||
await this.mediator.Send(command); | ||
|
||
next = next.AddMonths(1); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Autofac; | ||
using Autofac.Extensions.DependencyInjection; | ||
using AutoMapper.Contrib.Autofac.DependencyInjection; | ||
using Covid19Api.AutoMapper; | ||
using Covid19Api.IoC.Extensions; | ||
using Covid19Api.Mongo.Migrator.Abstractions; | ||
using Covid19Api.Mongo.Migrator.Configuration; | ||
using Covid19Api.UseCases.Commands; | ||
using MediatR.Extensions.Autofac.DependencyInjection; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
|
||
namespace Covid19Api.Mongo.Migrator | ||
{ | ||
public static class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
using var host = CreateHost(args); | ||
|
||
await host.StartAsync(); | ||
|
||
var migrations = host.Services.GetServices<DatabaseMigration>(); | ||
|
||
foreach (var databaseMigration in migrations.OrderBy(migration => migration.Number)) | ||
await databaseMigration.ExecuteUpdateAsync(); | ||
|
||
await host.StopAsync(); | ||
} | ||
|
||
private static IHost CreateHost(string[] args) | ||
=> Host.CreateDefaultBuilder(args) | ||
.UseContentRoot(AppContext.BaseDirectory) | ||
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) | ||
.UseSerilog(ConfigureLogger) | ||
.ConfigureServices(ConfigureServices) | ||
.ConfigureContainer<ContainerBuilder>(ConfigureContainer) | ||
.Build(); | ||
|
||
private static void ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services) | ||
{ | ||
services.AddOptions(); | ||
services.Configure<GlobalAggregatesStartConfiguration>(options => | ||
hostBuilderContext.Configuration.GetSection(nameof(GlobalAggregatesStartConfiguration)).Bind(options)); | ||
} | ||
|
||
private static void ConfigureLogger(HostBuilderContext context, LoggerConfiguration loggerConfiguration) | ||
{ | ||
loggerConfiguration.ReadFrom.Configuration(context.Configuration); | ||
} | ||
|
||
private static void ConfigureContainer(HostBuilderContext context, ContainerBuilder builder) | ||
{ | ||
builder.RegisterAssemblyTypes(typeof(Program).Assembly) | ||
.AssignableTo(typeof(DatabaseMigration)) | ||
.As<DatabaseMigration>() | ||
.InstancePerLifetimeScope(); | ||
|
||
builder.RegisterRepositories(context.HostingEnvironment, context.Configuration) | ||
.RegisterServices() | ||
.RegisterMediatR(typeof(RefreshGlobalStatisticsCommandHandler).Assembly) | ||
.RegisterAutoMapper(typeof(CountryStatsProfile).Assembly); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Covid19Api.Mongo.Migrator/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"Covid19Api.Mongo.Migrator": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.