Started refactoring to be compatible with the changed nuget packages and the mooncore changes
This commit is contained in:
@@ -6,6 +6,7 @@ using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Models;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Mappers;
|
||||
using MoonlightServers.Shared.Http.Requests.Admin.NodeAllocations;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations;
|
||||
|
||||
@@ -29,9 +30,9 @@ public class NodeAllocationsController : Controller
|
||||
|
||||
[HttpGet("{nodeId:int}/allocations")]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
|
||||
public async Task<IPagedData<NodeAllocationDetailResponse>> Get(
|
||||
public async Task<IPagedData<NodeAllocationResponse>> Get(
|
||||
[FromRoute] int nodeId,
|
||||
[FromQuery] int page,
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize
|
||||
)
|
||||
{
|
||||
@@ -44,14 +45,11 @@ public class NodeAllocationsController : Controller
|
||||
.Where(x => x.Node.Id == nodeId)
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedAllocations = allocations.Select(x => new NodeAllocationDetailResponse()
|
||||
{
|
||||
Id = x.Id,
|
||||
IpAddress = x.IpAddress,
|
||||
Port = x.Port
|
||||
}).ToArray();
|
||||
var mappedAllocations = allocations
|
||||
.Select(AllocationMapper.ToNodeAllocation)
|
||||
.ToArray();
|
||||
|
||||
return new PagedData<NodeAllocationDetailResponse>()
|
||||
return new PagedData<NodeAllocationResponse>()
|
||||
{
|
||||
Items = mappedAllocations,
|
||||
CurrentPage = page,
|
||||
@@ -63,7 +61,7 @@ public class NodeAllocationsController : Controller
|
||||
|
||||
[HttpGet("{nodeId:int}/allocations/{id:int}")]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
|
||||
public async Task<NodeAllocationDetailResponse> GetSingle([FromRoute] int nodeId, [FromRoute] int id)
|
||||
public async Task<NodeAllocationResponse> GetSingle([FromRoute] int nodeId, [FromRoute] int id)
|
||||
{
|
||||
var allocation = await AllocationRepository
|
||||
.Get()
|
||||
@@ -73,17 +71,12 @@ public class NodeAllocationsController : Controller
|
||||
if (allocation == null)
|
||||
throw new HttpApiException("No allocation with that id found", 404);
|
||||
|
||||
return new()
|
||||
{
|
||||
Id = allocation.Id,
|
||||
IpAddress = allocation.IpAddress,
|
||||
Port = allocation.Port
|
||||
};
|
||||
return AllocationMapper.ToNodeAllocation(allocation);
|
||||
}
|
||||
|
||||
[HttpPost("{nodeId:int}/allocations")]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.create")]
|
||||
public async Task<NodeAllocationDetailResponse> Create(
|
||||
public async Task<NodeAllocationResponse> Create(
|
||||
[FromRoute] int nodeId,
|
||||
[FromBody] CreateNodeAllocationRequest request
|
||||
)
|
||||
@@ -95,26 +88,19 @@ public class NodeAllocationsController : Controller
|
||||
if (node == null)
|
||||
throw new HttpApiException("No node with that id found", 404);
|
||||
|
||||
var allocation = new Allocation
|
||||
{
|
||||
IpAddress = request.IpAddress,
|
||||
Port = request.Port,
|
||||
Node = node
|
||||
};
|
||||
var allocation = AllocationMapper.ToAllocation(request);
|
||||
|
||||
var finalVariable = await AllocationRepository.Add(allocation);
|
||||
var finalAllocation = await AllocationRepository.Add(allocation);
|
||||
|
||||
return new()
|
||||
{
|
||||
Id = finalVariable.Id,
|
||||
IpAddress = finalVariable.IpAddress,
|
||||
Port = finalVariable.Port
|
||||
};
|
||||
return AllocationMapper.ToNodeAllocation(finalAllocation);
|
||||
}
|
||||
|
||||
[HttpPatch("{nodeId:int}/allocations/{id:int}")]
|
||||
public async Task<NodeAllocationDetailResponse> Update([FromRoute] int nodeId, [FromRoute] int id,
|
||||
[FromBody] UpdateNodeAllocationRequest request)
|
||||
public async Task<NodeAllocationResponse> Update(
|
||||
[FromRoute] int nodeId,
|
||||
[FromRoute] int id,
|
||||
[FromBody] UpdateNodeAllocationRequest request
|
||||
)
|
||||
{
|
||||
var allocation = await AllocationRepository
|
||||
.Get()
|
||||
@@ -124,17 +110,10 @@ public class NodeAllocationsController : Controller
|
||||
if (allocation == null)
|
||||
throw new HttpApiException("No allocation with that id found", 404);
|
||||
|
||||
allocation.IpAddress = request.IpAddress;
|
||||
allocation.Port = request.Port;
|
||||
|
||||
AllocationMapper.Merge(request, allocation);
|
||||
await AllocationRepository.Update(allocation);
|
||||
|
||||
return new()
|
||||
{
|
||||
Id = allocation.Id,
|
||||
IpAddress = allocation.IpAddress,
|
||||
Port = allocation.Port
|
||||
};
|
||||
|
||||
return AllocationMapper.ToNodeAllocation(allocation);
|
||||
}
|
||||
|
||||
[HttpDelete("{nodeId:int}/allocations/{id:int}")]
|
||||
@@ -160,7 +139,7 @@ public class NodeAllocationsController : Controller
|
||||
|
||||
if (node == null)
|
||||
throw new HttpApiException("No node with that id found", 404);
|
||||
|
||||
|
||||
var existingAllocations = AllocationRepository
|
||||
.Get()
|
||||
.Where(x => x.Node.Id == nodeId)
|
||||
@@ -202,8 +181,12 @@ public class NodeAllocationsController : Controller
|
||||
|
||||
[HttpGet("{nodeId:int}/allocations/free")]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
|
||||
public async Task<IPagedData<NodeAllocationDetailResponse>> GetFree([FromRoute] int nodeId, [FromQuery] int page,
|
||||
[FromQuery][Range(1, 100)] int pageSize, [FromQuery] int serverId = -1)
|
||||
public async Task<IPagedData<NodeAllocationResponse>> GetFree(
|
||||
[FromRoute] int nodeId,
|
||||
[FromQuery] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize,
|
||||
[FromQuery] int serverId = -1
|
||||
)
|
||||
{
|
||||
var node = NodeRepository
|
||||
.Get()
|
||||
@@ -220,14 +203,11 @@ public class NodeAllocationsController : Controller
|
||||
var count = await freeAllocationsQuery.CountAsync();
|
||||
var allocations = await freeAllocationsQuery.ToArrayAsync();
|
||||
|
||||
var mappedAllocations = allocations.Select(x => new NodeAllocationDetailResponse()
|
||||
{
|
||||
Id = x.Id,
|
||||
IpAddress = x.IpAddress,
|
||||
Port = x.Port
|
||||
}).ToArray();
|
||||
var mappedAllocations = allocations
|
||||
.Select(AllocationMapper.ToNodeAllocation)
|
||||
.ToArray();
|
||||
|
||||
return new PagedData<NodeAllocationDetailResponse>()
|
||||
return new PagedData<NodeAllocationResponse>()
|
||||
{
|
||||
Items = mappedAllocations,
|
||||
CurrentPage = page,
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Helpers;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Models;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Mappers;
|
||||
using MoonlightServers.Shared.Http.Requests.Admin.Nodes;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.Nodes;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes;
|
||||
@@ -16,57 +17,102 @@ namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes;
|
||||
[Route("api/admin/servers/nodes")]
|
||||
public class NodesController : Controller
|
||||
{
|
||||
private readonly CrudHelper<Node, NodeDetailResponse> CrudHelper;
|
||||
private readonly DatabaseRepository<Node> NodeRepository;
|
||||
|
||||
public NodesController(
|
||||
CrudHelper<Node, NodeDetailResponse> crudHelper,
|
||||
DatabaseRepository<Node> nodeRepository
|
||||
)
|
||||
{
|
||||
CrudHelper = crudHelper;
|
||||
NodeRepository = nodeRepository;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
|
||||
public async Task<IPagedData<NodeDetailResponse>> Get([FromQuery] int page, [FromQuery] int pageSize)
|
||||
public async Task<IPagedData<NodeResponse>> Get(
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize
|
||||
)
|
||||
{
|
||||
return await CrudHelper.Get(page, pageSize);
|
||||
var query = NodeRepository
|
||||
.Get();
|
||||
|
||||
var count = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedItems = items
|
||||
.Select(NodeMapper.ToAdminNodeResponse)
|
||||
.ToArray();
|
||||
|
||||
return new PagedData<NodeResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : count / pageSize
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
|
||||
public async Task<NodeDetailResponse> GetSingle([FromRoute] int id)
|
||||
public async Task<NodeResponse> GetSingle([FromRoute] int id)
|
||||
{
|
||||
return await CrudHelper.GetSingle(id);
|
||||
var node = await NodeRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (node == null)
|
||||
throw new HttpApiException("No node with this id found", 404);
|
||||
|
||||
return NodeMapper.ToAdminNodeResponse(node);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.create")]
|
||||
public async Task<NodeDetailResponse> Create([FromBody] CreateNodeRequest request)
|
||||
public async Task<NodeResponse> Create([FromBody] CreateNodeRequest request)
|
||||
{
|
||||
var node = Mapper.Map<Node>(request);
|
||||
var node = NodeMapper.ToNode(request);
|
||||
|
||||
node.TokenId = Formatter.GenerateString(6);
|
||||
node.Token = Formatter.GenerateString(32);
|
||||
|
||||
var finalNode = await NodeRepository.Add(node);
|
||||
|
||||
return CrudHelper.MapToResult(finalNode);
|
||||
return NodeMapper.ToAdminNodeResponse(finalNode);
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.update")]
|
||||
public async Task<NodeDetailResponse> Update([FromRoute] int id, [FromBody] UpdateNodeRequest request)
|
||||
public async Task<NodeResponse> Update([FromRoute] int id, [FromBody] UpdateNodeRequest request)
|
||||
{
|
||||
return await CrudHelper.Update(id, request);
|
||||
var node = await NodeRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (node == null)
|
||||
throw new HttpApiException("No node with this id found", 404);
|
||||
|
||||
node = NodeMapper.Merge(request, node);
|
||||
await NodeRepository.Update(node);
|
||||
|
||||
return NodeMapper.ToAdminNodeResponse(node);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
[Authorize(Policy = "permissions:admin.servers.nodes.delete")]
|
||||
public async Task Delete([FromRoute] int id)
|
||||
{
|
||||
await CrudHelper.Delete(id);
|
||||
var node = await NodeRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (node == null)
|
||||
throw new HttpApiException("No node with this id found", 404);
|
||||
|
||||
await NodeRepository.Remove(node);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Models;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Mappers;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.ServerVariables;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Servers;
|
||||
@@ -17,32 +18,39 @@ public class ServerVariablesController : Controller
|
||||
private readonly DatabaseRepository<ServerVariable> VariableRepository;
|
||||
private readonly DatabaseRepository<Server> ServerRepository;
|
||||
|
||||
public ServerVariablesController(DatabaseRepository<ServerVariable> variableRepository, DatabaseRepository<Server> serverRepository)
|
||||
public ServerVariablesController(DatabaseRepository<ServerVariable> variableRepository,
|
||||
DatabaseRepository<Server> serverRepository)
|
||||
{
|
||||
VariableRepository = variableRepository;
|
||||
ServerRepository = serverRepository;
|
||||
}
|
||||
|
||||
[HttpGet("{serverId}/variables")]
|
||||
[Authorize(Policy = "permissions:admin.servers.get")]
|
||||
public async Task<PagedData<ServerVariableDetailResponse>> Get([FromRoute] int serverId, [FromQuery] int page, [FromQuery] int pageSize)
|
||||
[Authorize(Policy = "permissions:admin.servers.read")]
|
||||
public async Task<PagedData<ServerVariableResponse>> Get(
|
||||
[FromRoute] int serverId,
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize
|
||||
)
|
||||
{
|
||||
var server = await ServerRepository
|
||||
var serverExists = await ServerRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == serverId);
|
||||
.AnyAsync(x => x.Id == serverId);
|
||||
|
||||
if (server == null)
|
||||
if (!serverExists)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
|
||||
|
||||
var variables = await VariableRepository
|
||||
.Get()
|
||||
.Where(x => x.Server.Id == server.Id)
|
||||
.Where(x => x.Server.Id == serverId)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var castedVariables = variables
|
||||
.Select(x => Mapper.Map<ServerVariableDetailResponse>(x))
|
||||
.Select(ServerVariableMapper.ToAdminResponse)
|
||||
.ToArray();
|
||||
|
||||
return PagedData<ServerVariableDetailResponse>.Create(castedVariables, page, pageSize);
|
||||
return PagedData<ServerVariableResponse>.Create(castedVariables, page, pageSize);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Helpers;
|
||||
@@ -8,6 +10,7 @@ using MoonCore.Helpers;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Mappers;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.Shared.Http.Requests.Admin.Servers;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.Servers;
|
||||
@@ -18,7 +21,6 @@ namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Servers;
|
||||
[Route("api/admin/servers")]
|
||||
public class ServersController : Controller
|
||||
{
|
||||
private readonly CrudHelper<Server, ServerDetailResponse> CrudHelper;
|
||||
private readonly DatabaseRepository<Star> StarRepository;
|
||||
private readonly DatabaseRepository<Node> NodeRepository;
|
||||
private readonly DatabaseRepository<Allocation> AllocationRepository;
|
||||
@@ -29,7 +31,6 @@ public class ServersController : Controller
|
||||
private readonly ServerService ServerService;
|
||||
|
||||
public ServersController(
|
||||
CrudHelper<Server, ServerDetailResponse> crudHelper,
|
||||
DatabaseRepository<Star> starRepository,
|
||||
DatabaseRepository<Node> nodeRepository,
|
||||
DatabaseRepository<Allocation> allocationRepository,
|
||||
@@ -40,7 +41,6 @@ public class ServersController : Controller
|
||||
ServerService serverService
|
||||
)
|
||||
{
|
||||
CrudHelper = crudHelper;
|
||||
StarRepository = starRepository;
|
||||
NodeRepository = nodeRepository;
|
||||
AllocationRepository = allocationRepository;
|
||||
@@ -49,48 +49,68 @@ public class ServersController : Controller
|
||||
UserRepository = userRepository;
|
||||
ServerService = serverService;
|
||||
Logger = logger;
|
||||
|
||||
CrudHelper.QueryModifier = servers => servers
|
||||
.Include(x => x.Node)
|
||||
.Include(x => x.Allocations)
|
||||
.Include(x => x.Variables)
|
||||
.Include(x => x.Star);
|
||||
|
||||
CrudHelper.LateMapper = (server, response) =>
|
||||
{
|
||||
response.NodeId = server.Node.Id;
|
||||
response.StarId = server.Star.Id;
|
||||
response.AllocationIds = server.Allocations.Select(x => x.Id).ToArray();
|
||||
|
||||
return response;
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize(Policy = "permissions:admin.servers.get")]
|
||||
public async Task<IPagedData<ServerDetailResponse>> Get([FromQuery] int page, [FromQuery] int pageSize)
|
||||
[Authorize(Policy = "permissions:admin.servers.read")]
|
||||
public async Task<IPagedData<ServerResponse>> Get(
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize
|
||||
)
|
||||
{
|
||||
return await CrudHelper.Get(page, pageSize);
|
||||
var count = await ServerRepository.Get().CountAsync();
|
||||
|
||||
var items = await ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.Include(x => x.Allocations)
|
||||
.Include(x => x.Variables)
|
||||
.Include(x => x.Star)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedItems = items
|
||||
.Select(ServerMapper.ToAdminServerResponse)
|
||||
.ToArray();
|
||||
|
||||
return new PagedData<ServerResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : count / pageSize
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
[Authorize(Policy = "permissions:admin.servers.get")]
|
||||
public async Task<ServerDetailResponse> GetSingle([FromRoute] int id)
|
||||
[Authorize(Policy = "permissions:admin.servers.read")]
|
||||
public async Task<ServerResponse> GetSingle([FromRoute] int id)
|
||||
{
|
||||
return await CrudHelper.GetSingle(id);
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.Include(x => x.Allocations)
|
||||
.Include(x => x.Variables)
|
||||
.Include(x => x.Star)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with that id found", 404);
|
||||
|
||||
return ServerMapper.ToAdminServerResponse(server);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = "permissions:admin.servers.create")]
|
||||
public async Task<ServerDetailResponse> Create([FromBody] CreateServerRequest request)
|
||||
[Authorize(Policy = "permissions:admin.servers.write")]
|
||||
public async Task<ServerResponse> Create([FromBody] CreateServerRequest request)
|
||||
{
|
||||
// Construct model
|
||||
var server = Mapper.Map<Server>(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);
|
||||
|
||||
// Check if the star exists
|
||||
var star = await StarRepository
|
||||
.Get()
|
||||
.Include(x => x.Variables)
|
||||
@@ -146,6 +166,8 @@ public class ServersController : Controller
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
var server = ServerMapper.ToServer(request);
|
||||
|
||||
// Set allocations
|
||||
server.Allocations = allocations;
|
||||
@@ -181,23 +203,33 @@ public class ServersController : Controller
|
||||
Logger.LogError("Unable to sync server to node the server is assigned to: {e}", e);
|
||||
|
||||
// We are deleting the server from the database after the creation has failed
|
||||
// to ensure we wont have a bugged server in the database which doesnt exist on the node
|
||||
// to ensure we won't have a bugged server in the database which doesnt exist on the node
|
||||
await ServerRepository.Remove(finalServer);
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
return CrudHelper.MapToResult(finalServer);
|
||||
return ServerMapper.ToAdminServerResponse(finalServer);
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
public async Task<ServerDetailResponse> Update([FromRoute] int id, [FromBody] UpdateServerRequest request)
|
||||
[Authorize(Policy = "permissions.admin.servers.write")]
|
||||
public async Task<ServerResponse> Update([FromRoute] int id, [FromBody] UpdateServerRequest request)
|
||||
{
|
||||
//TODO: Handle shrinking virtual disk
|
||||
|
||||
var server = await CrudHelper.GetSingleModel(id);
|
||||
|
||||
server = Mapper.Map(server, request);
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.Include(x => x.Allocations)
|
||||
.Include(x => x.Variables)
|
||||
.Include(x => x.Star)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with that id found", 404);
|
||||
|
||||
server = ServerMapper.Merge(request, server);
|
||||
|
||||
var allocations = new List<Allocation>();
|
||||
|
||||
@@ -250,14 +282,20 @@ public class ServersController : Controller
|
||||
// Notify the node about the changes
|
||||
await ServerService.Sync(server);
|
||||
|
||||
return CrudHelper.MapToResult(server);
|
||||
return ServerMapper.ToAdminServerResponse(server);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task Delete([FromRoute] int id, [FromQuery] bool force = false)
|
||||
{
|
||||
var server = await CrudHelper.GetSingleModel(id);
|
||||
|
||||
var server = await ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with that id found", 404);
|
||||
|
||||
try
|
||||
{
|
||||
// If the sync fails on the node and we aren't forcing the deletion,
|
||||
@@ -277,6 +315,6 @@ public class ServersController : Controller
|
||||
throw;
|
||||
}
|
||||
|
||||
await CrudHelper.Delete(id);
|
||||
await ServerRepository.Remove(server);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
@@ -7,6 +8,7 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Models;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Mappers;
|
||||
using MoonlightServers.Shared.Http.Requests.Admin.StarDockerImages;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.StarDockerImages;
|
||||
|
||||
@@ -16,45 +18,56 @@ namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Stars;
|
||||
[Route("api/admin/servers/stars")]
|
||||
public class StarDockerImagesController : Controller
|
||||
{
|
||||
private readonly CrudHelper<StarDockerImage, StarDockerImageDetailResponse> CrudHelper;
|
||||
private readonly DatabaseRepository<Star> StarRepository;
|
||||
private readonly DatabaseRepository<StarDockerImage> StarDockerImageRepository;
|
||||
|
||||
private Star Star;
|
||||
|
||||
public StarDockerImagesController(
|
||||
CrudHelper<StarDockerImage, StarDockerImageDetailResponse> crudHelper,
|
||||
DatabaseRepository<Star> starRepository,
|
||||
DatabaseRepository<StarDockerImage> starDockerImageRepository
|
||||
)
|
||||
{
|
||||
CrudHelper = crudHelper;
|
||||
StarRepository = starRepository;
|
||||
StarDockerImageRepository = starDockerImageRepository;
|
||||
}
|
||||
|
||||
private async Task ApplyStar(int id)
|
||||
{
|
||||
var star = await StarRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (star == null)
|
||||
throw new HttpApiException("A star with this id could not be found", 404);
|
||||
|
||||
Star = star;
|
||||
|
||||
CrudHelper.QueryModifier = dockerImages =>
|
||||
dockerImages.Where(x => x.Star.Id == star.Id);
|
||||
}
|
||||
|
||||
[HttpGet("{starId:int}/dockerImages")]
|
||||
[Authorize(Policy = "permissions:admin.servers.stars.get")]
|
||||
public async Task<IPagedData<StarDockerImageDetailResponse>> Get([FromRoute] int starId, [FromQuery] int page, [FromQuery] int pageSize)
|
||||
public async Task<IPagedData<StarDockerImageDetailResponse>> Get(
|
||||
[FromRoute] int starId,
|
||||
[FromQuery] [Range(0, int.MaxValue)] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize
|
||||
)
|
||||
{
|
||||
await ApplyStar(starId);
|
||||
var starExists = StarRepository
|
||||
.Get()
|
||||
.Any(x => x.Id == starId);
|
||||
|
||||
return await CrudHelper.Get(page, pageSize);
|
||||
if(starExists)
|
||||
throw new HttpApiException("No star with this id found", 404);
|
||||
|
||||
var query = StarDockerImageRepository
|
||||
.Get()
|
||||
.Where(x => x.Star.Id == starId);
|
||||
|
||||
var count = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedItems = items
|
||||
.Select(DockerImageMapper.ToAdminResponse)
|
||||
.ToArray();
|
||||
|
||||
return new PagedData<StarDockerImageDetailResponse>()
|
||||
{
|
||||
Items = mappedItems,
|
||||
CurrentPage = page,
|
||||
PageSize = pageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : count / pageSize
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{starId:int}/dockerImages/{id:int}")]
|
||||
@@ -62,16 +75,17 @@ public class StarDockerImagesController : Controller
|
||||
public async Task<StarDockerImageDetailResponse> GetSingle([FromRoute] int starId, [FromRoute] int id)
|
||||
{
|
||||
await ApplyStar(starId);
|
||||
|
||||
|
||||
return await CrudHelper.GetSingle(id);
|
||||
}
|
||||
|
||||
[HttpPost("{starId:int}/dockerImages")]
|
||||
[Authorize(Policy = "permissions:admin.servers.stars.create")]
|
||||
public async Task<StarDockerImageDetailResponse> Create([FromRoute] int starId, [FromBody] CreateStarDockerImageRequest request)
|
||||
public async Task<StarDockerImageDetailResponse> Create([FromRoute] int starId,
|
||||
[FromBody] CreateStarDockerImageRequest request)
|
||||
{
|
||||
await ApplyStar(starId);
|
||||
|
||||
|
||||
var starDockerImage = Mapper.Map<StarDockerImage>(request);
|
||||
starDockerImage.Star = Star;
|
||||
|
||||
@@ -86,7 +100,7 @@ public class StarDockerImagesController : Controller
|
||||
[FromBody] UpdateStarDockerImageRequest request)
|
||||
{
|
||||
await ApplyStar(starId);
|
||||
|
||||
|
||||
return await CrudHelper.Update(id, request);
|
||||
}
|
||||
|
||||
@@ -95,7 +109,7 @@ public class StarDockerImagesController : Controller
|
||||
public async Task Delete([FromRoute] int starId, [FromRoute] int id)
|
||||
{
|
||||
await ApplyStar(starId);
|
||||
|
||||
|
||||
await CrudHelper.Delete(id);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Models;
|
||||
|
||||
Reference in New Issue
Block a user