try
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
// src/Webshop.Api/Controllers/Customer/ProfileController.cs
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
using Webshop.Application.DTOs; // CustomerDto
|
||||
using Webshop.Application.DTOs.Auth; // ChangePasswordRequestDto
|
||||
using Webshop.Application.DTOs.Customers; // UpdateCustomerProfileDto
|
||||
using Webshop.Application.Services;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Customers;
|
||||
using Webshop.Application.Services.Customers.Interfaces;
|
||||
using Webshop.Application.Services.Customers;
|
||||
|
||||
namespace Webshop.Api.Controllers.Customers
|
||||
namespace Webshop.Api.Controllers.Customer
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/customer/profile")] // Eindeutige Route f<>r das Profil
|
||||
[Authorize(Roles = "Customer")] // Nur f<>r eingeloggte Kunden!
|
||||
[Route("api/v1/customer/[controller]")] // z.B. /api/v1/customer/profile
|
||||
[Authorize(Roles = "Customer")]
|
||||
public class ProfileController : ControllerBase
|
||||
{
|
||||
private readonly ICustomerService _customerService;
|
||||
@@ -19,33 +23,49 @@ namespace Webshop.Api.Controllers.Customers
|
||||
_customerService = customerService;
|
||||
}
|
||||
|
||||
// Hilfsmethode, um die ID des eingeloggten Benutzers aus dem Token zu holen
|
||||
private string GetUserId() => User.FindFirstValue(ClaimTypes.NameIdentifier)!;
|
||||
|
||||
[HttpGet("me")] // GET /api/v1/customer/profile/me
|
||||
[HttpGet("me")] // /api/v1/customer/profile/me
|
||||
public async Task<ActionResult<CustomerDto>> GetMyProfile()
|
||||
{
|
||||
var userId = GetUserId();
|
||||
var profile = await _customerService.GetMyProfileAsync(userId);
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
|
||||
|
||||
if (profile == null)
|
||||
{
|
||||
return NotFound("Kundenprofil nicht gefunden.");
|
||||
}
|
||||
return Ok(profile);
|
||||
var customerProfile = await _customerService.GetMyProfileAsync(userId);
|
||||
if (customerProfile == null) return NotFound(new { Message = "Kundenprofil nicht gefunden. Bitte erstellen Sie es." });
|
||||
|
||||
return Ok(customerProfile);
|
||||
}
|
||||
|
||||
[HttpPut("me")] // PUT /api/v1/customer/profile/me
|
||||
public async Task<IActionResult> UpdateMyProfile([FromBody] UpdateCustomerProfileDto profileDto)
|
||||
[HttpPost("change-password")] // /api/v1/customer/profile/change-password
|
||||
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequestDto request)
|
||||
{
|
||||
var userId = GetUserId();
|
||||
var success = await _customerService.UpdateMyProfileAsync(userId, profileDto);
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
return NotFound("Kundenprofil nicht gefunden.");
|
||||
}
|
||||
return NoContent(); // Standardantwort f<>r ein erfolgreiches Update
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
|
||||
|
||||
var (success, errorMessage) = await _customerService.ChangePasswordAsync(userId, request);
|
||||
|
||||
if (!success) return BadRequest(new { Message = errorMessage });
|
||||
|
||||
return Ok(new { Message = "Passwort erfolgreich ge<67>ndert. Bitte melden Sie sich mit dem neuen Passwort an." });
|
||||
}
|
||||
|
||||
// << NEUER/AKTUALISIERTER ENDPUNKT F<>R PROFIL-AKTUALISIERUNG >>
|
||||
[HttpPost("update-profile")] // /api/v1/customer/profile/update-profile
|
||||
public async Task<IActionResult> UpdateProfile([FromBody] UpdateCustomerProfileDto request)
|
||||
{
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (string.IsNullOrEmpty(userId)) return Unauthorized(new { Message = "Benutzer-ID nicht im Token gefunden." });
|
||||
|
||||
var (success, errorMessage) = await _customerService.UpdateMyProfileAsync(userId, request);
|
||||
|
||||
if (!success) return BadRequest(new { Message = errorMessage });
|
||||
|
||||
return Ok(new { Message = "Profil erfolgreich aktualisiert." });
|
||||
}
|
||||
|
||||
// << ENTFERNT: UpdateContactInfo Endpoint >>
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -14,7 +14,7 @@ using Webshop.Application.DTOs.Products;
|
||||
using Webshop.Application.DTOs.Payments;
|
||||
using Webshop.Application.DTOs.Orders;
|
||||
using Webshop.Application.DTOs.Discounts;
|
||||
using Webshop.Application.DTOs.Categorys; // Für Guid.NewGuid()
|
||||
using Webshop.Application.DTOs.Categorys;
|
||||
|
||||
namespace Webshop.Api.SwaggerFilters
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user