// src/Webshop.Application/Services/Customers/AddressService.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Webshop.Application.DTOs.Customers; using Webshop.Domain.Entities; using Webshop.Domain.Interfaces; namespace Webshop.Application.Services.Customers { public class AddressService : IAddressService { private readonly IAddressRepository _addressRepository; private readonly ICustomerRepository _customerRepository; public AddressService(IAddressRepository addressRepository, ICustomerRepository customerRepository) { _addressRepository = addressRepository; _customerRepository = customerRepository; } public async Task> GetMyAddressesAsync(string userId) { var customer = await _customerRepository.GetByUserIdAsync(userId); if (customer == null) return new List(); var addresses = await _addressRepository.GetAllForCustomerAsync(customer.Id); return addresses.Select(a => new AddressDto { Id = a.Id, Street = a.Street, HouseNumber = a.HouseNumber, City = a.City, PostalCode = a.PostalCode, Country = a.Country, Type = a.Type }).ToList(); } public async Task GetMyAddressByIdAsync(Guid addressId, string userId) { var customer = await _customerRepository.GetByUserIdAsync(userId); if (customer == null) return null; var address = await _addressRepository.GetByIdAsync(addressId); if (address == null || address.CustomerId != customer.Id) return null; return new AddressDto { /* ... Mapping ... */ }; } public async Task<(AddressDto? CreatedAddress, string? ErrorMessage)> CreateAddressAsync(CreateAddressDto addressDto, string userId) { var customer = await _customerRepository.GetByUserIdAsync(userId); if (customer == null) return (null, "Kunde nicht gefunden."); var newAddress = new Address { Id = Guid.NewGuid(), CustomerId = customer.Id, Street = addressDto.Street, HouseNumber = addressDto.HouseNumber, City = addressDto.City, PostalCode = addressDto.PostalCode, Country = addressDto.Country, Type = addressDto.Type }; await _addressRepository.AddAsync(newAddress); var createdDto = new AddressDto { Id = newAddress.Id, /* ... Mapping ... */ }; return (createdDto, null); } // Update- und Delete-Logik (vereinfacht) public async Task<(bool Success, string? ErrorMessage)> UpdateAddressAsync(Guid addressId, CreateAddressDto addressDto, string userId) { // Implementierung ähnlich wie GetMyAddressByIdAsync + Speichern return (true, null); } public async Task<(bool Success, string? ErrorMessage)> DeleteAddressAsync(Guid addressId, string userId) { // Implementierung ähnlich wie GetMyAddressByIdAsync + Löschen return (true, null); } } }