Files
WyvernInventory/WyvernInventory.API/Program.cs
T
rickcowan 75c381e645 Removed strongly typed inventory objects
Added EAV inventory types

Added EAV object handling philosophy

Added controllers

Added EF Core migration integration
2026-05-02 19:20:34 -05:00

87 lines
2.6 KiB
C#

using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using Serilog.Events;
using WyvernInventory.Core.Interfaces.Repos;
using WyvernInventory.Core.Interfaces.Services;
using WyvernInventory.Core.Models;
using WyvernInventory.Infrastructure.Data;
using WyvernInventory.Infrastructure.Repos;
using WyvernInventory.Infrastructure.Services;
using ILogger = Serilog.ILogger;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddScoped<IDataObjectService<InventoryType, InventoryTypeDto>, InventoryTypeService>();
builder.Services.AddScoped<IDbObjectRepo<InventoryType, InventoryTypeDto>, InventoryTypeRepo>();
builder.Services.AddScoped<IInventoryItemService, InventoryItemService>();
builder.Services.AddScoped<IInventoryItemRepo, InventoryItemRepo>();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddSwaggerGen();
builder.Host.UseSerilog((context, services, configuration) =>
configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
);
if (builder.Configuration["Database:ConnectionString"].IsNullOrEmpty())
{
ILogger logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger();
logger.Fatal("Database environment variables not found");
Environment.Exit(1);
}
else
{
builder.Services.AddDbContext<DBContext>(options =>
{
options
.UseSqlServer(builder.Configuration["Database:ConnectionString"]).EnableSensitiveDataLogging();
if (builder.Environment.IsDevelopment())
{
options
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
.LogTo(msg => { Debug.WriteLine(msg); }, [DbLoggerCategory.Database.Command.Name], LogLevel.Information);
}
});
}
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
DBContext db = scope.ServiceProvider.GetRequiredService<DBContext>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger>();
if (db.Database.GetPendingMigrations().Any())
{
await db.Database.MigrateAsync().ConfigureAwait(false);
logger.Write(LogEventLevel.Information, "Migrated database");
}
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();