diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs index 2e5c445..02fb1a5 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs @@ -90,4 +90,57 @@ public class NodeAllocationsController : Controller await CrudHelper.Delete(id); } + + [HttpPost("{nodeId:int}/allocations/range")] + public Task CreateRange([FromRoute] int nodeId, [FromBody] CreateNodeAllocationRangeRequest rangeRequest) + { + ApplyNode(nodeId); + + var existingAllocations = AllocationRepository + .Get() + .Where(x => x.Node.Id == nodeId) + .ToArray(); + + var ports = new List(); + + for (var i = rangeRequest.Start; i < rangeRequest.End; i++) + { + // Skip existing allocations + if(existingAllocations.Any(x => x.Port == i && x.IpAddress == rangeRequest.IpAddress)) + continue; + + ports.Add(i); + } + + var allocations = ports + .Select(port => new Allocation() + { + IpAddress = rangeRequest.IpAddress, + Port = port, + Node = Node + }) + .ToArray(); + + // TODO: Add AddRange in database repository + foreach (var allocation in allocations) + AllocationRepository.Add(allocation); + + return Task.CompletedTask; + } + + [HttpDelete("{nodeId:int}/allocations/all")] + public Task DeleteAll([FromRoute] int nodeId) + { + ApplyNode(nodeId); + + var allocations = AllocationRepository + .Get() + .Where(x => x.Node.Id == nodeId) + .ToArray(); + + foreach (var allocation in allocations) + AllocationRepository.Delete(allocation); + + return Task.CompletedTask; + } } \ No newline at end of file diff --git a/MoonlightServers.Frontend/UI/Components/Nodes/Modals/CreateMultipleAllocationModal.razor b/MoonlightServers.Frontend/UI/Components/Nodes/Modals/CreateMultipleAllocationModal.razor new file mode 100644 index 0000000..7386f22 --- /dev/null +++ b/MoonlightServers.Frontend/UI/Components/Nodes/Modals/CreateMultipleAllocationModal.razor @@ -0,0 +1,55 @@ +@using MoonCore.Blazor.Tailwind.Modals.Components +@using MoonCore.Blazor.Tailwind.Components +@using MoonlightServers.Shared.Http.Requests.Admin.NodeAllocations + +@inherits BaseModal + +

Add a range of new allocations

+ + +
+
+ + +
+ +
+ + +
+ +
+ + +
+
+
+ +
+ Cancel + Create +
+ +@code +{ + [Parameter] public Func OnSubmit { get; set; } + + private CreateNodeAllocationRangeRequest Form; + private HandleForm HandleForm; + + protected override void OnInitialized() + { + Form = new() + { + IpAddress = "0.0.0.0" + }; + } + + private async Task OnValidSubmit() + { + await OnSubmit.Invoke(Form); + await Hide(); + } + + private Task Submit() => HandleForm.Submit(); +} \ No newline at end of file diff --git a/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor b/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor index 923f0c5..7ebeba1 100644 --- a/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor +++ b/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor @@ -17,14 +17,15 @@
-
+
Actions
- - + +
@@ -40,10 +41,12 @@