Basic.cshtml.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Application.Features.Config;
  2. using Application.Features.Config.Commands;
  3. using Application.Features.Config.Queries;
  4. using MediatR;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. namespace Admin.Pages.Config
  8. {
  9. public class BasicModel(IMediator mediator) : PageModel
  10. {
  11. [BindProperty]
  12. public InputModel Input { get; set; } = new();
  13. public async Task OnGetAsync(CancellationToken cancellationToken)
  14. {
  15. var config = await mediator.Send(new GetConfigQuery(), cancellationToken);
  16. if (config is not null)
  17. {
  18. Input = InputModel.From(config);
  19. }
  20. }
  21. public async Task<IActionResult> OnPostAsync(CancellationToken cancellationToken)
  22. {
  23. if (!ModelState.IsValid)
  24. {
  25. return Page();
  26. }
  27. await mediator.Send(Input.ToCommand(), cancellationToken);
  28. return RedirectToPage();
  29. }
  30. public sealed class InputModel
  31. {
  32. public ConfigDto.BasicConfigDto Basic { get; set; } = new();
  33. public static InputModel From(ConfigDto config)
  34. {
  35. return new()
  36. {
  37. Basic = config.Basic
  38. };
  39. }
  40. public UpdateConfigCommand ToCommand()
  41. {
  42. return new(
  43. Basic
  44. );
  45. }
  46. }
  47. }
  48. }