zeroframework/Services/DeviceCenter/ZeroFramework.DeviceCenter.Domain/Aggregates/BuyerAggregate/Buyer.cs

34 lines
1.2 KiB
C#
Raw Normal View History

2023-12-05 09:22:48 +00:00
using ZeroFramework.DeviceCenter.Domain.Entities;
using ZeroFramework.DeviceCenter.Domain.Events.Buyers;
namespace ZeroFramework.DeviceCenter.Domain.Aggregates.BuyerAggregate
{
public class Buyer(Guid userId) : BaseAggregateRoot<Guid>
{
public Guid UserId { get; private set; } = userId;
private readonly List<PaymentMethod> _paymentMethods = [];
public IEnumerable<PaymentMethod> PaymentMethods => _paymentMethods.AsReadOnly();
public PaymentMethod VerifyOrAddPaymentMethod(string cardNumber, int cardType, DateTimeOffset expiration, Guid orderId)
{
var existingPayment = _paymentMethods.SingleOrDefault(p => p.CardNumber.Equals(cardNumber));
if (existingPayment != null)
{
AddDomainEvent(new BuyerAndPaymentMethodVerifiedDomainEvent(this, existingPayment, orderId));
return existingPayment;
}
var payment = new PaymentMethod(cardNumber, cardType, expiration);
_paymentMethods.Add(payment);
AddDomainEvent(new BuyerAndPaymentMethodVerifiedDomainEvent(this, payment, orderId));
return payment;
}
}
}