Simple Database + Endpoints
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user