address update
This commit is contained in:
36
Webshop.Application/DTOs/Customers/UpdateAddressDto.cs
Normal file
36
Webshop.Application/DTOs/Customers/UpdateAddressDto.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
// src/Webshop.Application/DTOs/Customers/UpdateAddressDto.cs
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Webshop.Domain.Enums;
|
||||
|
||||
namespace Webshop.Application.DTOs.Customers
|
||||
{
|
||||
public class UpdateAddressDto
|
||||
{
|
||||
[Required]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[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; }
|
||||
|
||||
// Fügen Sie auch FirstName und LastName hinzu, falls diese änderbar sein sollen
|
||||
[Required]
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ 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
|
||||
{
|
||||
@@ -13,11 +14,13 @@ namespace Webshop.Application.Services.Customers
|
||||
{
|
||||
private readonly IAddressRepository _addressRepository;
|
||||
private readonly ICustomerRepository _customerRepository;
|
||||
private readonly ApplicationDbContext _context; // Für komplexe Abfragen
|
||||
|
||||
public AddressService(IAddressRepository addressRepository, ICustomerRepository customerRepository)
|
||||
public AddressService(IAddressRepository addressRepository, ICustomerRepository customerRepository, ApplicationDbContext context)
|
||||
{
|
||||
_addressRepository = addressRepository;
|
||||
_customerRepository = customerRepository;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<AddressDto>> GetMyAddressesAsync(string userId)
|
||||
@@ -29,6 +32,8 @@ namespace Webshop.Application.Services.Customers
|
||||
return addresses.Select(a => new AddressDto
|
||||
{
|
||||
Id = a.Id,
|
||||
FirstName = a.FirstName,
|
||||
LastName = a.LastName,
|
||||
Street = a.Street,
|
||||
HouseNumber = a.HouseNumber,
|
||||
City = a.City,
|
||||
@@ -56,8 +61,9 @@ namespace Webshop.Application.Services.Customers
|
||||
|
||||
var newAddress = new Address
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
CustomerId = customer.Id,
|
||||
FirstName = customer.FirstName, // Vorerst vom Kundenprofil übernehmen
|
||||
LastName = customer.LastName,
|
||||
Street = addressDto.Street,
|
||||
HouseNumber = addressDto.HouseNumber,
|
||||
City = addressDto.City,
|
||||
@@ -72,16 +78,74 @@ namespace Webshop.Application.Services.Customers
|
||||
return (createdDto, null);
|
||||
}
|
||||
|
||||
// Update- und Delete-Logik (vereinfacht)
|
||||
public async Task<(bool Success, string? ErrorMessage)> UpdateAddressAsync(Guid addressId, CreateAddressDto addressDto, string userId)
|
||||
public async Task<(bool Success, string? ErrorMessage)> UpdateAddressAsync(UpdateAddressDto addressDto, string userId)
|
||||
{
|
||||
// Implementierung ähnlich wie GetMyAddressByIdAsync + Speichern
|
||||
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)
|
||||
{
|
||||
// Implementierung ähnlich wie GetMyAddressByIdAsync + Löschen
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ namespace Webshop.Application.Services.Customers
|
||||
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)> UpdateAddressAsync(UpdateAddressDto addressDto, string userId);
|
||||
Task<(bool Success, string? ErrorMessage)> DeleteAddressAsync(Guid addressId, string userId);
|
||||
Task<(bool Success, string? ErrorMessage)> SetDefaultShippingAddressAsync(Guid addressId, string userId);
|
||||
Task<(bool Success, string? ErrorMessage)> SetDefaultBillingAddressAsync(Guid addressId, string userId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user