Email.cshtml.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using MediatR;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. namespace Admin.Pages.Config.Template
  5. {
  6. public class EmailModel(IMediator mediator) : PageModel
  7. {
  8. [BindProperty]
  9. public InputModel Input { get; set; } = new();
  10. public async Task OnGetAsync(CancellationToken ct)
  11. {
  12. var config = await mediator.Send(new GetConfig.Query(), ct);
  13. if (config is not null)
  14. {
  15. Input = InputModel.From(config);
  16. }
  17. }
  18. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  19. {
  20. if (!ModelState.IsValid)
  21. {
  22. return Page();
  23. }
  24. await mediator.Send(Input.ToCommand(), ct);
  25. TempData["SuccessMessage"] = "저장되었습니다.";
  26. return RedirectToPage();
  27. }
  28. public sealed class InputModel
  29. {
  30. public UpdateConfig.Request.EmailTemplateConfigDto EmailTemplate { get; set; } = new();
  31. public static InputModel From(GetConfig.Response config)
  32. {
  33. var req = UpdateConfig.Request.From(config);
  34. return new()
  35. {
  36. EmailTemplate = req.EmailTemplate
  37. };
  38. }
  39. public UpdateConfig.Command ToCommand()
  40. {
  41. return new(
  42. EmailTemplate: EmailTemplate
  43. );
  44. }
  45. }
  46. }
  47. }