152 lines
6.3 KiB
C#
152 lines
6.3 KiB
C#
// 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;
|
|
using Webshop.Infrastructure.Data; // Für direkten Zugriff auf den DbContext (optional)
|
|
|
|
namespace Webshop.Application.Services.Customers
|
|
{
|
|
public class AddressService : IAddressService
|
|
{
|
|
private readonly IAddressRepository _addressRepository;
|
|
private readonly ICustomerRepository _customerRepository;
|
|
private readonly ApplicationDbContext _context; // Für komplexe Abfragen
|
|
|
|
public AddressService(IAddressRepository addressRepository, ICustomerRepository customerRepository, ApplicationDbContext context)
|
|
{
|
|
_addressRepository = addressRepository;
|
|
_customerRepository = customerRepository;
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IEnumerable<AddressDto>> GetMyAddressesAsync(string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return 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();
|
|
}
|
|
|
|
public async Task<AddressDto?> 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
|
|
{
|
|
CustomerId = customer.Id,
|
|
FirstName = customer.FirstName, // Vorerst vom Kundenprofil übernehmen
|
|
LastName = customer.LastName,
|
|
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);
|
|
}
|
|
|
|
public async Task<(bool Success, string? ErrorMessage)> UpdateAddressAsync(UpdateAddressDto addressDto, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return (false, "Kunde 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.");
|
|
}
|
|
|
|
address.FirstName = addressDto.FirstName;
|
|
address.LastName = addressDto.LastName;
|
|
address.Street = addressDto.Street;
|
|
address.HouseNumber = addressDto.HouseNumber;
|
|
address.City = addressDto.City;
|
|
address.PostalCode = addressDto.PostalCode;
|
|
address.Country = addressDto.Country;
|
|
address.Type = addressDto.Type;
|
|
|
|
await _addressRepository.UpdateAsync(address);
|
|
return (true, null);
|
|
}
|
|
|
|
public async Task<(bool Success, string? ErrorMessage)> DeleteAddressAsync(Guid addressId, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return (false, "Kunde 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.");
|
|
}
|
|
|
|
await _addressRepository.DeleteAsync(addressId);
|
|
return (true, null);
|
|
}
|
|
|
|
public async Task<(bool Success, string? ErrorMessage)> SetDefaultShippingAddressAsync(Guid addressId, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return (false, "Kunde 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.");
|
|
}
|
|
|
|
customer.DefaultShippingAddressId = addressId;
|
|
await _customerRepository.UpdateAsync(customer);
|
|
return (true, null);
|
|
}
|
|
|
|
public async Task<(bool Success, string? ErrorMessage)> SetDefaultBillingAddressAsync(Guid addressId, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return (false, "Kunde 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.");
|
|
}
|
|
|
|
customer.DefaultBillingAddressId = addressId;
|
|
await _customerRepository.UpdateAsync(customer);
|
|
return (true, null);
|
|
}
|
|
}
|
|
} |