Finished adding basic category adding functionality

This commit is contained in:
Vincent Allen
2025-07-21 16:28:47 -04:00
parent 84108877d5
commit a822ad8559
184 changed files with 6557 additions and 28 deletions

View File

@@ -5,6 +5,10 @@ using MudBlazor.Services;
using OpenArchival.Blazor.Components;
using OpenArchival.Blazor.Components.Account;
using OpenArchival.Blazor.Data;
using OpenArchival.Database;
using Dapper;
using Npgsql;
using OpenArchival.Database.Category;
var builder = WebApplication.CreateBuilder(args);
@@ -15,10 +19,21 @@ builder.Services.AddMudServices();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
var postgresOptions = builder.Configuration
.GetSection(PostgresConnectionOptions.Key)
.Get<PostgresConnectionOptions>();
if (postgresOptions == null || string.IsNullOrEmpty(postgresOptions.ConnectionString)) throw new InvalidOperationException("Postgres connection options are not configured properly.");
builder.Services.AddNpgsqlDataSource(postgresOptions.ConnectionString);
// Add options
builder.Services.AddOptions<PostgresConnectionOptions>().Bind(builder.Configuration.GetSection(PostgresConnectionOptions.Key));
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
builder.Services.AddScoped<ICategoryProvider, CategoryProvider>();
builder.Services.AddAuthentication(options =>
{
@@ -39,6 +54,8 @@ builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.Requ
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
builder.Services.AddLogging();
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -65,4 +82,30 @@ app.MapRazorComponents<App>()
// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();
await InitializeDatabaseAsync(app.Services);
async Task InitializeDatabaseAsync(IServiceProvider services)
{
using var scope = services.CreateScope();
var serviceProvider = scope.ServiceProvider;
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Initializing database...");
var dataSource = serviceProvider.GetRequiredService<NpgsqlDataSource>();
await using var connection = await dataSource.OpenConnectionAsync();
await connection.ExecuteAsync(CategoryProvider.TableCreationQuery);
var categoryProvider = serviceProvider.GetRequiredService<ICategoryProvider>();
await categoryProvider.InsertCategoryAsync(new Category() {CategoryName="Pictures", FieldSeparator="-", FieldNames=new string[]{"one", "two"},FieldDescriptions=new string[] { "one", "two"} });
await categoryProvider.InsertCategoryAsync(new Category() {CategoryName="Yearbooks", FieldSeparator="-", FieldNames=new string[]{"one", "two"},FieldDescriptions=new string[] { "one", "two"}});
await categoryProvider.InsertCategoryAsync(new Category() {CategoryName="Books", FieldSeparator="-", FieldNames=new string[]{"one", "two"}, FieldDescriptions = new string[] { "one", "two" } });
await categoryProvider.InsertCategoryAsync(new Category() {CategoryName="Newspapers", FieldSeparator="-", FieldNames=new string[]{"one", "two"}, FieldDescriptions = new string[] { "one", "two" }});
await categoryProvider.InsertCategoryAsync(new Category() {CategoryName="Letters", FieldSeparator="-", FieldNames=new string[]{"one", "two"}, FieldDescriptions = new string[] { "one", "two" } });
logger.LogInformation("Database initialization complete.");
}
app.Run();