Index.cshtml.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Application.Abstractions.Messaging;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. namespace Admin.Pages.Config.Payment;
  5. public class IndexModel(IMediator mediator) : PageModel
  6. {
  7. [BindProperty]
  8. public InputModel Input { get; set; } = new();
  9. /// <summary>저장된 키 존재 여부 — 화면에는 키 원문/암호문을 노출하지 않고 placeholder 로만 표시</summary>
  10. public GetConfig.Response.TossConfigDto KeyState { get; set; } = new();
  11. public async Task OnGetAsync(CancellationToken ct)
  12. {
  13. var config = await mediator.Send(new GetConfig.Query(), ct);
  14. if (config is not null)
  15. {
  16. Input = InputModel.From(config);
  17. KeyState = config.Toss;
  18. }
  19. }
  20. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  21. {
  22. if (!ModelState.IsValid)
  23. {
  24. var config = await mediator.Send(new GetConfig.Query(), ct);
  25. if (config is not null)
  26. {
  27. KeyState = config.Toss;
  28. }
  29. return Page();
  30. }
  31. await mediator.Send(Input.ToCommand(), ct);
  32. TempData["SuccessMessage"] = "저장되었습니다.";
  33. return RedirectToPage();
  34. }
  35. public sealed class InputModel
  36. {
  37. public UpdateConfig.Request.TossConfigDto Toss { get; set; } = new();
  38. public static InputModel From(GetConfig.Response config)
  39. {
  40. var req = UpdateConfig.Request.From(config);
  41. return new()
  42. {
  43. Toss = req.Toss ?? new()
  44. };
  45. }
  46. public UpdateConfig.Command ToCommand()
  47. {
  48. return new(
  49. Toss: Toss
  50. );
  51. }
  52. }
  53. }