| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using Domain.Entities.Common.ValueObject;
- using Domain.Entities.Wallets.ValueObject;
- namespace Domain.Entities.Wallets
- {
- public class WalletBalance
- {
- public int ID { get; private set; }
- public Guid WalletKey { get; private set; }
- public WalletBalanceType Type { get; private set; }
- public Money Amount { get; private set; } = Money.KRW(0);
- private WalletBalance() { }
- private WalletBalance(Guid walletKey, WalletBalanceType type, Money initial)
- {
- if (walletKey == Guid.Empty)
- {
- throw new ArgumentException("WalletKey is required.", nameof(walletKey));
- }
- WalletKey = walletKey;
- Type = type;
- Amount = initial;
- }
- public static WalletBalance Create(Guid walletKey, WalletBalanceType type, string currency = "KRW")
- {
- return new(walletKey, type, Money.Zero(currency));
- }
- public void Increase(Money amount)
- {
- EnsurePositive(amount);
- Amount = Amount + amount;
- }
- public void Decrease(Money amount)
- {
- EnsurePositive(amount);
- if (Amount.Value < amount.Value)
- {
- throw new InvalidOperationException("Insufficient balance.");
- }
- Amount = Amount - amount;
- }
- private static void EnsurePositive(Money amount)
- {
- if (amount.IsZero || amount.Value <= 0)
- {
- throw new ArgumentException("Amount must be positive.", nameof(amount));
- }
- }
- }
- }
|