WalletTransaction.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Domain.Entities.Common.ValueObject;
  4. using Domain.Entities.Wallets.ValueObject;
  5. namespace Domain.Entities.Wallets
  6. {
  7. public class WalletTransaction
  8. {
  9. [ForeignKey(nameof(WalletKey))]
  10. public virtual Wallet Wallet { get; private set; } = null!;
  11. [Key]
  12. public int ID { get; private set; }
  13. public Guid WalletKey { get; private set; }
  14. public WalletBalanceType BalanceType { get; private set; }
  15. public WalletTransactionType TxType { get; private set; }
  16. public Money Amount { get; private set; } = Money.KRW(0);
  17. public Money BalanceAfter { get; private set; } = Money.KRW(0);
  18. public string Reason { get; private set; } = default!;
  19. public string? RefID { get; private set; }
  20. public string? UserID { get; private set; }
  21. public string? Memo { get; private set; }
  22. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  23. private WalletTransaction() { }
  24. private WalletTransaction(
  25. Guid walletKey,
  26. WalletBalanceType balanceType,
  27. WalletTransactionType txType,
  28. Money amount,
  29. Money balanceAfter,
  30. string reason,
  31. string? refID,
  32. string? userID,
  33. string? memo
  34. ) {
  35. WalletKey = walletKey;
  36. BalanceType = balanceType;
  37. TxType = txType;
  38. Amount = amount;
  39. BalanceAfter = balanceAfter;
  40. Reason = reason;
  41. RefID = refID;
  42. UserID = userID;
  43. Memo = memo;
  44. }
  45. public static WalletTransaction Create(
  46. Guid walletKey,
  47. WalletBalanceType balanceType,
  48. WalletTransactionType txType,
  49. Money amount,
  50. Money balanceAfter,
  51. string reason,
  52. string? refID = null,
  53. string? userID = null,
  54. string? memo = null
  55. ) {
  56. if (walletKey == Guid.Empty)
  57. {
  58. throw new ArgumentException("WalletKey is required.", nameof(walletKey));
  59. }
  60. if (amount.IsZero || amount.Value <= 0)
  61. {
  62. throw new ArgumentException("Transaction amount must be positive.", nameof(amount));
  63. }
  64. if (string.IsNullOrWhiteSpace(reason))
  65. {
  66. throw new ArgumentException("Reason is required.", nameof(reason));
  67. }
  68. return new WalletTransaction(walletKey, balanceType, txType, amount, balanceAfter, reason, refID, userID, memo);
  69. }
  70. }
  71. }