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 NotificationSettings GeneralNotificationSettings { get; set; } = null; public Dictionary FilterFolder { get; set; } = new Dictionary(); protected override async Task OnParametersSetAsync() { try { 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"); NewFileForm.Templates.AddRange(await NginxService.GetTemplates()); } catch (Exception ex) { throw ex; } await base.OnParametersSetAsync(); } public void SelectedFileChanged(ConfigFile configFile) { SelectedFile = configFile; } protected Modal AddFileModal { get; set; } public void ShowAddFileModal() { AddFileModal.Show(); } public NewFileForm NewFileForm { get; set; } = new NewFileForm(); public async Task AddFileAsync() { var addFileResult = await NginxService.AddFileAsync(NewFileForm); if (!addFileResult.AllOk) throw new Exception(":/"); ConfigFiles.Add(addFileResult.Data); ConfigFiles = ConfigFiles.OrderBy(cf => cf.Name).ToList(); AddFileModal.Hide(); //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 Modal TestNotificationModal { get; set; } = new Modal(); public async Task TestConfiguration() { TestResult = await NginxService.TestNginxConfigurations(SelectedFile); if (TestResult.AllOk) GeneralNotificationSettings = new NotificationSettings { Text = $"Test of config file '{SelectedFile.Name}' completed with success.", PopupType = PopupType.Ok }; else GeneralNotificationSettings = new NotificationSettings { Text = TestResult.ErrorMessage, PopupType = PopupType.Ok }; TestNotificationModal.Show(); } public GeneralNotificationModalBase DeleteNotificationModal { get; set; } = new GeneralNotificationModalBase(); public void DeleteFile(PopupAnswer popupAnswer) { if (popupAnswer == PopupAnswer.Yes) { var deleteResult = FileService.DeleteFile(SelectedFile); } } public void CloseModal(Modal modal) { modal.Hide(); } } }