Simple Database + Endpoints

This commit is contained in:
2025-12-07 12:07:10 +01:00
parent 35eee78efc
commit 3125d657dd
22 changed files with 595 additions and 23 deletions

View File

@@ -1,12 +1,29 @@
using API.Database;
using API.Repository.AgeGroup;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Database
var postgreConnection = builder.Configuration.GetConnectionString("PostgresConnection");
if (!string.IsNullOrEmpty(postgreConnection))
{
// Nutze PostgresSQL
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(postgreConnection));
}
else
{
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite("Data Source=app.db"));
}
builder.Services.AddScoped<IAgeGroupService, AgeGroupService>();
// Add Database Services
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -16,6 +33,12 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
using(var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
}
app.UseHttpsRedirection();
app.UseAuthorization();