using Application.Features.Config.Commands; using Application.Features.Config.Queries; using Application.Features.Config; using MediatR; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Authorization; namespace Admin.Pages.Config; public sealed class IndexModel(IMediator mediator) : PageModel { [BindProperty] public InputModel Input { get; set; } = new(); public async Task OnGetAsync(CancellationToken cancellationToken) { var config = await mediator.Send(new GetConfigQuery(), cancellationToken); if (config is not null) { Input = InputModel.From(config); } } public async Task OnPostAsync(CancellationToken cancellationToken) { if (!ModelState.IsValid) { return Page(); } await mediator.Send(Input.ToCommand(), cancellationToken); return RedirectToPage(); } public sealed class InputModel { public ConfigDto.BasicConfigDto Basic { get; set; } = new(); public ConfigDto.MetaConfigDto Meta { get; set; } = new(); public ConfigDto.CompanyConfigDto Company { get; set; } = new(); public ConfigDto.AccountConfigDto Account { get; set; } = new(); public ConfigDto.EmailTemplateConfigDto EmailTemplate { get; set; } = new(); public ConfigDto.ExternalApiConfigDto External { get; set; } = new(); public ConfigDto.PaymentConfigDto Payment { get; set; } = new(); public static InputModel From(ConfigDto config) { return new() { Basic = config.Basic, Meta = config.Meta, Company = config.Company, Account = config.Account, EmailTemplate = config.EmailTemplate, External = config.External, Payment = config.Payment }; } public UpdateConfigCommand ToCommand() { return new( Basic, Meta, Company, Account, EmailTemplate, External, Payment ); } } }