View.cshtml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Domain.Entities.Wallets.ValueObject;
  2. using Infrastructure.Persistence.Identity;
  3. using Application.Abstractions.Messaging;
  4. using Microsoft.AspNetCore.Identity;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using System.ComponentModel;
  8. using System.ComponentModel.DataAnnotations;
  9. namespace Admin.Pages.Member.Wallet.List;
  10. public class ViewModel(IMediator mediator, UserManager<ApplicationUser> userManager) : PageModel
  11. {
  12. public WalletInfo Wallet { get; set; } = new();
  13. public MemberInfo Member { get; set; } = new();
  14. [BindProperty]
  15. [Required(ErrorMessage = "충전 금액을 입력해주세요.")]
  16. [Range(-99999999, 99999999, ErrorMessage = "금액은 -99,999,999 ~ 99,999,999 범위입니다.")]
  17. [DisplayName("충전 금액")]
  18. public long Amount { get; set; }
  19. [BindProperty]
  20. [DisplayName("잔액 유형")]
  21. public WalletBalanceType? BalanceType { get; set; }
  22. [BindProperty]
  23. [Required(ErrorMessage = "비밀번호를 입력해주세요.")]
  24. [DisplayName("비밀번호")]
  25. public string Password { get; set; } = default!;
  26. [BindProperty]
  27. [MaxLength(1000, ErrorMessage = "메모는 {1}자 이하로 입력하세요.")]
  28. [DisplayName("메모")]
  29. public string? Memo { get; set; }
  30. public sealed class WalletInfo
  31. {
  32. public int ID { get; set; }
  33. public string Balance { get; set; } = "0";
  34. public List<BalanceRow> Balances { get; set; } = [];
  35. }
  36. public sealed record BalanceRow(
  37. WalletBalanceType Type,
  38. string TypeName,
  39. string Amount
  40. );
  41. public static string GetBalanceTypeName(WalletBalanceType type) => type switch
  42. {
  43. WalletBalanceType.PgCharged => "PG 충전",
  44. WalletBalanceType.Deposit => "직접 입금",
  45. WalletBalanceType.Donation => "후원",
  46. WalletBalanceType.Reward => "보상",
  47. WalletBalanceType.Airdrop => "에어드랍",
  48. WalletBalanceType.Locked => "잠금",
  49. WalletBalanceType.Adjusted => "조정",
  50. WalletBalanceType.StoreRevenue => "상점 수익",
  51. _ => type.ToString()
  52. };
  53. public sealed class MemberInfo
  54. {
  55. public int ID { get; set; }
  56. public string Email { get; set; } = default!;
  57. public string? GradeName { get; set; }
  58. }
  59. public async Task<IActionResult> OnGetAsync(int id, CancellationToken ct)
  60. {
  61. var walletResult = await mediator.Send(new GetWallet.Query(id), ct);
  62. if (walletResult.IsFailure)
  63. {
  64. TempData["ErrorMessages"] = walletResult.Error.Description;
  65. return RedirectToPage("Index");
  66. }
  67. var result = walletResult.Value;
  68. Wallet = new WalletInfo
  69. {
  70. ID = result.WalletID,
  71. Balance = result.Balance.ToString("N0"),
  72. Balances = [..result.Balances.Select(c => new BalanceRow(c.Type, GetBalanceTypeName(c.Type), c.Amount.ToString("N0")))]
  73. };
  74. Member = new MemberInfo
  75. {
  76. ID = result.MemberID,
  77. Email = result.MemberEmail,
  78. GradeName = result.GradeName
  79. };
  80. return Page();
  81. }
  82. public async Task<IActionResult> OnPostChargeAsync(int id, CancellationToken ct)
  83. {
  84. if (Amount == 0)
  85. {
  86. TempData["ErrorMessages"] = "충전 금액을 입력해주세요.";
  87. return RedirectToPage("View", new { id });
  88. }
  89. var admin = await userManager.GetUserAsync(User);
  90. if (admin is null || !await userManager.CheckPasswordAsync(admin, Password))
  91. {
  92. TempData["ErrorMessages"] = "비밀번호가 올바르지 않습니다.";
  93. return RedirectToPage("View", new { id });
  94. }
  95. var result = await mediator.Send(new ChargeWallet.Command(
  96. id,
  97. Amount,
  98. Memo,
  99. BalanceType
  100. ), ct);
  101. if (result.IsFailure)
  102. {
  103. TempData["ErrorMessages"] = result.Error.Description;
  104. }
  105. else
  106. {
  107. TempData["SuccessMessage"] = Amount > 0
  108. ? $"{Amount:N0}P 충전되었습니다."
  109. : $"{Math.Abs(Amount):N0}P 차감되었습니다.";
  110. }
  111. return RedirectToPage("View", new { id });
  112. }
  113. }