Adress und shipping

This commit is contained in:
Tizian.Breuch
2025-08-01 09:09:49 +02:00
parent 7fc0b32c17
commit 14daa831cb
14 changed files with 460 additions and 47 deletions

View File

@@ -0,0 +1,90 @@
// src/Webshop.Application/Services/Admin/AdminShippingMethodService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Shipping;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Admin
{
public class AdminShippingMethodService : IAdminShippingMethodService
{
private readonly IShippingMethodRepository _shippingMethodRepository;
public AdminShippingMethodService(IShippingMethodRepository shippingMethodRepository)
{
_shippingMethodRepository = shippingMethodRepository;
}
public async Task<IEnumerable<ShippingMethodDto>> GetAllAsync()
{
var methods = await _shippingMethodRepository.GetAllAsync();
return methods.Select(sm => new ShippingMethodDto
{
Id = sm.Id,
Name = sm.Name,
Description = sm.Description,
Cost = sm.BaseCost,
IsActive = sm.IsActive,
MinDeliveryDays = 0, // Annahme, diese Felder existieren in Ihrer Entität
MaxDeliveryDays = 0 // Annahme, diese Felder existieren in Ihrer Entität
}).ToList();
}
public async Task<ShippingMethodDto?> GetByIdAsync(Guid id)
{
var sm = await _shippingMethodRepository.GetByIdAsync(id);
if (sm == null) return null;
return new ShippingMethodDto
{
Id = sm.Id,
Name = sm.Name,
Description = sm.Description,
Cost = sm.BaseCost,
IsActive = sm.IsActive
};
}
public async Task<ShippingMethodDto> CreateAsync(ShippingMethodDto shippingMethodDto)
{
var newMethod = new ShippingMethod
{
Id = Guid.NewGuid(),
Name = shippingMethodDto.Name,
Description = shippingMethodDto.Description,
BaseCost = shippingMethodDto.Cost,
IsActive = shippingMethodDto.IsActive
};
await _shippingMethodRepository.AddAsync(newMethod);
shippingMethodDto.Id = newMethod.Id;
return shippingMethodDto;
}
public async Task<bool> UpdateAsync(ShippingMethodDto shippingMethodDto)
{
var existingMethod = await _shippingMethodRepository.GetByIdAsync(shippingMethodDto.Id);
if (existingMethod == null) return false;
existingMethod.Name = shippingMethodDto.Name;
existingMethod.Description = shippingMethodDto.Description;
existingMethod.BaseCost = shippingMethodDto.Cost;
existingMethod.IsActive = shippingMethodDto.IsActive;
await _shippingMethodRepository.UpdateAsync(existingMethod);
return true;
}
public async Task<bool> DeleteAsync(Guid id)
{
var method = await _shippingMethodRepository.GetByIdAsync(id);
if (method == null) return false;
await _shippingMethodRepository.DeleteAsync(id);
return true;
}
}
}

View File

@@ -0,0 +1,17 @@
// src/Webshop.Application/Services/Admin/IAdminShippingMethodService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Shipping; // Für ShippingMethodDto
namespace Webshop.Application.Services.Admin
{
public interface IAdminShippingMethodService
{
Task<IEnumerable<ShippingMethodDto>> GetAllAsync();
Task<ShippingMethodDto?> GetByIdAsync(Guid id);
Task<ShippingMethodDto> CreateAsync(ShippingMethodDto shippingMethodDto);
Task<bool> UpdateAsync(ShippingMethodDto shippingMethodDto);
Task<bool> DeleteAsync(Guid id);
}
}

View File

@@ -0,0 +1,88 @@
// 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<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,
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
{
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);
}
}
}

View File

@@ -0,0 +1,21 @@
// src/Webshop.Application/DTOs/Customers/CreateAddressDto.cs
using System.ComponentModel.DataAnnotations;
using Webshop.Domain.Enums;
namespace Webshop.Application.DTOs.Customers
{
public class CreateAddressDto
{
[Required]
public string Street { get; set; } = string.Empty;
[Required]
public string HouseNumber { get; set; } = string.Empty;
[Required]
public string City { get; set; } = string.Empty;
[Required]
public string PostalCode { get; set; } = string.Empty;
[Required]
public string Country { get; set; } = string.Empty;
public AddressType Type { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
// src/Webshop.Application/Services/Customers/IAddressService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
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(Guid addressId, CreateAddressDto addressDto, string userId);
Task<(bool Success, string? ErrorMessage)> DeleteAddressAsync(Guid addressId, string userId);
}
}