Added multi allocation actions in node update page

This commit is contained in:
2024-12-14 18:38:12 +01:00
parent 680827e0ea
commit 747712c5c4
4 changed files with 167 additions and 10 deletions

View File

@@ -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<int>();
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;
}
}