WalletBalance.cs 1.5 KB

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