163 lines
7.2 KiB
C#
163 lines
7.2 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;
|
|
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<ServiceResult<IEnumerable<AddressDto>>> GetMyAddressesAsync(string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null)
|
|
{
|
|
return ServiceResult.Ok<IEnumerable<AddressDto>>(new List<AddressDto>());
|
|
}
|
|
|
|
var addresses = await _addressRepository.GetAllForCustomerAsync(customer.Id);
|
|
return ServiceResult.Ok(addresses.Select(MapToDto));
|
|
}
|
|
|
|
public async Task<ServiceResult<AddressDto>> GetMyAddressByIdAsync(Guid addressId, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null)
|
|
{
|
|
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
|
|
}
|
|
|
|
var address = await _addressRepository.GetByIdAsync(addressId);
|
|
if (address == null)
|
|
{
|
|
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, $"Adresse mit ID '{addressId}' nicht gefunden.");
|
|
}
|
|
|
|
if (address.CustomerId != customer.Id)
|
|
{
|
|
return ServiceResult.Fail<AddressDto>(ServiceResultType.Forbidden, "Zugriff auf diese Adresse verweigert.");
|
|
}
|
|
|
|
return ServiceResult.Ok(MapToDto(address));
|
|
}
|
|
|
|
public async Task<ServiceResult<AddressDto>> CreateAddressAsync(CreateAddressDto addressDto, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null)
|
|
{
|
|
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
|
|
}
|
|
|
|
var newAddress = new Address
|
|
{
|
|
CustomerId = customer.Id,
|
|
// << KORREKTUR: Namen vom Kundenprofil übernehmen, da sie im DTO nicht vorhanden sind. >>
|
|
FirstName = customer.FirstName,
|
|
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);
|
|
return ServiceResult.Ok(MapToDto(newAddress));
|
|
}
|
|
|
|
public async Task<ServiceResult> UpdateAddressAsync(UpdateAddressDto addressDto, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
|
|
|
|
var address = await _addressRepository.GetByIdAsync(addressDto.Id);
|
|
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;
|
|
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 ServiceResult.Ok();
|
|
}
|
|
|
|
public async Task<ServiceResult> DeleteAddressAsync(Guid addressId, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
|
|
|
|
var address = await _addressRepository.GetByIdAsync(addressId);
|
|
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 ServiceResult.Ok();
|
|
}
|
|
|
|
public async Task<ServiceResult> SetDefaultShippingAddressAsync(Guid addressId, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
|
|
|
|
var address = await _addressRepository.GetByIdAsync(addressId);
|
|
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 ServiceResult.Ok();
|
|
}
|
|
|
|
public async Task<ServiceResult> SetDefaultBillingAddressAsync(Guid addressId, string userId)
|
|
{
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
|
if (customer == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kundenprofil nicht gefunden.");
|
|
|
|
var address = await _addressRepository.GetByIdAsync(addressId);
|
|
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 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
|
|
};
|
|
}
|
|
}
|
|
} |