InitialCode Commit

This commit is contained in:
Mueller Wayan
2025-09-07 19:06:43 +02:00
parent 887948bb02
commit d29e25992b
183 changed files with 82537 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MTG_CollectionVerwaltung.API_Connector;
using MTG_CollectionVerwaltung.Data;
using MTG_CollectionVerwaltung.Data.MTGCollection;
SQLitePCL.Batteries_V2.Init();
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<MTGContext>(options =>
options.UseSqlite("Data Source=mtgcollection.db"));
/*var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));*/
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite("Data Source=mtgcollection.db"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
builder.Services.AddRazorPages();
builder.Services.AddHostedService<ScryfallDBUpdater>();
var app = builder.Build();
//Generate and actualize DBs at Powerup
using (var scope = app.Services.CreateScope())
{
var mtgContext = scope.ServiceProvider.GetRequiredService<MTGContext>();
mtgContext.Database.Migrate(); // erstellt DB + Tabellen falls nötig
var identityContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
identityContext.Database.Migrate();
}
//Admin User ertsellen wenn nicht vorhanden
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
var adminEmail = builder.Configuration["AdminUser:Email"];
var adminPassword = "ChangeMe!123";
// Rolle "Admin" anlegen, falls nicht vorhanden
if (!await roleManager.RoleExistsAsync("Admin"))
{
await roleManager.CreateAsync(new IdentityRole("Admin"));
}
var adminUser = await userManager.FindByEmailAsync(adminEmail);
if (adminUser == null)
{
adminUser = new ApplicationUser
{
UserName = adminEmail,
Email = adminEmail,
EmailConfirmed = true,
MustChangePassword = true
};
var result = await userManager.CreateAsync(adminUser, adminPassword);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(adminUser, "Admin");
}
}
else
{
if (!await userManager.IsInRoleAsync(adminUser, "Admin"))
{
await userManager.AddToRoleAsync(adminUser, "Admin");
}
}
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();