Added allocations to server update
This commit is contained in:
@@ -146,7 +146,7 @@ public class NodeAllocationsController : Controller
|
|||||||
|
|
||||||
[HttpGet("{nodeId:int}/allocations/free")]
|
[HttpGet("{nodeId:int}/allocations/free")]
|
||||||
[RequirePermission("admin.servers.nodes.get")]
|
[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
|
var node = NodeRepository
|
||||||
.Get()
|
.Get()
|
||||||
@@ -159,7 +159,7 @@ public class NodeAllocationsController : Controller
|
|||||||
|
|
||||||
CrudHelper.QueryModifier = variables => variables
|
CrudHelper.QueryModifier = variables => variables
|
||||||
.Where(x => x.Node.Id == node.Id)
|
.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);
|
return await CrudHelper.Get(page, pageSize);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ public class ServersController : Controller
|
|||||||
{
|
{
|
||||||
// Construct model
|
// Construct model
|
||||||
var server = Mapper.Map<Server>(request);
|
var server = Mapper.Map<Server>(request);
|
||||||
|
|
||||||
// Check if owner user exist
|
// Check if owner user exist
|
||||||
if (UserRepository.Get().All(x => x.Id != request.OwnerId))
|
if (UserRepository.Get().All(x => x.Id != request.OwnerId))
|
||||||
throw new HttpApiException("No user with this id found", 400);
|
throw new HttpApiException("No user with this id found", 400);
|
||||||
@@ -160,7 +160,7 @@ public class ServersController : Controller
|
|||||||
// Set relations
|
// Set relations
|
||||||
server.Node = node;
|
server.Node = node;
|
||||||
server.Star = star;
|
server.Star = star;
|
||||||
|
|
||||||
// TODO: Call node
|
// TODO: Call node
|
||||||
|
|
||||||
var finalServer = ServerRepository.Add(server);
|
var finalServer = ServerRepository.Add(server);
|
||||||
@@ -171,7 +171,45 @@ public class ServersController : Controller
|
|||||||
[HttpPatch("{id:int}")]
|
[HttpPatch("{id:int}")]
|
||||||
public async Task<ServerDetailResponse> Update([FromRoute] int id, [FromBody] UpdateServerRequest request)
|
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}")]
|
[HttpDelete("{id:int}")]
|
||||||
|
|||||||
@@ -29,16 +29,12 @@
|
|||||||
|
|
||||||
private async Task<NodeAllocationDetailResponse[]> Loader()
|
private async Task<NodeAllocationDetailResponse[]> Loader()
|
||||||
{
|
{
|
||||||
// Handle unselected node
|
|
||||||
if (Server.NodeId <= 0)
|
|
||||||
return [];
|
|
||||||
|
|
||||||
var items = await PagedData<NodeAllocationDetailResponse>.All(async (page, pageSize) =>
|
var items = await PagedData<NodeAllocationDetailResponse>.All(async (page, pageSize) =>
|
||||||
await ApiClient.GetJson<PagedData<NodeAllocationDetailResponse>>(
|
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}"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@
|
|||||||
|
|
||||||
private async Task Load(LazyLoader _)
|
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);
|
Request = Mapper.Map<UpdateServerRequest>(Server);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user