WalletTransaction.cs 2.4 KB

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