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

@@ -8,6 +8,7 @@ using System.Linq;
using Webshop.Application.DTOs.Products;
using Webshop.Application.Services.Admin.Interfaces; // F<>r Select
namespace Webshop.Application.Services.Admin
{
public class AdminProductService : IAdminProductService // Sicherstellen, dass IAdminProductService implementiert wird

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Suppliers;
using Webshop.Application.Services.Admin.Interfaces;
using Webshop.Domain.Identity;
namespace Webshop.Application.Services.Admin
{

View File

@@ -8,6 +8,7 @@ using Webshop.Application.DTOs.Users;
using Webshop.Application.Services.Admin.Interfaces;
using Webshop.Domain.Entities;
using Webshop.Infrastructure.Data; // WICHTIG: Stellt sicher, dass ApplicationDbContext gefunden wird.
using Webshop.Domain.Identity;
namespace Webshop.Application.Services.Admin
{

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
// src/Webshop.Application/Services/Admin/Interfaces/IAdminUserService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Users;

View File

@@ -1,52 +1,122 @@
// src/Webshop.Application/Services/Customers/CustomerService.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Customers;
using Webshop.Application.Services.Customers.Interfaces;
using Webshop.Domain.Interfaces; // Wichtig für ICustomerRepository
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)
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;
}
if (customer == null) return null;
var identityUser = await _userManager.FindByIdAsync(userId);
if (identityUser == null) return null;
// Mappe die Entity auf das CustomerDto
return new CustomerDto
{
Id = customer.Id,
UserId = customer.AspNetUserId,
FirstName = customer.FirstName,
LastName = customer.LastName,
// Fügen Sie hier weitere Felder hinzu, die der Kunde sehen soll (Email, Phone etc.)
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> UpdateMyProfileAsync(string userId, UpdateCustomerProfileDto profileDto)
public async Task<(bool Success, string ErrorMessage)> ChangePasswordAsync(string userId, ChangePasswordRequestDto request)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null)
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)
{
return false; // Kunde nicht gefunden
var errors = string.Join(" ", result.Errors.Select(e => e.Description));
return (false, errors);
}
// Aktualisiere die Felder
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
await _customerRepository.UpdateAsync(customer);
return true;
// 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 >>
}
}

View File

@@ -1,11 +1,16 @@
// src/Webshop.Application/Services/Customers/ICustomerService.cs
using System.Threading.Tasks;
using Webshop.Application.DTOs.Customers; // Korrektes Using für DTOs
using Webshop.Application.DTOs; // CustomerDto
using Webshop.Application.DTOs.Auth; // ChangePasswordRequestDto
using Webshop.Application.DTOs.Customers; // UpdateCustomerProfileDto
namespace Webshop.Application.Services.Customers.Interfaces
namespace Webshop.Application.Services.Customers
{
public interface ICustomerService
{
Task<CustomerDto?> GetMyProfileAsync(string userId);
Task<bool> UpdateMyProfileAsync(string userId, UpdateCustomerProfileDto profileDto);
Task<(bool Success, string ErrorMessage)> ChangePasswordAsync(string userId, ChangePasswordRequestDto request);
Task<(bool Success, string ErrorMessage)> UpdateMyProfileAsync(string userId, UpdateCustomerProfileDto profileDto);
}
}