updating nginx add and delete

This commit is contained in:
2020-07-02 19:39:14 +02:00
parent 720ed56432
commit ca43e89583
19 changed files with 455 additions and 335 deletions

View File

@ -3,72 +3,6 @@
<FilesWithEditor CFile="ConfigFile" Filters="Filters" Files="ConfigFiles" FilterFolder="FilterFolder"
SelectedFile="SelectedFile" SelectedFileChanged="SelectedFileChanged"
TestConfiguration="TestConfiguration" TestResult="TestResult"
DeleteFileModal="DeleteNotificationModal" AddFileModal="ShowAddFileModal">
DeleteFileCallback="DeleteFile" ShowAddFileModal="ShowAddFileModal" >
</FilesWithEditor>
<Modal @ref="AddFileModal">
<ModalBackdrop />
<ModalContent Class="neomorph">
<ModalHeader>
<ModalTitle Class="has-text-centered">Add a new configuration for a service</ModalTitle>
</ModalHeader>
<ModalBody>
<div class="field">
<label class="label">Template</label>
<div class="control has-icons-left">
<div class="select is-small is-rounded neoSelect fullwidth">
<select class="fullwidth" @bind="NewFileForm.SelectedTemplate">
<option value="0">No template</option>
@foreach (var template in NewFileForm.Templates)
{
<option value="@template.Name">@($"{template.Name.First().ToString().ToUpper()}{template.Name.Substring(1)}")</option>
}
</select>
</div>
<span class="icon is-small is-left has-text-dark">
<i class="mdi mdi-puzzle-outline"></i>
</span>
</div>
<p class="help">Any template to quick setup the configuration</p>
</div>
<div class="field">
<label class="label">Configuration file name</label>
<div class="control has-icons-left has-icons-right">
<input class="input is-rounded is-small neoInput" type="text" @bind="NewFileForm.Name" placeholder="Name" />
<span class="icon is-small is-left has-text-dark">
<i class="mdi mdi-file-code-outline"></i>
</span>
</div>
<p class="help">Name it the same as the service which is going to run behind</p>
</div>
</ModalBody>
<ModalFooter>
<div class="level fullwidth">
<div class="level-left">
<div class="level-item">
<Blazorise.Bulma.Button Clicked="e => CloseModal(AddFileModal)"
Class="is-rounded neoBtnSmall is-small has-text-dark">
<span class="icon is-small">
<i class="mdi mdi-close"></i>
</span>
<span>Close</span>
</Blazorise.Bulma.Button>
</div>
</div>
<div class="level-right">
<div class="level-item">
<Blazorise.Bulma.Button Color="Color.Primary" Clicked="AddFileAsync"
Class="is-rounded neoBtnSmall is-small has-text-dark" Type="ButtonType.Submit">
<span class="icon is-small has-text-success">
<i class="mdi mdi-plus"></i>
</span>
<span>Add</span>
</Blazorise.Bulma.Button>
</div>
</div>
</div>
</ModalFooter>
</ModalContent>
</Modal>
<GeneralNotificationModal PopupCallback="DeleteFile"></GeneralNotificationModal>
@*<GeneralNotificationModal ></GeneralNotificationModal>*@

View File

@ -1,23 +1,23 @@
using Blazorise;
using Blazored.Modal;
using Blazored.Modal.Services;
using Blazorise;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Seenginx.Components;
using Seenginx.Models;
using Seenginx.Services;
using Seenginx.Shared;
using Seenginx.Utility;
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; }
[Inject] public INginxService NginxService { get; set; }
[Inject] public IFileManager FileService { get; set; }
[Inject] public IModalService Modal { get; set; }
public string InputSearch { get; set; }
@ -26,6 +26,7 @@ namespace Seenginx.Pages
public List<string> Filters { get; set; } = new List<string>();
public List<int> FilteredOutFiles { get; set; } = new List<int>();
public NotificationSettings GeneralNotificationSettings { get; set; } = null;
public List<Template> Templates { get; set; } = new List<Template>();
public Dictionary<string, string> FilterFolder { get; set; } = new Dictionary<string, string>();
@ -40,7 +41,7 @@ namespace Seenginx.Pages
FilterFolder.Add("Conf.d", "/conf.d");
FilterFolder.Add("Available", "/sites-available");
FilterFolder.Add("Enabled", "/sites-enabled");
NewFileForm.Templates.AddRange(await NginxService.GetTemplates());
Templates.AddRange(await NginxService.GetTemplates());
}
catch (Exception ex)
{
@ -54,83 +55,71 @@ namespace Seenginx.Pages
SelectedFile = configFile;
}
protected Modal AddFileModal { get; set; }
public void ShowAddFileModal()
public async Task ShowAddFileModal()
{
AddFileModal.Show();
}
var parameters = new ModalParameters();
parameters.Add(nameof(Templates), Templates);
public NewFileForm NewFileForm { get; set; } = new NewFileForm();
public async Task AddFileAsync()
{
var addFileResult = await NginxService.AddFileAsync(NewFileForm);
var resultAwait = Modal.Show<NginxConfigForm>(string.Empty, parameters);
var result = await resultAwait.Result;
if (result.Cancelled) return;
if (!addFileResult.AllOk)
throw new Exception(":/");
var validationResult = await NginxService.ValidateNewConfigurationAsync((NewFileForm)result.Data);
if (!validationResult.AllOk)
{
var validationPopupParameters = new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage);
var validationPopup = Modal.Show<GenericPopup>(string.Empty, validationPopupParameters);
await validationPopup.Result;
validationPopup.Close();
return;
}
var addFileResult = await NginxService.AddFileAsync((NewFileForm)result.Data);
if (SelectedFile != null)
SelectedFile.Deselect();
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();
//}
SelectedFile = ConfigFiles.Find(cf => cf.Name == addFileResult.Data.Name);
SelectedFile.Select();
}
public Result<ConfigFile> SaveUpdateDraftResult { get; set; }
public async Task SaveUpdateDraftFileAsync()
{
SaveUpdateDraftResult = await FileService.SaveUpdateDraftFileAsync(SelectedFile);
var saveUpdateDraftResult = await FileService.SaveUpdateDraftFileAsync(SelectedFile);
}
public Result<ConfigFile> SaveUpdateResult { get; set; }
public async Task SaveUpdateFileAsync()
{
SaveUpdateResult = await FileService.SaveUpdateFileAsync(SelectedFile);
var saveUpdateResult = await FileService.SaveUpdateFileAsync(SelectedFile);
}
public Result<string> 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)
public async Task DeleteFile()
{
if (popupAnswer == PopupAnswer.Yes)
var parameters = new ModalParameters().Setup(PopupType.YesNo, $"Are you sure you want to delete '{SelectedFile.Name}'?");
var resultAwait = Modal.Show<GenericPopup>(string.Empty, parameters);
var result = await resultAwait.Result;
if ((PopupAnswer)result.Data == PopupAnswer.No) return;
var deleteFileResult = await NginxService.DeleteConfigurationFileAsync(SelectedFile);
if (!deleteFileResult.AllOk)
{
var deleteResult = FileService.DeleteFile(SelectedFile);
var errorParameters = new ModalParameters().Setup(PopupType.Ok, $"Something went wrong, here's the error message: '{deleteFileResult.ErrorMessage}'?");
var errorReportModalAwait = Modal.Show<GenericPopup>(string.Empty, errorParameters);
await errorReportModalAwait.Result;
return;
}
}
public void CloseModal(Modal modal)
{
modal.Hide();
ConfigFiles.Remove(SelectedFile);
SelectedFile = null;
}
}
}