Seenginx/Seenginx/Pages/Nginx.razor.cs

137 lines
3.9 KiB
C#
Raw Normal View History

2020-04-24 00:18:16 +02:00
using Blazorise;
using Microsoft.AspNetCore.Components;
2020-05-03 02:10:32 +02:00
using Microsoft.AspNetCore.Components.Web;
using Seenginx.Components;
2020-04-13 23:58:26 +02:00
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; }
2020-05-02 18:57:42 +02:00
[Inject]
public IFileManager FileService { get; set; }
2020-04-13 23:58:26 +02:00
public string InputSearch { get; set; }
public List<ConfigFile> ConfigFiles { get; set; } = new List<ConfigFile>();
2020-04-16 18:50:05 +02:00
public ConfigFile SelectedFile { get; set; }
public List<string> Filters { get; set; } = new List<string>();
public List<int> FilteredOutFiles { get; set; } = new List<int>();
2020-07-02 09:15:33 +02:00
public NotificationSettings GeneralNotificationSettings { get; set; } = null;
2020-04-16 18:50:05 +02:00
2020-04-18 20:41:46 +02:00
public Dictionary<string, string> FilterFolder { get; set; } = new Dictionary<string, string>();
2020-04-13 23:58:26 +02:00
2020-07-01 19:36:22 +02:00
protected override async Task OnParametersSetAsync()
2020-04-13 23:58:26 +02:00
{
2020-07-01 19:36:22 +02:00
try
{
ConfigFiles.AddRange(await NginxService.GetFilesAsync());
Filters.AddRange(new List<string> { "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();
2020-04-13 23:58:26 +02:00
}
2020-07-01 19:36:22 +02:00
public void SelectedFileChanged(ConfigFile configFile)
2020-04-16 18:50:05 +02:00
{
SelectedFile = configFile;
2020-04-13 23:58:26 +02:00
}
2020-04-24 00:18:16 +02:00
2020-05-03 02:10:32 +02:00
protected Modal AddFileModal { get; set; }
2020-07-01 19:36:22 +02:00
public void ShowAddFileModal()
2020-04-24 00:18:16 +02:00
{
2020-05-03 02:10:32 +02:00
AddFileModal.Show();
2020-04-24 00:18:16 +02:00
}
2020-07-01 19:36:22 +02:00
2020-05-03 02:10:32 +02:00
public NewFileForm NewFileForm { get; set; } = new NewFileForm();
public async Task AddFileAsync()
2020-04-24 00:18:16 +02:00
{
2020-07-01 19:36:22 +02:00
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();
//}
2020-04-24 00:18:16 +02:00
}
2020-05-03 02:10:32 +02:00
public Result<ConfigFile> SaveUpdateDraftResult { get; set; }
public async Task SaveUpdateDraftFileAsync()
2020-04-16 18:50:05 +02:00
{
2020-05-03 02:10:32 +02:00
SaveUpdateDraftResult = await FileService.SaveUpdateDraftFileAsync(SelectedFile);
2020-04-16 18:50:05 +02:00
}
2020-05-02 18:57:42 +02:00
2020-05-03 02:10:32 +02:00
public Result<ConfigFile> SaveUpdateResult { get; set; }
public async Task SaveUpdateFileAsync()
{
SaveUpdateResult = await FileService.SaveUpdateFileAsync(SelectedFile);
}
2020-05-02 18:57:42 +02:00
2020-05-03 02:10:32 +02:00
public Result<string> TestResult { get; set; }
2020-07-02 09:15:33 +02:00
public Modal TestNotificationModal { get; set; } = new Modal();
2020-05-03 02:10:32 +02:00
public async Task TestConfiguration()
{
TestResult = await NginxService.TestNginxConfigurations(SelectedFile);
if (TestResult.AllOk)
2020-07-02 09:15:33 +02:00
GeneralNotificationSettings = new NotificationSettings
2020-05-03 02:10:32 +02:00
{
2020-07-02 09:15:33 +02:00
Text = $"Test of config file '{SelectedFile.Name}' completed with success.",
PopupType = PopupType.Ok
2020-05-03 02:10:32 +02:00
};
else
2020-07-02 09:15:33 +02:00
GeneralNotificationSettings = new NotificationSettings
2020-05-03 02:10:32 +02:00
{
2020-07-02 09:15:33 +02:00
Text = TestResult.ErrorMessage,
PopupType = PopupType.Ok
2020-05-03 02:10:32 +02:00
};
2020-07-02 09:15:33 +02:00
TestNotificationModal.Show();
2020-05-03 02:10:32 +02:00
}
2020-05-02 18:57:42 +02:00
2020-07-02 09:15:33 +02:00
public GeneralNotificationModalBase DeleteNotificationModal { get; set; } = new GeneralNotificationModalBase();
public void DeleteFile(PopupAnswer popupAnswer)
2020-04-16 18:50:05 +02:00
{
2020-07-02 09:15:33 +02:00
if (popupAnswer == PopupAnswer.Yes)
{
var deleteResult = FileService.DeleteFile(SelectedFile);
}
2020-04-16 18:50:05 +02:00
}
2020-05-02 18:57:42 +02:00
2020-05-03 02:10:32 +02:00
public void CloseModal(Modal modal)
{
modal.Hide();
}
2020-04-13 23:58:26 +02:00
}
}