Completed server variables in server crud
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
@using MoonCore.Helpers
|
||||
@using MoonCore.Blazor.Tailwind.Components
|
||||
@using MoonCore.Models
|
||||
@using MoonlightServers.Shared.Http.Requests.Admin.ServerVariables
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.StarVariables
|
||||
|
||||
@inject HttpApiClient ApiClient
|
||||
@@ -11,8 +12,9 @@
|
||||
@foreach (var variable in StarVariables)
|
||||
{
|
||||
// Load value of default
|
||||
var value = Request.Variables.TryGetValue(variable.Key, out var val)
|
||||
? val
|
||||
var requestVar = Request.Variables.FirstOrDefault(x => x.Key == variable.Key);
|
||||
var value = requestVar != null
|
||||
? requestVar.Value
|
||||
: variable.DefaultValue;
|
||||
|
||||
<div class="sm:col-span-2">
|
||||
@@ -60,10 +62,25 @@
|
||||
var value = args.Value?.ToString() ?? "";
|
||||
|
||||
// Remove variable from request when set to its default value
|
||||
if (value == starVariable.DefaultValue && Request.Variables.ContainsKey(starVariable.Key))
|
||||
Request.Variables.Remove(starVariable.Key);
|
||||
if (value == starVariable.DefaultValue && Request.Variables.Any(x => x.Key == starVariable.Key))
|
||||
Request.Variables.RemoveAll(x => x.Key == starVariable.Key);
|
||||
else
|
||||
Request.Variables[starVariable.Key] = value;
|
||||
{
|
||||
var serverVar = Request.Variables
|
||||
.FirstOrDefault(x => x.Key == starVariable.Key);
|
||||
|
||||
if (serverVar == null)
|
||||
{
|
||||
serverVar = new CreateServerVariableRequest()
|
||||
{
|
||||
Key = starVariable.Key
|
||||
};
|
||||
|
||||
Request.Variables.Add(serverVar);
|
||||
}
|
||||
|
||||
serverVar.Value = value;
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
@@ -2,36 +2,38 @@
|
||||
@using MoonCore.Helpers
|
||||
@using MoonCore.Blazor.Tailwind.Components
|
||||
@using MoonCore.Models
|
||||
@using MoonlightServers.Shared.Http.Requests.Admin.ServerVariables
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.Servers
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.ServerVariables
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.StarVariables
|
||||
|
||||
@inject HttpApiClient ApiClient
|
||||
|
||||
<LazyLoader Load="Load">
|
||||
<div class="grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
|
||||
@foreach (var variable in StarVariables)
|
||||
@foreach (var variable in Variables)
|
||||
{
|
||||
// Load value of default
|
||||
/*
|
||||
var value = Request.Variables.TryGetValue(variable.Key, out var val)
|
||||
? val
|
||||
: variable.DefaultValue;*/
|
||||
var reqVariable = Request.Variables.FirstOrDefault(x => x.Key == variable.Key);
|
||||
var starVariable = StarVariables.FirstOrDefault(x => x.Key == variable.Key);
|
||||
|
||||
var value = "";
|
||||
// Ignore all variables which aren't defined in the star
|
||||
if (starVariable == null)
|
||||
continue;
|
||||
|
||||
var value = reqVariable != null
|
||||
? reqVariable.Value
|
||||
: variable.Value;
|
||||
|
||||
<div class="sm:col-span-2">
|
||||
<label class="block text-sm font-medium leading-6 text-white">
|
||||
@variable.Name
|
||||
@starVariable.Name
|
||||
</label>
|
||||
<div class="mt-2">
|
||||
<input type="text"
|
||||
class="form-input placeholder-gray-500 w-full"
|
||||
value="@value"
|
||||
placeholder="@variable.DefaultValue"
|
||||
<input type="text" class="form-input w-full" value="@value"
|
||||
@onchange="@(args => UpdateValue(variable, args))"/>
|
||||
</div>
|
||||
<p class="mt-1 text-sm leading-6 text-gray-400">
|
||||
@variable.Description
|
||||
@starVariable.Description
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
@@ -44,6 +46,7 @@
|
||||
[Parameter] public ServerDetailResponse Server { get; set; }
|
||||
|
||||
private StarVariableDetailResponse[] StarVariables;
|
||||
private ServerVariableDetailResponse[] Variables;
|
||||
|
||||
private async Task Load(LazyLoader _)
|
||||
{
|
||||
@@ -52,19 +55,39 @@
|
||||
$"api/admin/servers/stars/{Server.StarId}/variables?page={page}&pageSize={pageSize}"
|
||||
)
|
||||
);
|
||||
|
||||
Variables = await PagedData<ServerVariableDetailResponse>.All(async (page, pageSize) =>
|
||||
await ApiClient.GetJson<PagedData<ServerVariableDetailResponse>>(
|
||||
$"api/admin/servers/{Server.Id}/variables?page={page}&pageSize={pageSize}"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private async Task UpdateValue(StarVariableDetailResponse starVariable, ChangeEventArgs args)
|
||||
private async Task UpdateValue(ServerVariableDetailResponse serverVariable, ChangeEventArgs args)
|
||||
{
|
||||
/*
|
||||
var value = args.Value?.ToString() ?? "";
|
||||
|
||||
// Remove variable from request when set to its default value
|
||||
if (value == starVariable.DefaultValue && Request.Variables.ContainsKey(starVariable.Key))
|
||||
Request.Variables.Remove(starVariable.Key);
|
||||
if (value == serverVariable.Value && Request.Variables.Any(x => x.Key == serverVariable.Key))
|
||||
Request.Variables.RemoveAll(x => x.Key == serverVariable.Key);
|
||||
else
|
||||
Request.Variables[starVariable.Key] = value;
|
||||
{
|
||||
var serverVar = Request.Variables
|
||||
.FirstOrDefault(x => x.Key == serverVariable.Key);
|
||||
|
||||
await InvokeAsync(StateHasChanged);*/
|
||||
if (serverVar == null)
|
||||
{
|
||||
serverVar = new UpdateServerVariableRequest()
|
||||
{
|
||||
Key = serverVariable.Key
|
||||
};
|
||||
|
||||
Request.Variables.Add(serverVar);
|
||||
}
|
||||
|
||||
serverVar.Value = value;
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace MoonlightServers.Shared.Http.Requests.Admin.ServerVariables;
|
||||
|
||||
public class CreateServerVariableRequest
|
||||
{
|
||||
[Required(ErrorMessage = "You need to provide a key for the variable")]
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace MoonlightServers.Shared.Http.Requests.Admin.ServerVariables;
|
||||
|
||||
public class UpdateServerVariableRequest
|
||||
{
|
||||
[Required(ErrorMessage = "You need to provide a key for the variable")]
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using MoonlightServers.Shared.Http.Requests.Admin.ServerVariables;
|
||||
|
||||
namespace MoonlightServers.Shared.Http.Requests.Admin.Servers;
|
||||
|
||||
@@ -31,5 +32,5 @@ public class CreateServerRequest
|
||||
public int NodeId { get; set; }
|
||||
|
||||
public int[] AllocationIds { get; set; } = [];
|
||||
public Dictionary<string, string> Variables { get; set; } = new();
|
||||
public List<CreateServerVariableRequest> Variables { get; set; } = new();
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using MoonlightServers.Shared.Http.Requests.Admin.ServerVariables;
|
||||
|
||||
namespace MoonlightServers.Shared.Http.Requests.Admin.Servers;
|
||||
|
||||
@@ -31,4 +32,6 @@ public class UpdateServerRequest
|
||||
//public int NodeId { get; set; }
|
||||
|
||||
public int[] AllocationIds { get; set; } = [];
|
||||
|
||||
public List<UpdateServerVariableRequest> Variables { get; set; } = new();
|
||||
}
|
||||
Reference in New Issue
Block a user