Meta.cshtml.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 MetaModel(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.MetaConfigDto Meta { get; set; } = new();
  33. public static InputModel From(ConfigDto config)
  34. {
  35. return new()
  36. {
  37. Meta = config.Meta
  38. };
  39. }
  40. public UpdateConfigCommand ToCommand()
  41. {
  42. return new(
  43. null,
  44. null,
  45. Meta
  46. );
  47. }
  48. }
  49. }
  50. }