Index.cshtml.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Application.Features.Config.Commands;
  2. using Application.Features.Config.Queries;
  3. using Application.Features.Config;
  4. using MediatR;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using Microsoft.AspNetCore.Authorization;
  8. namespace Admin.Pages.Config;
  9. public sealed class IndexModel(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 ConfigDto.MetaConfigDto Meta { get; set; } = new();
  34. public ConfigDto.CompanyConfigDto Company { get; set; } = new();
  35. public ConfigDto.AccountConfigDto Account { get; set; } = new();
  36. public ConfigDto.EmailTemplateConfigDto EmailTemplate { get; set; } = new();
  37. public ConfigDto.ExternalApiConfigDto External { get; set; } = new();
  38. public ConfigDto.PaymentConfigDto Payment { get; set; } = new();
  39. public static InputModel From(ConfigDto config)
  40. {
  41. return new()
  42. {
  43. Basic = config.Basic,
  44. Meta = config.Meta,
  45. Company = config.Company,
  46. Account = config.Account,
  47. EmailTemplate = config.EmailTemplate,
  48. External = config.External,
  49. Payment = config.Payment
  50. };
  51. }
  52. public UpdateConfigCommand ToCommand()
  53. {
  54. return new(
  55. Basic,
  56. Meta,
  57. Company,
  58. Account,
  59. EmailTemplate,
  60. External,
  61. Payment
  62. );
  63. }
  64. }
  65. }