From 93a1f2411bdc9f344cd91454afc59adacffb2783 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Tue, 29 Jul 2025 15:06:27 +0200 Subject: [PATCH] customer --- .../SwaggerFilters/AddExampleSchemaFilter.cs | 14 ++++++++++++++ .../DTOs/Customers/UpdateCustomerDto.cs | 3 +++ .../Services/Customers/CustomerService.cs | 17 ++++++++--------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Webshop.Api/SwaggerFilters/AddExampleSchemaFilter.cs b/Webshop.Api/SwaggerFilters/AddExampleSchemaFilter.cs index 80050e0..f4be996 100644 --- a/Webshop.Api/SwaggerFilters/AddExampleSchemaFilter.cs +++ b/Webshop.Api/SwaggerFilters/AddExampleSchemaFilter.cs @@ -193,6 +193,20 @@ namespace Webshop.Api.SwaggerFilters ["defaultBillingAddressId"] = new OpenApiNull() }; } + else if (type == typeof(UpdateCustomerDto)) // Dieses DTO verarbeitet jetzt alles + { + schema.Example = new OpenApiObject + { + ["firstName"] = new OpenApiString("Max"), + ["lastName"] = new OpenApiString("Mustermann"), + ["phoneNumber"] = new OpenApiString("+491701234567"), + ["email"] = new OpenApiString($"max.mustermann.neu.{uniqueId}@example.com"), + ["currentPassword"] = new OpenApiString("SecureCustomerPass123!"), + ["defaultShippingAddressId"] = new OpenApiNull(), + ["defaultBillingAddressId"] = new OpenApiNull() + }; + } + // --- Rabatte --- else if (type == typeof(DiscountDto)) { diff --git a/Webshop.Application/DTOs/Customers/UpdateCustomerDto.cs b/Webshop.Application/DTOs/Customers/UpdateCustomerDto.cs index a051dce..b8724d3 100644 --- a/Webshop.Application/DTOs/Customers/UpdateCustomerDto.cs +++ b/Webshop.Application/DTOs/Customers/UpdateCustomerDto.cs @@ -22,5 +22,8 @@ namespace Webshop.Application.DTOs.Customers // Optional, aber gute Sicherheitspraxis: Aktuelles Passwort zur Bestätigung sensibler Änderungen [Required(ErrorMessage = "Aktuelles Passwort ist zur Bestätigung erforderlich.")] public string CurrentPassword { get; set; } = string.Empty; + + public Guid? DefaultShippingAddressId { get; set; } + public Guid? DefaultBillingAddressId { get; set; } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Customers/CustomerService.cs b/Webshop.Application/Services/Customers/CustomerService.cs index 111fd08..a478a70 100644 --- a/Webshop.Application/Services/Customers/CustomerService.cs +++ b/Webshop.Application/Services/Customers/CustomerService.cs @@ -70,17 +70,20 @@ namespace Webshop.Application.Services.Customers var identityUser = await _userManager.FindByIdAsync(userId); if (identityUser == null) return (false, "Benutzerkonto nicht gefunden."); - // 1. Aktuelles Passwort prüfen (für alle sensiblen Änderungen) + // 1. Aktuelles Passwort prüfen if (!await _userManager.CheckPasswordAsync(identityUser, profileDto.CurrentPassword)) { return (false, "Falsches aktuelles Passwort zur Bestätigung."); } - // 2. Felder der Customer-Entität aktualisieren (FirstName, LastName) + // 2. Felder der Customer-Entität aktualisieren (FirstName, LastName, DEFAULT ADDRESS IDs) customer.FirstName = profileDto.FirstName; customer.LastName = profileDto.LastName; - // customer.PhoneNumber = profileDto.PhoneNumber; // Entfernt, da es jetzt in ApplicationUser zentralisiert ist - await _customerRepository.UpdateAsync(customer); // Speichert Änderungen im Customer-Profil + // << NEU: DEFAULT ADDRESS IDs aktualisieren >> + customer.DefaultShippingAddressId = profileDto.DefaultShippingAddressId; + customer.DefaultBillingAddressId = profileDto.DefaultBillingAddressId; + // -- ENDE NEU -- + await _customerRepository.UpdateAsync(customer); // 3. Felder des ApplicationUser (IdentityUser) aktualisieren (Email, PhoneNumber) bool identityUserChanged = false; @@ -90,9 +93,8 @@ namespace Webshop.Application.Services.Customers { identityUser.Email = profileDto.Email; identityUser.NormalizedEmail = _userManager.NormalizeEmail(profileDto.Email); - identityUser.UserName = profileDto.Email; // Oft wird der UserName auch mit der E-Mail synchronisiert + identityUser.UserName = profileDto.Email; identityUser.NormalizedUserName = _userManager.NormalizeName(profileDto.Email); - // Optional: user.EmailConfirmed = false; wenn Sie Bestätigungs-E-Mails senden identityUserChanged = true; } @@ -100,7 +102,6 @@ namespace Webshop.Application.Services.Customers if (!string.IsNullOrEmpty(profileDto.PhoneNumber) && identityUser.PhoneNumber != profileDto.PhoneNumber) { identityUser.PhoneNumber = profileDto.PhoneNumber; - // Optional: identityUser.PhoneNumberConfirmed = false; identityUserChanged = true; } @@ -116,7 +117,5 @@ namespace Webshop.Application.Services.Customers return (true, "Profil und Kontaktdaten erfolgreich aktualisiert."); } - - // << ENTFERNT: UpdateMyContactInfoAsync >> } } \ No newline at end of file