Made all database calls async. Fixed smaller issue

This commit is contained in:
2025-01-06 22:37:01 +01:00
parent f652945a3f
commit 0b143d1c81
8 changed files with 55 additions and 56 deletions

View File

@@ -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<IPagedData<NodeAllocationDetailResponse>> 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<NodeAllocationDetailResponse> 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<NodeAllocationDetailResponse> Create([FromRoute] int nodeId, [FromBody] CreateNodeAllocationRequest request)
{
ApplyNode(nodeId);
await ApplyNode(nodeId);
var allocation = Mapper.Map<Allocation>(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<NodeAllocationDetailResponse> 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")]

View File

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

View File

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

View File

@@ -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<IPagedData<StarDockerImageDetailResponse>> 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<StarDockerImageDetailResponse> 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<StarDockerImageDetailResponse> Create([FromRoute] int starId, [FromBody] CreateStarDockerImageRequest request)
{
ApplyStar(starId);
await ApplyStar(starId);
var starDockerImage = Mapper.Map<StarDockerImage>(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<StarDockerImageDetailResponse> 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);
}

View File

@@ -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<IPagedData<StarVariableDetailResponse>> 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<StarVariableDetailResponse> 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<StarVariableDetailResponse> Create([FromRoute] int starId, [FromBody] CreateStarVariableRequest request)
{
ApplyStar(starId);
await ApplyStar(starId);
var starVariable = Mapper.Map<StarVariable>(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<StarVariableDetailResponse> 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);
}

View File

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

View File

@@ -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;
}

View File

@@ -198,7 +198,7 @@
catch (HttpApiException e)
{
if (e.Status == 404)
NotFound = false;
NotFound = true;
else
throw;
}