Register.cshtml.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using MediatR;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. namespace Admin.Pages.Config
  5. {
  6. public class RegisterModel(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. return RedirectToPage();
  26. }
  27. public sealed class InputModel
  28. {
  29. public UpdateConfig.Request.AccountConfigDto Account { get; set; } = new();
  30. public static InputModel From(GetConfig.Response config)
  31. {
  32. var req = UpdateConfig.Request.From(config);
  33. return new()
  34. {
  35. Account = req.Account
  36. };
  37. }
  38. public UpdateConfig.Command ToCommand()
  39. {
  40. return new(
  41. Account: Account
  42. );
  43. }
  44. }
  45. }
  46. }