WalletBalance.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Domain.Entities.Common.ValueObject;
  2. using Domain.Entities.Wallets.ValueObject;
  3. namespace Domain.Entities.Wallets
  4. {
  5. public class WalletBalance
  6. {
  7. public int ID { get; private set; }
  8. public Guid WalletKey { get; private set; }
  9. public WalletBalanceType Type { get; private set; }
  10. public Money Amount { get; private set; } = Money.KRW(0);
  11. private WalletBalance() { }
  12. private WalletBalance(Guid walletKey, WalletBalanceType type, Money initial)
  13. {
  14. if (walletKey == Guid.Empty)
  15. {
  16. throw new ArgumentException("WalletKey is required.", nameof(walletKey));
  17. }
  18. WalletKey = walletKey;
  19. Type = type;
  20. Amount = initial;
  21. }
  22. public static WalletBalance Create(Guid walletKey, WalletBalanceType type, string currency = "KRW")
  23. {
  24. return new(walletKey, type, Money.Zero(currency));
  25. }
  26. public void Increase(Money amount)
  27. {
  28. EnsurePositive(amount);
  29. Amount = Amount + amount;
  30. }
  31. public void Decrease(Money amount)
  32. {
  33. EnsurePositive(amount);
  34. if (Amount.Value < amount.Value)
  35. {
  36. throw new InvalidOperationException("Insufficient balance.");
  37. }
  38. Amount = Amount - amount;
  39. }
  40. private static void EnsurePositive(Money amount)
  41. {
  42. if (amount.IsZero || amount.Value <= 0)
  43. {
  44. throw new ArgumentException("Amount must be positive.", nameof(amount));
  45. }
  46. }
  47. }
  48. }