This commit is contained in:
Tizian.Breuch
2025-09-25 16:11:46 +02:00
parent 36cc2d97a0
commit c80b5ccc22
3 changed files with 163 additions and 96 deletions

View File

@@ -3,10 +3,10 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Customers;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data; // Für direkten Zugriff auf den DbContext (optional)
namespace Webshop.Application.Services.Customers
{
@@ -14,56 +14,61 @@ namespace Webshop.Application.Services.Customers
{
private readonly IAddressRepository _addressRepository;
private readonly ICustomerRepository _customerRepository;
private readonly ApplicationDbContext _context; // Für komplexe Abfragen
public AddressService(IAddressRepository addressRepository, ICustomerRepository customerRepository, ApplicationDbContext context)
public AddressService(IAddressRepository addressRepository, ICustomerRepository customerRepository)
{
_addressRepository = addressRepository;
_customerRepository = customerRepository;
_context = context;
}
public async Task<IEnumerable<AddressDto>> GetMyAddressesAsync(string userId)
public async Task<ServiceResult<IEnumerable<AddressDto>>> GetMyAddressesAsync(string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return new List<AddressDto>();
if (customer == null)
{
// If the customer profile doesn't exist for a valid user, return an empty list.
return ServiceResult.Ok<IEnumerable<AddressDto>>(new List<AddressDto>());
}
var addresses = await _addressRepository.GetAllForCustomerAsync(customer.Id);
return addresses.Select(a => new AddressDto
{
Id = a.Id,
FirstName = a.FirstName,
LastName = a.LastName,
Street = a.Street,
HouseNumber = a.HouseNumber,
City = a.City,
PostalCode = a.PostalCode,
Country = a.Country,
Type = a.Type
}).ToList();
return ServiceResult.Ok(addresses.Select(MapToDto));
}
public async Task<AddressDto?> GetMyAddressByIdAsync(Guid addressId, string userId)
public async Task<ServiceResult<AddressDto>> GetMyAddressByIdAsync(Guid addressId, string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return null;
if (customer == null)
{
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
}
var address = await _addressRepository.GetByIdAsync(addressId);
if (address == null || address.CustomerId != customer.Id) return null;
if (address == null)
{
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, $"Adresse mit ID '{addressId}' nicht gefunden.");
}
return new AddressDto { /* ... Mapping ... */ };
if (address.CustomerId != customer.Id)
{
return ServiceResult.Fail<AddressDto>(ServiceResultType.Forbidden, "Zugriff auf diese Adresse verweigert.");
}
return ServiceResult.Ok(MapToDto(address));
}
public async Task<(AddressDto? CreatedAddress, string? ErrorMessage)> CreateAddressAsync(CreateAddressDto addressDto, string userId)
public async Task<ServiceResult<AddressDto>> CreateAddressAsync(CreateAddressDto addressDto, string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (null, "Kunde nicht gefunden.");
if (customer == null)
{
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
}
var newAddress = new Address
{
CustomerId = customer.Id,
FirstName = customer.FirstName, // Vorerst vom Kundenprofil übernehmen
LastName = customer.LastName,
FirstName = addressDto.FirstName ?? customer.FirstName,
LastName = addressDto.LastName ?? customer.LastName,
Street = addressDto.Street,
HouseNumber = addressDto.HouseNumber,
City = addressDto.City,
@@ -73,21 +78,17 @@ namespace Webshop.Application.Services.Customers
};
await _addressRepository.AddAsync(newAddress);
var createdDto = new AddressDto { Id = newAddress.Id, /* ... Mapping ... */ };
return (createdDto, null);
return ServiceResult.Ok(MapToDto(newAddress));
}
public async Task<(bool Success, string? ErrorMessage)> UpdateAddressAsync(UpdateAddressDto addressDto, string userId)
public async Task<ServiceResult> UpdateAddressAsync(UpdateAddressDto addressDto, string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (false, "Kunde nicht gefunden.");
if (customer == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
var address = await _addressRepository.GetByIdAsync(addressDto.Id);
if (address == null || address.CustomerId != customer.Id)
{
return (false, "Adresse nicht gefunden oder gehört nicht zum aktuellen Benutzer.");
}
if (address == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Adresse nicht gefunden.");
if (address.CustomerId != customer.Id) return ServiceResult.Fail(ServiceResultType.Forbidden, "Zugriff auf diese Adresse verweigert.");
address.FirstName = addressDto.FirstName;
address.LastName = addressDto.LastName;
@@ -99,54 +100,64 @@ namespace Webshop.Application.Services.Customers
address.Type = addressDto.Type;
await _addressRepository.UpdateAsync(address);
return (true, null);
return ServiceResult.Ok();
}
public async Task<(bool Success, string? ErrorMessage)> DeleteAddressAsync(Guid addressId, string userId)
public async Task<ServiceResult> DeleteAddressAsync(Guid addressId, string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (false, "Kunde nicht gefunden.");
if (customer == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
var address = await _addressRepository.GetByIdAsync(addressId);
if (address == null || address.CustomerId != customer.Id)
{
return (false, "Adresse nicht gefunden oder gehört nicht zum aktuellen Benutzer.");
}
if (address == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Adresse nicht gefunden.");
if (address.CustomerId != customer.Id) return ServiceResult.Fail(ServiceResultType.Forbidden, "Zugriff auf diese Adresse verweigert.");
await _addressRepository.DeleteAsync(addressId);
return (true, null);
return ServiceResult.Ok();
}
public async Task<(bool Success, string? ErrorMessage)> SetDefaultShippingAddressAsync(Guid addressId, string userId)
public async Task<ServiceResult> SetDefaultShippingAddressAsync(Guid addressId, string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (false, "Kunde nicht gefunden.");
if (customer == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
var address = await _addressRepository.GetByIdAsync(addressId);
if (address == null || address.CustomerId != customer.Id)
{
return (false, "Adresse nicht gefunden oder gehört nicht zum aktuellen Benutzer.");
}
if (address == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Adresse nicht gefunden.");
if (address.CustomerId != customer.Id) return ServiceResult.Fail(ServiceResultType.Forbidden, "Zugriff auf diese Adresse verweigert.");
customer.DefaultShippingAddressId = addressId;
await _customerRepository.UpdateAsync(customer);
return (true, null);
return ServiceResult.Ok();
}
public async Task<(bool Success, string? ErrorMessage)> SetDefaultBillingAddressAsync(Guid addressId, string userId)
public async Task<ServiceResult> SetDefaultBillingAddressAsync(Guid addressId, string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (false, "Kunde nicht gefunden.");
if (customer == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
var address = await _addressRepository.GetByIdAsync(addressId);
if (address == null || address.CustomerId != customer.Id)
{
return (false, "Adresse nicht gefunden oder gehört nicht zum aktuellen Benutzer.");
}
if (address == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Adresse nicht gefunden.");
if (address.CustomerId != customer.Id) return ServiceResult.Fail(ServiceResultType.Forbidden, "Zugriff auf diese Adresse verweigert.");
customer.DefaultBillingAddressId = addressId;
await _customerRepository.UpdateAsync(customer);
return (true, null);
return ServiceResult.Ok();
}
private AddressDto MapToDto(Address address)
{
return new AddressDto
{
Id = address.Id,
FirstName = address.FirstName,
LastName = address.LastName,
Street = address.Street,
HouseNumber = address.HouseNumber,
City = address.City,
PostalCode = address.PostalCode,
Country = address.Country,
Type = address.Type
};
}
}
}

View File

@@ -2,18 +2,19 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Customers;
namespace Webshop.Application.Services.Customers
{
public interface IAddressService
{
Task<IEnumerable<AddressDto>> GetMyAddressesAsync(string userId);
Task<AddressDto?> GetMyAddressByIdAsync(Guid addressId, string userId);
Task<(AddressDto? CreatedAddress, string? ErrorMessage)> CreateAddressAsync(CreateAddressDto addressDto, string userId);
Task<(bool Success, string? ErrorMessage)> UpdateAddressAsync(UpdateAddressDto addressDto, string userId);
Task<(bool Success, string? ErrorMessage)> DeleteAddressAsync(Guid addressId, string userId);
Task<(bool Success, string? ErrorMessage)> SetDefaultShippingAddressAsync(Guid addressId, string userId);
Task<(bool Success, string? ErrorMessage)> SetDefaultBillingAddressAsync(Guid addressId, string userId);
Task<ServiceResult<IEnumerable<AddressDto>>> GetMyAddressesAsync(string userId);
Task<ServiceResult<AddressDto>> GetMyAddressByIdAsync(Guid addressId, string userId);
Task<ServiceResult<AddressDto>> CreateAddressAsync(CreateAddressDto addressDto, string userId);
Task<ServiceResult> UpdateAddressAsync(UpdateAddressDto addressDto, string userId);
Task<ServiceResult> DeleteAddressAsync(Guid addressId, string userId);
Task<ServiceResult> SetDefaultShippingAddressAsync(Guid addressId, string userId);
Task<ServiceResult> SetDefaultBillingAddressAsync(Guid addressId, string userId);
}
}