Completed server variables in server crud

This commit is contained in:
2024-12-23 23:51:01 +01:00
parent 2b697dffb7
commit 4326af2925
8 changed files with 159 additions and 27 deletions

View File

@@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Attributes;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Helpers;
using MoonCore.Models;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.Shared.Http.Responses.Admin.ServerVariables;
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Servers;
[ApiController]
[Route("api/admin/servers")]
public class ServerVariablesController : Controller
{
private readonly DatabaseRepository<ServerVariable> VariableRepository;
private readonly DatabaseRepository<Server> ServerRepository;
public ServerVariablesController(DatabaseRepository<ServerVariable> variableRepository, DatabaseRepository<Server> serverRepository)
{
VariableRepository = variableRepository;
ServerRepository = serverRepository;
}
[HttpGet("{serverId}/variables")]
[RequirePermission("admin.servers.get")]
public async Task<PagedData<ServerVariableDetailResponse>> Get([FromRoute] int serverId, [FromQuery] int page, [FromQuery] int pageSize)
{
var server = await ServerRepository
.Get()
.FirstOrDefaultAsync(x => x.Id == serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
//TODO: Replace with a extension method to use queryable extension for PagedData
var variables = await VariableRepository
.Get()
.Where(x => x.Server.Id == server.Id)
.ToArrayAsync();
var castedVariables = variables
.Select(x => Mapper.Map<ServerVariableDetailResponse>(x))
.ToArray();
return PagedData<ServerVariableDetailResponse>.Create(castedVariables, page, pageSize);
}
}

View File

@@ -146,11 +146,13 @@ public class ServersController : Controller
// Variables
foreach (var variable in star.Variables)
{
var requestVar = request.Variables.FirstOrDefault(x => x.Key == variable.Key);
var serverVar = new ServerVariable()
{
Key = variable.Key,
Value = request.Variables.TryGetValue(variable.Key, out var value)
? value
Value = requestVar != null
? requestVar.Value
: variable.DefaultValue
};
@@ -207,6 +209,22 @@ public class ServersController : Controller
// Set allocations
server.Allocations = allocations;
// Process variables
foreach (var variable in request.Variables)
{
// Search server variable associated to the variable in the request
var serverVar = server.Variables
.FirstOrDefault(x => x.Key == variable.Key);
if(serverVar == null)
continue;
// Update value
serverVar.Value = variable.Value;
}
// TODO: Call node
ServerRepository.Update(server);
return CrudHelper.MapToResult(server);