Added node allocation ui

This commit is contained in:
Masu-Baumgartner
2024-09-02 14:42:06 +02:00
parent 45a3a76214
commit 30d912e412
7 changed files with 144 additions and 12 deletions

View File

@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Extended.Abstractions;
using Moonlight.ApiServer.App.Helpers;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.Shared.Http.Requests.Admin.Allocations;
using MoonlightServers.Shared.Http.Responses.Admin.Allocations;
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes;
[ApiController]
[Route("admin/servers/nodes/{rootItem:int}/allocations")]
public class NodeAllocationsController : BaseSubCrudController<Node, Allocation, DetailAllocationResponse, CreateAllocationRequest, DetailAllocationResponse, UpdateAllocationRequest, DetailAllocationResponse>
{
public override Func<Node, List<Allocation>> Property => node => node.Allocations;
public NodeAllocationsController(DatabaseRepository<Allocation> itemRepository, DatabaseRepository<Node> rootItemRepository, IHttpContextAccessor contextAccessor) : base(itemRepository, rootItemRepository, contextAccessor)
{
PermissionPrefix = "admin.servers.nodes.allocations";
}
protected override IEnumerable<Node> IncludeRelations(IQueryable<Node> items)
=> items.Include(x => x.Allocations);
}

View File

@@ -9,7 +9,7 @@ using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.Shared.Http.Requests.Admin.Nodes;
using MoonlightServers.Shared.Http.Responses.Admin.Nodes;
namespace MoonlightServers.ApiServer.Http.Controllers.Admin;
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes;
[ApiController]
[Route("admin/servers/nodes")]
@@ -78,7 +78,4 @@ public class NodesController : BaseCrudController<Node, DetailNodeResponse, Crea
throw new ApiException("The fqdn needs to be either a domain or an ip", statusCode: 400);
}
}
// Allocations
}

View File

@@ -1 +1,65 @@
<h3 class="text-xl2 text-blue-500">Allocation editor</h3>
@using Moonlight.Client.App.Models.Crud
@using Moonlight.Shared.Http.Resources
@using MoonlightServers.Shared.Http.Requests.Admin.Allocations
@using MoonlightServers.Shared.Http.Responses.Admin.Allocations
@inject HttpApiClient HttpApiClient
<div class="rounded-lg border border-slate-700 bg-slate-700">
<SmartCrud TItem="DetailAllocationResponse"
TCreateForm="CreateAllocationRequest"
TUpdateForm="UpdateAllocationRequest"
OnConfigure="OnConfigure">
<View>
<SmartColumn TItem="DetailAllocationResponse" Field="@(x => x.Id)" Title="Id" />
<SmartColumn TItem="DetailAllocationResponse" Field="@(x => x.IpAddress)" Title="IP Address" />
<SmartColumn TItem="DetailAllocationResponse" Field="@(x => x.Port)" Title="Port" />
</View>
</SmartCrud>
</div>
@code
{
[Parameter]
public int NodeId { get; set; }
private void OnConfigure(CrudOptions<DetailAllocationResponse, CreateAllocationRequest, UpdateAllocationRequest> options)
{
options.Loader = async (page, pageSize)
=> await HttpApiClient.GetJson<PagedResponse<DetailAllocationResponse>>($"admin/servers/nodes/{NodeId}/allocations");
options.CreateFunction = async request => await HttpApiClient.Post($"admin/servers/nodes/{NodeId}/allocations", request);
options.UpdateFunction = async (request, item) => await HttpApiClient.Patch($"admin/servers/nodes/{NodeId}/allocations/{item.Id}", request);
options.DeleteFunction = async item => await HttpApiClient.Delete($"admin/servers/nodes/{NodeId}/allocations/{item.Id}");
options.OnConfigureCreate = option =>
{
option
.DefaultPage
.DefaultSection
.AddProperty(x => x.IpAddress)
.WithColumns(6);
option
.DefaultPage
.DefaultSection
.AddProperty(x => x.Port)
.WithColumns(6);
};
options.OnConfigureUpdate = (option, item) =>
{
option
.DefaultPage
.DefaultSection
.AddProperty(x => x.IpAddress)
.WithColumns(6);
option
.DefaultPage
.DefaultSection
.AddProperty(x => x.Port)
.WithColumns(6);
};
}
}

View File

@@ -17,11 +17,25 @@
TCreateForm="CreateNodeRequest"
TUpdateForm="UpdateNodeRequest"
OnConfigure="OnConfigure">
<View>
<View Context="ViewContext">
<SmartColumn TItem="DetailNodeResponse" Field="@(x => x.Id)" Title="Id" />
<SmartColumn TItem="DetailNodeResponse" Field="@(x => x.Name)" Title="Name" />
<SmartColumn TItem="DetailNodeResponse" Field="@(x => x.Name)" Title="Name">
<Template>
<a class="text-blue-500" href="#" @onclick:preventDefault @onclick="() => ViewContext.LaunchDetails(context)">@context.Name</a>
</Template>
</SmartColumn>
<SmartColumn TItem="DetailNodeResponse" Field="@(x => x.Fqdn)" Title="FQDN" />
</View>
<DetailView>
<SmartTabs>
<SmartTab Name="Overview">
<h3 class="text-2xl text-blue-500">Overview owo</h3>
</SmartTab>
<SmartTab Name="Allocations">
<AllocationEditor NodeId="@context.Id" />
</SmartTab>
</SmartTabs>
</DetailView>
</SmartCrud>
</div>
@@ -38,6 +52,7 @@
options.ShowCreateAsModal = false;
options.ShowUpdateAsModal = false;
options.ShowDetailsAsModal = false;
options.OnConfigureCreate = option =>
{
@@ -83,11 +98,6 @@
.DefaultPage
.DefaultSection
.AddProperty(x => x.SslEnabled);
option
.WithPage("Allocations")
.DefaultSection
.AddComponent<AllocationEditor>();
};
}
}

View File

@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
namespace MoonlightServers.Shared.Http.Requests.Admin.Allocations;
public class CreateAllocationRequest
{
[Required(ErrorMessage = "You need to provide an ip address")]
[RegularExpression("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$", ErrorMessage = "You need tp provide a valid ip address")]
public string IpAddress { get; set; } = "0.0.0.0";
[Required(ErrorMessage = "You need to provgide a port")]
[Range(1, 65535 , ErrorMessage = "You need to provide a valid port")]
public int Port { get; set; }
}

View File

@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
namespace MoonlightServers.Shared.Http.Requests.Admin.Allocations;
public class UpdateAllocationRequest
{
[Required(ErrorMessage = "You need to provide an ip address")]
[RegularExpression("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$", ErrorMessage = "You need tp provide a valid ip address")]
public string IpAddress { get; set; } = "0.0.0.0";
[Required(ErrorMessage = "You need to provgide a port")]
[Range(1, 65535 , ErrorMessage = "You need to provide a valid port")]
public int Port { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace MoonlightServers.Shared.Http.Responses.Admin.Allocations;
public class DetailAllocationResponse
{
public int Id { get; set; }
public string IpAddress { get; set; } = "0.0.0.0";
public int Port { get; set; }
}