Added allocations to server update

This commit is contained in:
2024-12-21 13:14:09 +01:00
parent 0baf9668f9
commit 2b697dffb7
4 changed files with 46 additions and 12 deletions

View File

@@ -146,7 +146,7 @@ public class NodeAllocationsController : Controller
[HttpGet("{nodeId:int}/allocations/free")]
[RequirePermission("admin.servers.nodes.get")]
public async Task<IPagedData<NodeAllocationDetailResponse>> GetFree([FromRoute] int nodeId, [FromQuery] int page, [FromQuery] int pageSize)
public async Task<IPagedData<NodeAllocationDetailResponse>> GetFree([FromRoute] int nodeId, [FromQuery] int page, [FromQuery] int pageSize, [FromQuery] int serverId = -1)
{
var node = NodeRepository
.Get()
@@ -159,7 +159,7 @@ public class NodeAllocationsController : Controller
CrudHelper.QueryModifier = variables => variables
.Where(x => x.Node.Id == node.Id)
.Where(x => x.Server == null);
.Where(x => x.Server == null || x.Server.Id == serverId);
return await CrudHelper.Get(page, pageSize);
}

View File

@@ -171,7 +171,45 @@ public class ServersController : Controller
[HttpPatch("{id:int}")]
public async Task<ServerDetailResponse> Update([FromRoute] int id, [FromBody] UpdateServerRequest request)
{
return await CrudHelper.Update(id, request);
var server = await CrudHelper.GetSingleModel(id);
server = Mapper.Map(server, request);
var allocations = new List<Allocation>();
// Fetch specified allocations from the request
foreach (var allocationId in request.AllocationIds)
{
var allocation = AllocationRepository
.Get()
.Where(x => x.Server == null || x.Server.Id == server.Id)
.Where(x => x.Node.Id == server.Node.Id)
.FirstOrDefault(x => x.Id == allocationId);
// ^ This loads the allocations specified in the request.
// Valid allocations are either free ones or ones which are already allocated to this server
if (allocation == null)
continue;
allocations.Add(allocation);
}
// Check if the specified allocations are enough for the star
if (allocations.Count < server.Star.RequiredAllocations)
{
throw new HttpApiException(
$"You need to specify at least {server.Star.RequiredAllocations} allocation(s)",
400
);
}
// Set allocations
server.Allocations = allocations;
ServerRepository.Update(server);
return CrudHelper.MapToResult(server);
}
[HttpDelete("{id:int}")]

View File

@@ -29,13 +29,9 @@
private async Task<NodeAllocationDetailResponse[]> Loader()
{
// Handle unselected node
if (Server.NodeId <= 0)
return [];
var items = await PagedData<NodeAllocationDetailResponse>.All(async (page, pageSize) =>
await ApiClient.GetJson<PagedData<NodeAllocationDetailResponse>>(
$"api/admin/servers/nodes/{Server.NodeId}/allocations/free?page={page}&pageSize={pageSize}"
$"api/admin/servers/nodes/{Server.NodeId}/allocations/free?page={page}&pageSize={pageSize}&serverId={Server.Id}"
)
);

View File

@@ -54,7 +54,7 @@
private async Task Load(LazyLoader _)
{
var Server = await ApiClient.GetJson<ServerDetailResponse>($"api/admin/servers/{Id}");
Server = await ApiClient.GetJson<ServerDetailResponse>($"api/admin/servers/{Id}");
Request = Mapper.Map<UpdateServerRequest>(Server);
}