Meta.cshtml.cs 1.3 KB

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