116 lines
4.4 KiB
C#
116 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Webshop.Application;
|
|
using Webshop.Application.DTOs.Customers;
|
|
using Webshop.Application.Services.Admin.Interfaces;
|
|
using Webshop.Domain.Entities;
|
|
using Webshop.Domain.Interfaces;
|
|
|
|
namespace Webshop.Application.Services.Admin
|
|
{
|
|
public class AdminAddressService : IAdminAddressService
|
|
{
|
|
private readonly IAddressRepository _addressRepository;
|
|
private readonly ICustomerRepository _customerRepository;
|
|
|
|
public AdminAddressService(IAddressRepository addressRepository, ICustomerRepository customerRepository)
|
|
{
|
|
_addressRepository = addressRepository;
|
|
_customerRepository = customerRepository;
|
|
}
|
|
|
|
public async Task<ServiceResult<IEnumerable<AddressDto>>> GetAllAddressesAsync()
|
|
{
|
|
// Hinweis: Um alle Adressen zu bekommen, brauchen wir eine neue Repository-Methode
|
|
var addresses = await _addressRepository.GetAllAsync();
|
|
return ServiceResult.Ok(addresses.Select(MapToDto));
|
|
}
|
|
|
|
public async Task<ServiceResult<AddressDto>> GetAddressByIdAsync(Guid addressId)
|
|
{
|
|
var address = await _addressRepository.GetByIdAsync(addressId);
|
|
if (address == null)
|
|
{
|
|
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, $"Adresse mit ID '{addressId}' nicht gefunden.");
|
|
}
|
|
return ServiceResult.Ok(MapToDto(address));
|
|
}
|
|
|
|
public async Task<ServiceResult<AddressDto>> CreateAddressForCustomerAsync(CreateAddressDto addressDto, Guid customerId)
|
|
{
|
|
var customer = await _customerRepository.GetByIdAsync(customerId);
|
|
if (customer == null)
|
|
{
|
|
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, $"Kunde mit ID '{customerId}' nicht gefunden.");
|
|
}
|
|
|
|
var newAddress = new Address
|
|
{
|
|
CustomerId = customer.Id,
|
|
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)
|
|
{
|
|
var address = await _addressRepository.GetByIdAsync(addressDto.Id);
|
|
if (address == null)
|
|
{
|
|
return ServiceResult.Fail(ServiceResultType.NotFound, "Adresse nicht gefunden.");
|
|
}
|
|
|
|
// Hier keine Berechtigungsprüfung, da der Admin alle Adressen bearbeiten darf.
|
|
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)
|
|
{
|
|
var address = await _addressRepository.GetByIdAsync(addressId);
|
|
if (address == null)
|
|
{
|
|
return ServiceResult.Fail(ServiceResultType.NotFound, "Adresse nicht gefunden.");
|
|
}
|
|
|
|
await _addressRepository.DeleteAsync(addressId);
|
|
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
|
|
};
|
|
}
|
|
}
|
|
} |