Files
ShopSolution-backend/Webshop.Application/Services/Customers/CustomerService.cs
Tizian.Breuch c1ee56c81c try
2025-07-29 14:04:35 +02:00

122 lines
5.6 KiB
C#

// src/Webshop.Application/Services/Customers/CustomerService.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using Webshop.Application.DTOs; // CustomerDto
using Webshop.Application.DTOs.Auth; // ChangePasswordRequestDto
using Webshop.Application.DTOs.Customers; // UpdateCustomerProfileDto
using Webshop.Domain.Entities; // Customer Entity
using Webshop.Domain.Interfaces; // ICustomerRepository
using Webshop.Domain.Identity; // Für ApplicationUser
using System.Linq; // Für Select
using System.Collections.Generic; // Für IEnumerable
namespace Webshop.Application.Services.Customers
{
public class CustomerService : ICustomerService
{
private readonly ICustomerRepository _customerRepository;
private readonly UserManager<ApplicationUser> _userManager;
public CustomerService(ICustomerRepository customerRepository, UserManager<ApplicationUser> userManager)
{
_customerRepository = customerRepository;
_userManager = userManager;
}
public async Task<CustomerDto?> GetMyProfileAsync(string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return null;
var identityUser = await _userManager.FindByIdAsync(userId);
if (identityUser == null) return null;
return new CustomerDto
{
Id = customer.Id,
UserId = customer.AspNetUserId,
FirstName = customer.FirstName,
LastName = customer.LastName,
Email = identityUser.Email ?? string.Empty, // E-Mail vom ApplicationUser
PhoneNumber = identityUser.PhoneNumber, // Telefonnummer vom ApplicationUser
DefaultShippingAddressId = customer.DefaultShippingAddressId,
DefaultBillingAddressId = customer.DefaultBillingAddressId
};
}
public async Task<(bool Success, string ErrorMessage)> ChangePasswordAsync(string userId, ChangePasswordRequestDto request)
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null) return (false, "Benutzer nicht gefunden.");
var result = await _userManager.ChangePasswordAsync(user, request.OldPassword, request.NewPassword);
if (!result.Succeeded)
{
var errors = string.Join(" ", result.Errors.Select(e => e.Description));
return (false, errors);
}
return (true, "Passwort erfolgreich geändert.");
}
// << NEUE IMPLEMENTIERUNG: UpdateMyProfileAsync verarbeitet alle Felder >>
public async Task<(bool Success, string ErrorMessage)> UpdateMyProfileAsync(string userId, UpdateCustomerProfileDto profileDto)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (false, "Kundenprofil nicht gefunden.");
var identityUser = await _userManager.FindByIdAsync(userId);
if (identityUser == null) return (false, "Benutzerkonto nicht gefunden.");
// 1. Aktuelles Passwort prüfen (für alle sensiblen Änderungen)
if (!await _userManager.CheckPasswordAsync(identityUser, profileDto.CurrentPassword))
{
return (false, "Falsches aktuelles Passwort zur Bestätigung.");
}
// 2. Felder der Customer-Entität aktualisieren (FirstName, LastName)
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
// 3. Felder des ApplicationUser (IdentityUser) aktualisieren (Email, PhoneNumber)
bool identityUserChanged = false;
// E-Mail aktualisieren (wenn anders und nicht leer)
if (!string.IsNullOrEmpty(profileDto.Email) && identityUser.Email != profileDto.Email)
{
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.NormalizedUserName = _userManager.NormalizeName(profileDto.Email);
// Optional: user.EmailConfirmed = false; wenn Sie Bestätigungs-E-Mails senden
identityUserChanged = true;
}
// Telefonnummer aktualisieren (wenn anders und nicht leer)
if (!string.IsNullOrEmpty(profileDto.PhoneNumber) && identityUser.PhoneNumber != profileDto.PhoneNumber)
{
identityUser.PhoneNumber = profileDto.PhoneNumber;
// Optional: identityUser.PhoneNumberConfirmed = false;
identityUserChanged = true;
}
if (identityUserChanged)
{
var updateResult = await _userManager.UpdateAsync(identityUser);
if (!updateResult.Succeeded)
{
var errors = string.Join(" ", updateResult.Errors.Select(e => e.Description));
return (false, $"Fehler beim Aktualisieren der Kontaktdaten: {errors}");
}
}
return (true, "Profil und Kontaktdaten erfolgreich aktualisiert.");
}
// << ENTFERNT: UpdateMyContactInfoAsync >>
}
}