External.cshtml.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Application.Abstractions.Messaging;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. namespace Admin.Pages.Config;
  5. public class ExternalModel(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.ExternalApiConfigDto External { get; set; } = new();
  30. public UpdateConfig.Request.DataCollectionConfigDto DataCollection { get; set; } = new();
  31. public static InputModel From(GetConfig.Response config)
  32. {
  33. var req = UpdateConfig.Request.From(config);
  34. return new()
  35. {
  36. External = req.External,
  37. DataCollection = req.DataCollection ?? new()
  38. };
  39. }
  40. public UpdateConfig.Command ToCommand()
  41. {
  42. return new(
  43. External: External,
  44. DataCollection: DataCollection
  45. );
  46. }
  47. }
  48. }