Added simple node crud
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Helpers;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Models;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
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;
|
||||
|
||||
[ApiController]
|
||||
[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;
|
||||
|
||||
CrudHelper.QueryModifier = nodes =>
|
||||
nodes.Include(x => x.Allocations);
|
||||
|
||||
CrudHelper.LateMapper = (node, response) =>
|
||||
{
|
||||
response.Allocations = node.Allocations
|
||||
.Select(x => Mapper.Map<NodeAllocationDetailResponse>(x))
|
||||
.ToArray();
|
||||
|
||||
return response;
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IPagedData<NodeDetailResponse>> Get([FromQuery] int page, [FromQuery] int pageSize)
|
||||
{
|
||||
return await CrudHelper.Get(page, pageSize);
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<NodeDetailResponse> GetSingle([FromRoute] int id)
|
||||
{
|
||||
return await CrudHelper.GetSingle(id);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<NodeDetailResponse> Create([FromBody] CreateNodeRequest request)
|
||||
{
|
||||
var node = Mapper.Map<Node>(request);
|
||||
|
||||
node.Token = Formatter.GenerateString(32);
|
||||
|
||||
var finalNode = NodeRepository.Add(node);
|
||||
|
||||
return CrudHelper.MapToResult(finalNode);
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
public async Task<NodeDetailResponse> Update([FromRoute] int id, [FromBody] UpdateNodeRequest request)
|
||||
{
|
||||
return await CrudHelper.Update(id, request);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task Delete([FromRoute] int id)
|
||||
{
|
||||
await CrudHelper.Delete(id);
|
||||
}
|
||||
}
|
||||
72
MoonlightServers.Frontend/UI/Views/Admin/Nodes/Create.razor
Normal file
72
MoonlightServers.Frontend/UI/Views/Admin/Nodes/Create.razor
Normal file
@@ -0,0 +1,72 @@
|
||||
@page "/admin/servers/nodes/create"
|
||||
|
||||
@using MoonCore.Blazor.Tailwind.Components
|
||||
@using MoonCore.Blazor.Tailwind.Forms
|
||||
@using MoonCore.Blazor.Tailwind.Toasts
|
||||
@using MoonCore.Helpers
|
||||
@using MoonlightServers.Shared.Http.Requests.Admin.Nodes
|
||||
|
||||
@inject HttpApiClient ApiClient
|
||||
@inject NavigationManager Navigation
|
||||
@inject ToastService ToastService
|
||||
|
||||
<PageHeader Title="Create Node">
|
||||
<button @onclick="GoBack" type="button" class="btn btn-secondary">
|
||||
<i class="icon-chevron-left mr-1"></i>
|
||||
Back
|
||||
</button>
|
||||
<WButton OnClick="_ => Form.Submit()" CssClasses="btn btn-primary">
|
||||
<i class="icon-check mr-1"></i>
|
||||
Create
|
||||
</WButton>
|
||||
</PageHeader>
|
||||
|
||||
<div class="mt-5">
|
||||
<HandleForm @ref="Form" Model="Request" OnValidSubmit="OnSubmit">
|
||||
<GeneratedForm TForm="CreateNodeRequest" Model="Request" OnConfigure="OnConfigure"/>
|
||||
</HandleForm>
|
||||
</div>
|
||||
|
||||
@code
|
||||
|
||||
{
|
||||
private HandleForm Form;
|
||||
private CreateNodeRequest Request;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Request = new();
|
||||
}
|
||||
|
||||
private void OnConfigure(FormConfiguration<CreateNodeRequest> configuration)
|
||||
{
|
||||
configuration.WithField(x => x.Name);
|
||||
configuration.WithField(x => x.Fqdn);
|
||||
|
||||
configuration.WithField(x => x.HttpPort, fieldConfiguration =>
|
||||
{
|
||||
fieldConfiguration.DefaultValue = 8080;
|
||||
});
|
||||
|
||||
configuration.WithField(x => x.FtpPort, fieldConfiguration =>
|
||||
{
|
||||
fieldConfiguration.DefaultValue = 2021;
|
||||
});
|
||||
|
||||
var advancedPage = configuration.WithPage("Advanced");
|
||||
|
||||
advancedPage.WithField(x => x.EnableTransparentMode);
|
||||
advancedPage.WithField(x => x.EnableDynamicFirewall);
|
||||
}
|
||||
|
||||
private async Task OnSubmit()
|
||||
{
|
||||
await ApiClient.Post("api/admin/servers/nodes", Request);
|
||||
|
||||
await ToastService.Success("Successfully created Node");
|
||||
GoBack();
|
||||
}
|
||||
|
||||
private void GoBack()
|
||||
=> Navigation.NavigateTo(ComponentHelper.GetRouteOfComponent<Index>()!);
|
||||
}
|
||||
80
MoonlightServers.Frontend/UI/Views/Admin/Nodes/Index.razor
Normal file
80
MoonlightServers.Frontend/UI/Views/Admin/Nodes/Index.razor
Normal file
@@ -0,0 +1,80 @@
|
||||
@page "/admin/servers/nodes"
|
||||
|
||||
@using MoonCore.Blazor.Tailwind.MinimalCrud
|
||||
@using MoonCore.Helpers
|
||||
@using MoonCore.Models
|
||||
@using MoonCore.Blazor.Tailwind.DataTable
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.Nodes
|
||||
@using MoonCore.Blazor.Tailwind.Components
|
||||
|
||||
@inject HttpApiClient ApiClient
|
||||
|
||||
<div class="mb-3">
|
||||
<NavTabs Index="2" Names="@UiConstants.AdminNavNames" Links="@UiConstants.AdminNavLinks"/>
|
||||
</div>
|
||||
|
||||
<MinimalCrud TItem="NodeDetailResponse" OnConfigure="OnConfigure">
|
||||
<ChildContent>
|
||||
<DataColumn TItem="NodeDetailResponse" Field="@(x => x.Id)" Title="Id" IsSortable="true"/>
|
||||
<DataColumn TItem="NodeDetailResponse" Field="@(x => x.Name)" Title="Name" IsSortable="true"/>
|
||||
<DataColumn TItem="NodeDetailResponse" Field="@(x => x.Fqdn)" Title="Fqdn"/>
|
||||
<DataColumn TItem="NodeDetailResponse" Field="@(x => x.Fqdn)" Title="Status">
|
||||
<Template>
|
||||
<span class="text-success-500">
|
||||
<i class="icon-check text-base me-1 align-middle"></i>
|
||||
<span class="align-middle">Online (v.2.1.0)</span>
|
||||
</span>
|
||||
</Template>
|
||||
</DataColumn>
|
||||
<DataColumn TItem="NodeDetailResponse"
|
||||
Title=""
|
||||
HeadCssClasses="p-2 font-semibold text-left hidden xl:table-cell"
|
||||
CssClasses="p-2 text-left font-normal hidden xl:table-cell">
|
||||
<Template>
|
||||
<div>
|
||||
<i class="icon-cpu text-lg me-1 align-middle text-primary-500"></i>
|
||||
<span class="align-middle">33% of 6 Cores</span>
|
||||
</div>
|
||||
</Template>
|
||||
</DataColumn>
|
||||
<DataColumn TItem="NodeDetailResponse"
|
||||
Title=""
|
||||
HeadCssClasses="p-2 font-semibold text-left hidden xl:table-cell"
|
||||
CssClasses="p-2 text-left font-normal hidden xl:table-cell">
|
||||
<Template>
|
||||
<div>
|
||||
<i class="icon-memory-stick text-lg me-1 align-middle text-primary-500"></i>
|
||||
<span class="align-middle">1.56GB / 64GB</span>
|
||||
</div>
|
||||
</Template>
|
||||
</DataColumn>
|
||||
<DataColumn TItem="NodeDetailResponse"
|
||||
Title=""
|
||||
HeadCssClasses="p-2 font-semibold text-left hidden xl:table-cell"
|
||||
CssClasses="p-2 text-left font-normal hidden xl:table-cell">
|
||||
<Template>
|
||||
<div>
|
||||
<i class="icon-hard-drive text-lg me-1 align-middle text-primary-500"></i>
|
||||
<span class="align-middle">78.68GB / 1TB</span>
|
||||
</div>
|
||||
</Template>
|
||||
</DataColumn>
|
||||
</ChildContent>
|
||||
</MinimalCrud>
|
||||
|
||||
@code
|
||||
|
||||
|
||||
|
||||
{
|
||||
private void OnConfigure(MinimalCrudOptions<NodeDetailResponse> options)
|
||||
{
|
||||
options.Title = "Nodes";
|
||||
options.ItemLoader = async (page, pageSize) =>
|
||||
await ApiClient.GetJson<PagedData<NodeDetailResponse>>($"api/admin/servers/nodes?page={page}&pageSize={pageSize}");
|
||||
|
||||
options.CreateUrl = ComponentHelper.GetRouteOfComponent<Create>();
|
||||
options.UpdateUrl = item => ComponentHelper.GetRouteOfComponent<Update>(item.Id)!;
|
||||
options.DeleteFunction = async item => await ApiClient.Delete($"api/admin/servers/nodes/{item.Id}");
|
||||
}
|
||||
}
|
||||
69
MoonlightServers.Frontend/UI/Views/Admin/Nodes/Update.razor
Normal file
69
MoonlightServers.Frontend/UI/Views/Admin/Nodes/Update.razor
Normal file
@@ -0,0 +1,69 @@
|
||||
@page "/admin/servers/nodes/update/{Id:int}"
|
||||
|
||||
@using MoonCore.Blazor.Tailwind.Components
|
||||
@using MoonCore.Blazor.Tailwind.Forms
|
||||
@using MoonCore.Blazor.Tailwind.Toasts
|
||||
@using MoonCore.Helpers
|
||||
@using MoonlightServers.Shared.Http.Requests.Admin.Nodes
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.Nodes
|
||||
|
||||
@inject HttpApiClient ApiClient
|
||||
@inject NavigationManager Navigation
|
||||
@inject ToastService ToastService
|
||||
|
||||
<LazyLoader Load="Load">
|
||||
<PageHeader Title="Update Node">
|
||||
<button @onclick="GoBack" type="button" class="btn btn-secondary">
|
||||
<i class="icon-chevron-left mr-1"></i>
|
||||
Back
|
||||
</button>
|
||||
<WButton OnClick="_ => Form.Submit()" CssClasses="btn btn-primary">
|
||||
<i class="icon-check mr-1"></i>
|
||||
Update
|
||||
</WButton>
|
||||
</PageHeader>
|
||||
|
||||
<div class="mt-5">
|
||||
<HandleForm @ref="Form" Model="Request" OnValidSubmit="OnSubmit">
|
||||
<GeneratedForm TForm="UpdateNodeRequest" Model="Request" OnConfigure="OnConfigure"/>
|
||||
</HandleForm>
|
||||
</div>
|
||||
</LazyLoader>
|
||||
|
||||
@code
|
||||
{
|
||||
[Parameter] public int Id { get; set; }
|
||||
|
||||
private HandleForm Form;
|
||||
private UpdateNodeRequest Request;
|
||||
|
||||
private async Task Load(LazyLoader _)
|
||||
{
|
||||
var detail = await ApiClient.GetJson<NodeDetailResponse>($"api/admin/servers/nodes/{Id}");
|
||||
Request = Mapper.Map<UpdateNodeRequest>(detail);
|
||||
}
|
||||
|
||||
private void OnConfigure(FormConfiguration<UpdateNodeRequest> configuration)
|
||||
{
|
||||
configuration.WithField(x => x.Name);
|
||||
configuration.WithField(x => x.Fqdn);
|
||||
configuration.WithField(x => x.HttpPort);
|
||||
configuration.WithField(x => x.FtpPort);
|
||||
|
||||
var advancedPage = configuration.WithPage("Advanced");
|
||||
|
||||
advancedPage.WithField(x => x.EnableTransparentMode);
|
||||
advancedPage.WithField(x => x.EnableDynamicFirewall);
|
||||
}
|
||||
|
||||
private async Task OnSubmit()
|
||||
{
|
||||
await ApiClient.Patch($"api/admin/servers/nodes/{Id}", Request);
|
||||
|
||||
await ToastService.Success("Successfully updated Node");
|
||||
GoBack();
|
||||
}
|
||||
|
||||
private void GoBack()
|
||||
=> Navigation.NavigateTo(ComponentHelper.GetRouteOfComponent<Index>()!);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace MoonlightServers.Shared.Http.Requests.Admin.Nodes;
|
||||
|
||||
public class CreateNodeRequest
|
||||
{
|
||||
[Required(ErrorMessage = "You need to provide a name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "You need to provide a fqdn")]
|
||||
public string Fqdn { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "You need to provide a http port")]
|
||||
[Range(1, 65535, ErrorMessage = "You need to provide a valid http port")]
|
||||
public int HttpPort { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "You need to provide a ftp port")]
|
||||
[Range(1, 65535, ErrorMessage = "You need to provide a valid ftp port")]
|
||||
public int FtpPort { get; set; }
|
||||
|
||||
public bool EnableTransparentMode { get; set; }
|
||||
public bool EnableDynamicFirewall { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace MoonlightServers.Shared.Http.Requests.Admin.Nodes;
|
||||
|
||||
public class UpdateNodeRequest
|
||||
{
|
||||
[Required(ErrorMessage = "You need to provide a name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "You need to provide a fqdn")]
|
||||
public string Fqdn { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "You need to provide a http port")]
|
||||
[Range(1, 65535, ErrorMessage = "You need to provide a valid http port")]
|
||||
public int HttpPort { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "You need to provide a ftp port")]
|
||||
[Range(1, 65535, ErrorMessage = "You need to provide a valid ftp port")]
|
||||
public int FtpPort { get; set; }
|
||||
|
||||
public bool EnableTransparentMode { get; set; }
|
||||
public bool EnableDynamicFirewall { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations;
|
||||
|
||||
public class NodeAllocationDetailResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations;
|
||||
|
||||
namespace MoonlightServers.Shared.Http.Responses.Admin.Nodes;
|
||||
|
||||
public class NodeDetailResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
//public List<Server> Servers { get; set; } = new();
|
||||
public NodeAllocationDetailResponse[] Allocations { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Fqdn { get; set; }
|
||||
public string Token { get; set; }
|
||||
public int HttpPort { get; set; }
|
||||
public int FtpPort { get; set; }
|
||||
|
||||
public bool EnableTransparentMode { get; set; }
|
||||
public bool EnableDynamicFirewall { get; set; }
|
||||
}
|
||||
@@ -6,4 +6,8 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Http\Requests\Admin\NodeAllocations\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user