WalletBalance.cs 1.7 KB

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