diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs index 6bf1ed7..d4f65d8 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs @@ -146,7 +146,7 @@ public class NodeAllocationsController : Controller [HttpGet("{nodeId:int}/allocations/free")] [RequirePermission("admin.servers.nodes.get")] - public async Task> GetFree([FromRoute] int nodeId, [FromQuery] int page, [FromQuery] int pageSize) + public async Task> 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); } diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Servers/ServersController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Servers/ServersController.cs index 0aaf9cc..21f7ca1 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Servers/ServersController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Servers/ServersController.cs @@ -80,7 +80,7 @@ public class ServersController : Controller { // Construct model var server = Mapper.Map(request); - + // Check if owner user exist if (UserRepository.Get().All(x => x.Id != request.OwnerId)) throw new HttpApiException("No user with this id found", 400); @@ -160,7 +160,7 @@ public class ServersController : Controller // Set relations server.Node = node; server.Star = star; - + // TODO: Call node var finalServer = ServerRepository.Add(server); @@ -171,7 +171,45 @@ public class ServersController : Controller [HttpPatch("{id:int}")] public async Task 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(); + + // 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}")] diff --git a/MoonlightServers.Frontend/UI/Components/Servers/UpdateServerPartials/AllocationsServerUpdate.razor b/MoonlightServers.Frontend/UI/Components/Servers/UpdateServerPartials/AllocationsServerUpdate.razor index b05e767..1d3618f 100644 --- a/MoonlightServers.Frontend/UI/Components/Servers/UpdateServerPartials/AllocationsServerUpdate.razor +++ b/MoonlightServers.Frontend/UI/Components/Servers/UpdateServerPartials/AllocationsServerUpdate.razor @@ -29,16 +29,12 @@ private async Task Loader() { - // Handle unselected node - if (Server.NodeId <= 0) - return []; - var items = await PagedData.All(async (page, pageSize) => await ApiClient.GetJson>( - $"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; } } \ No newline at end of file diff --git a/MoonlightServers.Frontend/UI/Views/Admin/All/Update.razor b/MoonlightServers.Frontend/UI/Views/Admin/All/Update.razor index 76cb9f7..c366105 100644 --- a/MoonlightServers.Frontend/UI/Views/Admin/All/Update.razor +++ b/MoonlightServers.Frontend/UI/Views/Admin/All/Update.razor @@ -54,7 +54,7 @@ private async Task Load(LazyLoader _) { - var Server = await ApiClient.GetJson($"api/admin/servers/{Id}"); + Server = await ApiClient.GetJson($"api/admin/servers/{Id}"); Request = Mapper.Map(Server); }