This commit is contained in:
Tizian.Breuch
2025-07-29 14:04:35 +02:00
parent 2d6eeb7a50
commit c1ee56c81c
19 changed files with 339 additions and 125 deletions

View File

@@ -18,6 +18,7 @@ using Webshop.Application.Services.Admin.Interfaces;
using Webshop.Application.Services.Public.Interfaces;
using Webshop.Application.Services.Customers.Interfaces;
using Webshop.Application.Services.Customers;
using Webshop.Domain.Identity;
var builder = WebApplication.CreateBuilder(args);
@@ -136,6 +137,7 @@ builder.Services.AddSwaggerGen(c =>
var app = builder.Build(); // <-- Hier wird die App gebaut
// OPTIONALE BL<42>CKE F<>R MIGRATION UND BENUTZERINITIALISIERUNG - DIESER CODE WIRD VOR APP.RUN() AUSGEF<45>HRT
// OPTIONALE BL<42>CKE F<>R MIGRATION UND BENUTZERINITIALISIERUNG - DIESER CODE WIRD VOR APP.RUN() AUSGEF<45>HRT
using (var scope = app.Services.CreateScope())
{
@@ -150,7 +152,7 @@ using (var scope = app.Services.CreateScope())
// Dieser Block erstellt Rollen und initiale Benutzer, falls sie noch nicht existieren.
// BITTE ENTFERNEN ODER KOMMENTIEREN SIE DIESEN BLOCK AUS, NACHDEM SIE IHRE ERSTEN BENUTZER ERSTELLT HABEN!
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>(); // << KORREKT: UserManager f<>r ApplicationUser >>
string[] roleNames = { "Admin", "Customer" };
@@ -162,23 +164,39 @@ using (var scope = app.Services.CreateScope())
}
}
// Erstelle einen initialen Admin-Benutzer
var adminUser = await userManager.FindByEmailAsync("admin@yourwebshop.com");
// Erstelle einen initialen Admin-Benutzer und sein Customer-Profil
var adminUser = await userManager.FindByEmailAsync("admin@yourwebshop.com"); // << ANPASSEN >>
if (adminUser == null)
{
// Erstellen Sie hier eine Instanz von ApplicationUser
adminUser = new ApplicationUser
adminUser = new ApplicationUser // << KORREKT: ERSTELLT ApplicationUser >>
{
UserName = "admin@yourwebshop.com",
Email = "admin@yourwebshop.com",
UserName = "admin@yourwebshop.com", // << ANPASSEN >>
Email = "admin@yourwebshop.com", // << ANPASSEN >>
EmailConfirmed = true,
CreatedDate = DateTimeOffset.UtcNow // Setzen Sie Ihr neues Feld!
CreatedDate = DateTimeOffset.UtcNow, // Custom Property auf ApplicationUser
LastActive = DateTimeOffset.UtcNow // Custom Property auf ApplicationUser
};
var createAdmin = await userManager.CreateAsync(adminUser, "SecureAdminPass123!");
var createAdmin = await userManager.CreateAsync(adminUser, "SecureAdminPass123!"); // << ANPASSEN >>
if (createAdmin.Succeeded)
{
await userManager.AddToRoleAsync(adminUser, "Admin");
Console.WriteLine("Admin user created.");
// Erstelle Customer-Profil f<>r Admin (falls Admins auch Kundenprofile haben sollen)
var adminCustomerProfile = await context.Customers.FirstOrDefaultAsync(c => c.AspNetUserId == adminUser.Id); // << KORREKT: SUCHT NACH AspNetUserId >>
if (adminCustomerProfile == null)
{
adminCustomerProfile = new Webshop.Domain.Entities.Customer
{
Id = Guid.NewGuid(),
AspNetUserId = adminUser.Id, // << KORREKT: VERKN<4B>PFUNG <20>BER AspNetUserId >>
FirstName = "Admin",
LastName = "User"
};
context.Customers.Add(adminCustomerProfile);
await context.SaveChangesAsync();
Console.WriteLine("Admin's Customer profile created.");
}
}
else
{
@@ -186,29 +204,46 @@ using (var scope = app.Services.CreateScope())
}
}
// Erstelle einen initialen Kunden-Benutzer
var customerUser = await userManager.FindByEmailAsync("customer@yourwebshop.com");
// Erstelle einen initialen Kunden-Benutzer und sein Customer-Profil (KOMBINIERT)
var customerUser = await userManager.FindByEmailAsync("customer@yourwebshop.com"); // << ANPASSEN >>
if (customerUser == null)
{
// Erstellen Sie auch hier eine Instanz von ApplicationUser
customerUser = new ApplicationUser
customerUser = new ApplicationUser // << KORREKT: ERSTELLT ApplicationUser >>
{
UserName = "customer@yourwebshop.com",
Email = "customer@yourwebshop.com",
UserName = "customer@yourwebshop.com", // << ANPASSEN >>
Email = "customer@yourwebshop.com", // << ANPASSEN >>
EmailConfirmed = true,
CreatedDate = DateTimeOffset.UtcNow // Setzen Sie Ihr neues Feld!
CreatedDate = DateTimeOffset.UtcNow, // Custom Property auf ApplicationUser
LastActive = DateTimeOffset.UtcNow // Custom Property auf ApplicationUser
};
var createCustomer = await userManager.CreateAsync(customerUser, "SecureCustomerPass123!");
var createCustomer = await userManager.CreateAsync(customerUser, "SecureCustomerPass123!"); // << ANPASSEN >>
if (createCustomer.Succeeded)
{
await userManager.AddToRoleAsync(customerUser, "Customer");
Console.WriteLine("Customer user created.");
// Kombinierter Teil: Customer-Profil erstellen, direkt nach IdentityUser-Erstellung
var customerProfile = await context.Customers.FirstOrDefaultAsync(c => c.AspNetUserId == customerUser.Id); // << KORREKT: SUCHT NACH AspNetUserId >>
if (customerProfile == null)
{
customerProfile = new Webshop.Domain.Entities.Customer
{
Id = Guid.NewGuid(),
AspNetUserId = customerUser.Id,
FirstName = "Test",
LastName = "Kunde"
};
context.Customers.Add(customerProfile);
await context.SaveChangesAsync();
Console.WriteLine("Customer profile created for new customer user.");
}
}
else
{
Console.WriteLine($"Error creating customer user: {string.Join(", ", createCustomer.Errors.Select(e => e.Description))}");
}
}
// --- ENDE DES TEMPOR<4F>REN SETUP-BLOCKS ---
}
catch (Exception ex)
{