feat: support delete selected configurations
This commit is contained in:
@@ -8,7 +8,8 @@
|
||||
OnClick="NewReverseProxy">New...
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Error" StartIcon="@Icons.Material.Filled.Delete"
|
||||
Disabled="@(_selectedCaddyConfigurations.Count <= 0)">Delete
|
||||
Disabled="@(_selectedCaddyConfigurations.Count <= 0)"
|
||||
OnClick="Delete">Delete
|
||||
</MudButton>
|
||||
</MudContainer>
|
||||
<MudList T="string" Style="padding-top: 16px;" SelectionMode="SelectionMode.MultiSelection"
|
||||
|
||||
@@ -14,6 +14,9 @@ public partial class ReverseProxiesPage : ComponentBase
|
||||
|
||||
[Inject]
|
||||
private IDialogService DialogService { get; set; } = null!;
|
||||
|
||||
[Inject]
|
||||
private ISnackbar Snackbar { get; set; } = null!;
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
@@ -55,4 +58,21 @@ public partial class ReverseProxiesPage : ComponentBase
|
||||
_availableCaddyConfigurations = CaddyService.GetExistingCaddyConfigurations();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void Delete()
|
||||
{
|
||||
var response = CaddyService.DeleteCaddyConfigurations(_selectedCaddyConfigurations.ToList());
|
||||
|
||||
_selectedCaddyConfigurations = _selectedCaddyConfigurations.Except(response.DeletedConfigurations).ToList();
|
||||
|
||||
if (response.Success)
|
||||
{
|
||||
Snackbar.Add("Configuration(s) deleted successfully", Severity.Success);
|
||||
Refresh();
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add(response.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,4 +40,11 @@ public interface ICaddyService
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
CaddyOperationResponse SaveCaddyGlobalConfiguration(string content);
|
||||
|
||||
/// <summary>
|
||||
/// Method to delete the given Caddy configurations by name
|
||||
/// </summary>
|
||||
/// <param name="configurationNames"></param>
|
||||
/// <returns></returns>
|
||||
CaddyDeleteOperationResponse DeleteCaddyConfigurations(List<string> configurationNames);
|
||||
}
|
||||
12
CaddyManager/Models/Caddy/CaddyDeleteOperationResponse.cs
Normal file
12
CaddyManager/Models/Caddy/CaddyDeleteOperationResponse.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace CaddyManager.Models.Caddy;
|
||||
|
||||
/// <summary>
|
||||
/// Class to wrap the response of a Caddy delete operation
|
||||
/// </summary>
|
||||
public class CaddyDeleteOperationResponse : CaddyOperationResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// List of configurations that were successfully deleted
|
||||
/// </summary>
|
||||
public List<string> DeletedConfigurations { get; set; } = [];
|
||||
}
|
||||
@@ -21,6 +21,7 @@ public class CaddyService(IConfigurationsService configurationsService) : ICaddy
|
||||
return Directory.GetFiles(Configurations.ConfigDir)
|
||||
.Where(filePath => Path.GetFileName(filePath) != CaddyGlobalConfigName)
|
||||
.Select(Path.GetFileNameWithoutExtension)
|
||||
.Order()
|
||||
.ToList()!;
|
||||
}
|
||||
|
||||
@@ -92,4 +93,41 @@ public class CaddyService(IConfigurationsService configurationsService) : ICaddy
|
||||
FileName = CaddyGlobalConfigName,
|
||||
Content = content
|
||||
});
|
||||
|
||||
/// <inheritdoc />
|
||||
public CaddyDeleteOperationResponse DeleteCaddyConfigurations(List<string> configurationNames)
|
||||
{
|
||||
var failed = new List<string>();
|
||||
|
||||
foreach (var configurationName in configurationNames)
|
||||
{
|
||||
var filePath = Path.Combine(Configurations.ConfigDir,
|
||||
configurationName == CaddyGlobalConfigName ? CaddyGlobalConfigName : $"{configurationName}.caddy");
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
failed.Add(configurationName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
failed.Add(configurationName);
|
||||
}
|
||||
}
|
||||
|
||||
return new CaddyDeleteOperationResponse
|
||||
{
|
||||
Success = failed.Count == 0,
|
||||
Message = failed.Count == 0
|
||||
? "Configuration(s) deleted successfully"
|
||||
: $"Failed to delete the following configuration(s): {string.Join(", ", failed)}",
|
||||
DeletedConfigurations = configurationNames.Except(failed).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user