Company.cshtml.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using GetBank = Application.Features.Admin.ReferenceData.GetBank;
  2. using MediatR;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. namespace Admin.Pages.Config
  6. {
  7. public class CompanyModel(IMediator mediator) : PageModel
  8. {
  9. [BindProperty]
  10. public InputModel Input { get; set; } = new();
  11. public IReadOnlyList<GetBank.Response> BankCodes { get; set; } = [];
  12. private async Task BindBankCodesAsync(CancellationToken ct)
  13. {
  14. BankCodes = await mediator.Send(new GetBank.Query(), ct);
  15. }
  16. public async Task OnGetAsync(CancellationToken ct)
  17. {
  18. await BindBankCodesAsync(ct);
  19. var config = await mediator.Send(new GetConfig.Query(), ct);
  20. if (config is not null)
  21. {
  22. Input = InputModel.From(config);
  23. }
  24. }
  25. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  26. {
  27. await BindBankCodesAsync(ct);
  28. if (!ModelState.IsValid)
  29. {
  30. return Page();
  31. }
  32. await mediator.Send(Input.ToCommand(), ct);
  33. TempData["SuccessMessage"] = "저장되었습니다.";
  34. return RedirectToPage();
  35. }
  36. public sealed class InputModel
  37. {
  38. public UpdateConfig.Request.CompanyConfigDto Company { get; set; } = new();
  39. public static InputModel From(GetConfig.Response config)
  40. {
  41. var req = UpdateConfig.Request.From(config);
  42. return new()
  43. {
  44. Company = req.Company
  45. };
  46. }
  47. public UpdateConfig.Command ToCommand()
  48. {
  49. return new(
  50. Company: Company
  51. );
  52. }
  53. }
  54. }
  55. }