using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Newsbot.Collector.Api.Domain.Consts; using Newsbot.Collector.Database; using Newsbot.Collector.Database.Repositories; using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Services; namespace Newsbot.Collector.Api.Startup; public class DatabaseStartup { public static void BuildDatabase(IServiceCollection services, IConfiguration config) { var dbconn = config.GetConnectionString("Database"); services.AddDbContext(o => o.UseNpgsql(dbconn ?? "")); // Add identity to our ef connection services.AddIdentity() .AddRoles() .AddEntityFrameworkStores(); } public static void InjectTableClasses(IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); } public static void InjectIdentityService(IServiceCollection services) { // Configure Identity services.AddScoped(); } public static async Task RunDatabaseMigrationsAsync(IServiceScope serviceScope) { var dbContext = serviceScope.ServiceProvider.GetRequiredService(); await dbContext.Database.MigrateAsync(); } public static async Task InjectIdentityRolesAsync(IServiceScope serviceScope) { var roleManager = serviceScope.ServiceProvider.GetRequiredService>(); if (!await roleManager.RoleExistsAsync(Authorization.AdministratorsRole)) { await roleManager.CreateAsync(new IdentityRole(Authorization.AdministratorsRole)); } if (!await roleManager.RoleExistsAsync(Authorization.UsersRole)) { await roleManager.CreateAsync(new IdentityRole(Authorization.UsersRole)); } } }