Company.cshtml.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. return RedirectToPage();
  34. }
  35. public sealed class InputModel
  36. {
  37. public UpdateConfig.Request.CompanyConfigDto Company { get; set; } = new();
  38. public static InputModel From(GetConfig.Response config)
  39. {
  40. var req = UpdateConfig.Request.From(config);
  41. return new()
  42. {
  43. Company = req.Company
  44. };
  45. }
  46. public UpdateConfig.Command ToCommand()
  47. {
  48. return new(
  49. Company: Company
  50. );
  51. }
  52. }
  53. }
  54. }