diff options
| author | transtrike <transtrike@gmail.com> | 2021-02-16 14:46:58 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-02-16 14:46:58 +0200 |
| commit | a2ca63701b71eed2ffa3fff6de16b9babe8bba08 (patch) | |
| tree | 5f90881df196987f5c1093a08c44a8651921f31b /src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs | |
| parent | ebf488fd51a2d1a83c124c380b9a1a98d62f42a2 (diff) | |
| download | DevHive-a2ca63701b71eed2ffa3fff6de16b9babe8bba08.tar DevHive-a2ca63701b71eed2ffa3fff6de16b9babe8bba08.tar.gz DevHive-a2ca63701b71eed2ffa3fff6de16b9babe8bba08.zip | |
Moved Default&Admin Roles insertion to DB Config
Diffstat (limited to 'src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs')
| -rw-r--r-- | src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs index 9f02dd7..84108ae 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Builder; using System; using Microsoft.AspNetCore.Authentication.JwtBearer; using DevHive.Data; +using System.Linq; +using System.Threading.Tasks; namespace DevHive.Web.Configurations.Extensions { @@ -58,13 +60,36 @@ namespace DevHive.Web.Configurations.Extensions }); } - public static void UseDatabaseConfiguration(this IApplicationBuilder app) + public static async Task UseDatabaseConfiguration(this IApplicationBuilder app) { app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); + + using var serviceScope = app.ApplicationServices.CreateScope(); + using var dbContext = serviceScope.ServiceProvider.GetRequiredService<DevHiveContext>(); + + dbContext.Database.Migrate(); + + var roleManager = (RoleManager<Role>)serviceScope.ServiceProvider.GetService(typeof(RoleManager<Role>)); + + if (!await dbContext.Roles.AnyAsync(x => x.Name == Role.DefaultRole)) + { + Role defaultRole = new() { Name = Role.DefaultRole }; + + await roleManager.CreateAsync(defaultRole); + } + + if (!await dbContext.Roles.AnyAsync(x => x.Name == Role.AdminRole)) + { + Role adminRole = new() { Name = Role.AdminRole }; + + await roleManager.CreateAsync(adminRole); + } + + await dbContext.SaveChangesAsync(); } } } |
