Company.cshtml.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Application.Features.Config;
  2. using Application.Features.Config.Commands;
  3. using Application.Features.Config.Queries;
  4. using Application.Features.ReferenceData.Dtos;
  5. using Application.Features.ReferenceData.Queries;
  6. using MediatR;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.RazorPages;
  9. namespace Admin.Pages.Config
  10. {
  11. public class CompanyModel(IMediator mediator) : PageModel
  12. {
  13. [BindProperty]
  14. public InputModel Input { get; set; } = new();
  15. public IReadOnlyList<BankCodeDto> BankCodes { get; set; } = [];
  16. private async Task BindBankCodesAsync(CancellationToken cancellationToken)
  17. {
  18. BankCodes = await mediator.Send(new GetBankCodesQuery(), cancellationToken);
  19. }
  20. public async Task OnGetAsync(CancellationToken cancellationToken)
  21. {
  22. await BindBankCodesAsync(cancellationToken);
  23. var config = await mediator.Send(new GetConfigQuery(), cancellationToken);
  24. if (config is not null)
  25. {
  26. Input = InputModel.From(config);
  27. }
  28. }
  29. public async Task<IActionResult> OnPostAsync(CancellationToken cancellationToken)
  30. {
  31. await BindBankCodesAsync(cancellationToken);
  32. if (!ModelState.IsValid)
  33. {
  34. return Page();
  35. }
  36. await mediator.Send(Input.ToCommand(), cancellationToken);
  37. return RedirectToPage();
  38. }
  39. public sealed class InputModel
  40. {
  41. public ConfigDto.CompanyConfigDto Company { get; set; } = new();
  42. public static InputModel From(ConfigDto config)
  43. {
  44. return new()
  45. {
  46. Company = config.Company
  47. };
  48. }
  49. public UpdateConfigCommand ToCommand()
  50. {
  51. return new(
  52. null,
  53. null,
  54. null,
  55. Company
  56. );
  57. }
  58. }
  59. }
  60. }