using Blazorise; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using Seenginx.Components; using Seenginx.Models; using Seenginx.Services; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace Seenginx.Pages { public class NginxBase : ComponentBase { [Inject] public INginxService NginxService { get; set; } [Inject] public IFileManager FileService { get; set; } public string InputSearch { get; set; } public List ConfigFiles { get; set; } = new List(); public ConfigFile SelectedFile { get; set; } public List Filters { get; set; } = new List(); public List FilteredOutFiles { get; set; } = new List(); public Modal GeneralNotificationModal { get; set; } = new Modal(); public GeneralNotificationSettings GeneralNotificationSettings { get; set; } = null; public Dictionary FilterFolder { get; set; } = new Dictionary(); protected override async Task OnInitializedAsync() { ConfigFiles.AddRange(await NginxService.GetFilesAsync()); Filters.AddRange(new List { "All", "Root", "Conf.d", "Available", "Enabled" }); FilterFolder.Add("All", null); FilterFolder.Add("Root", "/"); FilterFolder.Add("Conf.d", "/conf.d"); FilterFolder.Add("Available", "/sites-available"); FilterFolder.Add("Enabled", "/sites-enabled"); await base.OnInitializedAsync(); } public async Task SelectedFileChanged(ConfigFile configFile) { SelectedFile = configFile; } protected Modal AddFileModal { get; set; } public Result AddFileResult { get; set; } public async Task ShowAddFileModal() { AddFileModal.Show(); } public NewFileForm NewFileForm { get; set; } = new NewFileForm(); public async Task AddFileAsync() { AddFileResult = await NginxService.AddFileAsync(NewFileForm); if (AddFileResult.AllOk) ConfigFiles.Add(AddFileResult.Data); else { GeneralNotificationSettings = new GeneralNotificationSettings { ButtonColor = Color.Danger, TitleClass = "mdi-error", Title = "Failure", Text = TestResult.ErrorMessage }; GeneralNotificationModal.Show(); } } public Result SaveUpdateDraftResult { get; set; } public async Task SaveUpdateDraftFileAsync() { SaveUpdateDraftResult = await FileService.SaveUpdateDraftFileAsync(SelectedFile); } public Result SaveUpdateResult { get; set; } public async Task SaveUpdateFileAsync() { SaveUpdateResult = await FileService.SaveUpdateFileAsync(SelectedFile); } public Result TestResult { get; set; } public async Task TestConfiguration() { TestResult = await NginxService.TestNginxConfigurations(SelectedFile); if (TestResult.AllOk) GeneralNotificationSettings = new GeneralNotificationSettings { ButtonColor = Color.Success, TitleClass = "mdi-success", Title = "Success", Text = $"Test of config file '{SelectedFile.Name}' completed with success." }; else GeneralNotificationSettings = new GeneralNotificationSettings { ButtonColor = Color.Danger, TitleClass = "mdi-error", Title = "Failure", Text = TestResult.ErrorMessage }; GeneralNotificationModal.Show(); } public Result DeleteResult { get; set; } public void DeleteFile() { DeleteResult = FileService.DeleteFile(SelectedFile); } public void CloseModal(Modal modal) { modal.Hide(); } } }