diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs index d4f65d8..428974b 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using MoonCore.Attributes; using MoonCore.Exceptions; using MoonCore.Extended.Abstractions; @@ -28,11 +29,11 @@ public class NodeAllocationsController : Controller AllocationRepository = allocationRepository; } - private void ApplyNode(int id) + private async Task ApplyNode(int id) { - var node = NodeRepository + var node = await NodeRepository .Get() - .FirstOrDefault(x => x.Id == id); + .FirstOrDefaultAsync(x => x.Id == id); if (node == null) throw new HttpApiException("A node with this id could not be found", 404); @@ -47,7 +48,7 @@ public class NodeAllocationsController : Controller [RequirePermission("admin.servers.nodes.get")] public async Task> Get([FromRoute] int nodeId, [FromQuery] int page, [FromQuery] int pageSize) { - ApplyNode(nodeId); + await ApplyNode(nodeId); return await CrudHelper.Get(page, pageSize); } @@ -56,7 +57,7 @@ public class NodeAllocationsController : Controller [RequirePermission("admin.servers.nodes.get")] public async Task GetSingle([FromRoute] int nodeId, [FromRoute] int id) { - ApplyNode(nodeId); + await ApplyNode(nodeId); return await CrudHelper.GetSingle(id); } @@ -65,12 +66,12 @@ public class NodeAllocationsController : Controller [RequirePermission("admin.servers.nodes.create")] public async Task Create([FromRoute] int nodeId, [FromBody] CreateNodeAllocationRequest request) { - ApplyNode(nodeId); + await ApplyNode(nodeId); var allocation = Mapper.Map(request); allocation.Node = Node; - var finalVariable = AllocationRepository.Add(allocation); + var finalVariable = await AllocationRepository.Add(allocation); return CrudHelper.MapToResult(finalVariable); } @@ -78,7 +79,7 @@ public class NodeAllocationsController : Controller [HttpPatch("{nodeId:int}/allocations/{id:int}")] public async Task Update([FromRoute] int nodeId, [FromRoute] int id, [FromBody] UpdateNodeAllocationRequest request) { - ApplyNode(nodeId); + await ApplyNode(nodeId); return await CrudHelper.Update(id, request); } @@ -86,15 +87,15 @@ public class NodeAllocationsController : Controller [HttpDelete("{nodeId:int}/allocations/{id:int}")] public async Task Delete([FromRoute] int nodeId, [FromRoute] int id) { - ApplyNode(nodeId); + await ApplyNode(nodeId); await CrudHelper.Delete(id); } [HttpPost("{nodeId:int}/allocations/range")] - public Task CreateRange([FromRoute] int nodeId, [FromBody] CreateNodeAllocationRangeRequest rangeRequest) + public async Task CreateRange([FromRoute] int nodeId, [FromBody] CreateNodeAllocationRangeRequest rangeRequest) { - ApplyNode(nodeId); + await ApplyNode(nodeId); var existingAllocations = AllocationRepository .Get() @@ -123,15 +124,13 @@ public class NodeAllocationsController : Controller // TODO: Add AddRange in database repository foreach (var allocation in allocations) - AllocationRepository.Add(allocation); - - return Task.CompletedTask; + await AllocationRepository.Add(allocation); } [HttpDelete("{nodeId:int}/allocations/all")] - public Task DeleteAll([FromRoute] int nodeId) + public async Task DeleteAll([FromRoute] int nodeId) { - ApplyNode(nodeId); + await ApplyNode(nodeId); var allocations = AllocationRepository .Get() @@ -139,9 +138,7 @@ public class NodeAllocationsController : Controller .ToArray(); foreach (var allocation in allocations) - AllocationRepository.Delete(allocation); - - return Task.CompletedTask; + await AllocationRepository.Remove(allocation); } [HttpGet("{nodeId:int}/allocations/free")] diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodesController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodesController.cs index 5025ef2..386a74a 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodesController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodesController.cs @@ -46,7 +46,7 @@ public class NodesController : Controller node.Token = Formatter.GenerateString(32); - var finalNode = NodeRepository.Add(node); + var finalNode = await NodeRepository.Add(node); return CrudHelper.MapToResult(finalNode); } diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Servers/ServersController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Servers/ServersController.cs index b91923c..bd29d4a 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Servers/ServersController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Servers/ServersController.cs @@ -83,18 +83,18 @@ public class ServersController : Controller if (UserRepository.Get().All(x => x.Id != request.OwnerId)) throw new HttpApiException("No user with this id found", 400); - var star = StarRepository + var star = await StarRepository .Get() .Include(x => x.Variables) .Include(x => x.DockerImages) - .FirstOrDefault(x => x.Id == request.StarId); + .FirstOrDefaultAsync(x => x.Id == request.StarId); if (star == null) throw new HttpApiException("No star with this id found", 400); - var node = NodeRepository + var node = await NodeRepository .Get() - .FirstOrDefault(x => x.Id == request.NodeId); + .FirstOrDefaultAsync(x => x.Id == request.NodeId); if (node == null) throw new HttpApiException("No node with this id found", 400); @@ -104,11 +104,11 @@ public class ServersController : Controller // Fetch specified allocations from the request foreach (var allocationId in request.AllocationIds) { - var allocation = AllocationRepository + var allocation = await AllocationRepository .Get() .Where(x => x.Server == null) .Where(x => x.Node.Id == node.Id) - .FirstOrDefault(x => x.Id == allocationId); + .FirstOrDefaultAsync(x => x.Id == allocationId); if (allocation == null) continue; @@ -121,11 +121,12 @@ public class ServersController : Controller { var amountRequiredToSatisfy = star.RequiredAllocations - allocations.Count; - var freeAllocations = AllocationRepository + var freeAllocations = await AllocationRepository .Get() .Where(x => x.Server == null) .Where(x => x.Node.Id == node.Id) - .Take(amountRequiredToSatisfy); + .Take(amountRequiredToSatisfy) + .ToArrayAsync(); allocations.AddRange(freeAllocations); @@ -163,7 +164,7 @@ public class ServersController : Controller // TODO: Call node - var finalServer = ServerRepository.Add(server); + var finalServer = await ServerRepository.Add(server); return CrudHelper.MapToResult(finalServer); } @@ -180,11 +181,11 @@ public class ServersController : Controller // Fetch specified allocations from the request foreach (var allocationId in request.AllocationIds) { - var allocation = AllocationRepository + var allocation = await 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); + .FirstOrDefaultAsync(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 @@ -223,7 +224,7 @@ public class ServersController : Controller // TODO: Call node - ServerRepository.Update(server); + await ServerRepository.Update(server); return CrudHelper.MapToResult(server); } diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarDockerImagesController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarDockerImagesController.cs index da68dc3..ab00aad 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarDockerImagesController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarDockerImagesController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using MoonCore.Exceptions; using MoonCore.Extended.Abstractions; using MoonCore.Extended.Helpers; @@ -31,11 +32,11 @@ public class StarDockerImagesController : Controller StarDockerImageRepository = starDockerImageRepository; } - private void ApplyStar(int id) + private async Task ApplyStar(int id) { - var star = StarRepository + var star = await StarRepository .Get() - .FirstOrDefault(x => x.Id == id); + .FirstOrDefaultAsync(x => x.Id == id); if (star == null) throw new HttpApiException("A star with this id could not be found", 404); @@ -49,7 +50,7 @@ public class StarDockerImagesController : Controller [HttpGet("{starId:int}/dockerImages")] public async Task> Get([FromRoute] int starId, [FromQuery] int page, [FromQuery] int pageSize) { - ApplyStar(starId); + await ApplyStar(starId); return await CrudHelper.Get(page, pageSize); } @@ -57,7 +58,7 @@ public class StarDockerImagesController : Controller [HttpGet("{starId:int}/dockerImages/{id:int}")] public async Task GetSingle([FromRoute] int starId, [FromRoute] int id) { - ApplyStar(starId); + await ApplyStar(starId); return await CrudHelper.GetSingle(id); } @@ -65,12 +66,12 @@ public class StarDockerImagesController : Controller [HttpPost("{starId:int}/dockerImages")] public async Task Create([FromRoute] int starId, [FromBody] CreateStarDockerImageRequest request) { - ApplyStar(starId); + await ApplyStar(starId); var starDockerImage = Mapper.Map(request); starDockerImage.Star = Star; - var finalVariable = StarDockerImageRepository.Add(starDockerImage); + var finalVariable = await StarDockerImageRepository.Add(starDockerImage); return CrudHelper.MapToResult(finalVariable); } @@ -79,7 +80,7 @@ public class StarDockerImagesController : Controller public async Task Update([FromRoute] int starId, [FromRoute] int id, [FromBody] UpdateStarDockerImageRequest request) { - ApplyStar(starId); + await ApplyStar(starId); return await CrudHelper.Update(id, request); } @@ -87,7 +88,7 @@ public class StarDockerImagesController : Controller [HttpDelete("{starId:int}/dockerImages/{id:int}")] public async Task Delete([FromRoute] int starId, [FromRoute] int id) { - ApplyStar(starId); + await ApplyStar(starId); await CrudHelper.Delete(id); } diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarVariablesController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarVariablesController.cs index 7e8fd53..d15952a 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarVariablesController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarVariablesController.cs @@ -32,11 +32,11 @@ public class StarVariablesController : Controller StarVariableRepository = starVariableRepository; } - private void ApplyStar(int id) + private async Task ApplyStar(int id) { - var star = StarRepository + var star = await StarRepository .Get() - .FirstOrDefault(x => x.Id == id); + .FirstOrDefaultAsync(x => x.Id == id); if (star == null) throw new HttpApiException("A star with this id could not be found", 404); @@ -51,7 +51,7 @@ public class StarVariablesController : Controller [RequirePermission("admin.servers.stars.get")] public async Task> Get([FromRoute] int starId, [FromQuery] int page, [FromQuery] int pageSize) { - ApplyStar(starId); + await ApplyStar(starId); return await CrudHelper.Get(page, pageSize); } @@ -60,7 +60,7 @@ public class StarVariablesController : Controller [RequirePermission("admin.servers.stars.get")] public async Task GetSingle([FromRoute] int starId, [FromRoute] int id) { - ApplyStar(starId); + await ApplyStar(starId); return await CrudHelper.GetSingle(id); } @@ -69,12 +69,12 @@ public class StarVariablesController : Controller [RequirePermission("admin.servers.stars.create")] public async Task Create([FromRoute] int starId, [FromBody] CreateStarVariableRequest request) { - ApplyStar(starId); + await ApplyStar(starId); var starVariable = Mapper.Map(request); starVariable.Star = Star; - var finalVariable = StarVariableRepository.Add(starVariable); + var finalVariable = await StarVariableRepository.Add(starVariable); return CrudHelper.MapToResult(finalVariable); } @@ -84,13 +84,13 @@ public class StarVariablesController : Controller public async Task Update([FromRoute] int starId, [FromRoute] int id, [FromBody] UpdateStarVariableRequest request) { - ApplyStar(starId); + await ApplyStar(starId); var variable = await CrudHelper.GetSingleModel(id); variable = Mapper.Map(variable, request); - StarVariableRepository.Update(variable); + await StarVariableRepository.Update(variable); return CrudHelper.MapToResult(variable); } @@ -99,7 +99,7 @@ public class StarVariablesController : Controller [RequirePermission("admin.servers.stars.delete")] public async Task Delete([FromRoute] int starId, [FromRoute] int id) { - ApplyStar(starId); + await ApplyStar(starId); await CrudHelper.Delete(id); } diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarsController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarsController.cs index 342c0ed..d3a1916 100644 --- a/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarsController.cs +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Stars/StarsController.cs @@ -61,7 +61,7 @@ public class StarsController : Controller star.DefaultDockerImage = -1; star.ParseConfiguration = "[]"; - var finalStar = StarRepository.Add(star); + var finalStar = await StarRepository.Add(star); return CrudHelper.MapToResult(finalStar); } diff --git a/MoonlightServers.ApiServer/Services/StarImportExportService.cs b/MoonlightServers.ApiServer/Services/StarImportExportService.cs index 253eeb7..eeeac97 100644 --- a/MoonlightServers.ApiServer/Services/StarImportExportService.cs +++ b/MoonlightServers.ApiServer/Services/StarImportExportService.cs @@ -138,7 +138,7 @@ public class StarImportExportService }).ToList() }; - var finalStar = StarRepository.Add(star); + var finalStar = await StarRepository.Add(star); return finalStar; } @@ -235,7 +235,7 @@ public class StarImportExportService #endregion - var finalStar = StarRepository.Add(star); + var finalStar = await StarRepository.Add(star); return finalStar; } @@ -397,7 +397,7 @@ public class StarImportExportService star.AllowDockerImageChange = true; // Finally save it to the db - var finalStar = StarRepository.Add(star); + var finalStar = await StarRepository.Add(star); return finalStar; } diff --git a/MoonlightServers.Frontend/UI/Views/User/Manage.razor b/MoonlightServers.Frontend/UI/Views/User/Manage.razor index 865db8b..33bd1cf 100644 --- a/MoonlightServers.Frontend/UI/Views/User/Manage.razor +++ b/MoonlightServers.Frontend/UI/Views/User/Manage.razor @@ -198,7 +198,7 @@ catch (HttpApiException e) { if (e.Status == 404) - NotFound = false; + NotFound = true; else throw; }