Started refactoring to be compatible with the changed nuget packages and the mooncore changes

This commit is contained in:
2025-07-15 19:21:44 +02:00
parent b53140e633
commit 514f862a9d
71 changed files with 616 additions and 247 deletions

View File

@@ -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,

View File

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