Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(WebAPI): added UseMigrationCreator middleware #135

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Persistence.MigrationConfigurations.Services;

namespace Persistence.MigrationConfigurations.Extensions;
public static class MigrationCreatorExtensions
{
public static Task UseMigrationCreator(this IApplicationBuilder app)
{
var service = app.ApplicationServices.GetRequiredService<IMigrationCreatorService>();
service.Initialze();
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Persistence.MigrationConfigurations.Services;
public interface IMigrationCreatorService
{
void Initialze();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Persistence.Contexts;

namespace Persistence.MigrationConfigurations.Services;
public class MigrationCreatorService : IMigrationCreatorService
{
private readonly BaseDbContext _context;

public MigrationCreatorService(BaseDbContext context)
{
_context = context;
}

public void Initialze()
{
if (_context.Database.CanConnect())
_context.Database.Migrate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Persistence.Contexts;
using Persistence.MigrationConfigurations.Services;
using Persistence.Repositories;

namespace Persistence;
Expand All @@ -18,6 +19,7 @@ public static IServiceCollection AddPersistenceServices(this IServiceCollection
services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IUserOperationClaimRepository, UserOperationClaimRepository>();
services.AddScoped<IMigrationCreatorService, MigrationCreatorService>();

return services;
}
Expand Down
3 changes: 3 additions & 0 deletions src/starterProject/WebAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Persistence;
using Persistence.MigrationConfigurations.Extensions;
using Swashbuckle.AspNetCore.SwaggerUI;
using WebAPI;

Expand Down Expand Up @@ -89,6 +90,8 @@
if (app.Environment.IsProduction())
app.ConfigureCustomExceptionMiddleware();

_ = app.UseMigrationCreator();

app.UseAuthentication();
app.UseAuthorization();

Expand Down