From 680827e0eaa2d6ad7efc07ce85675c605122b3c7 Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Fri, 13 Dec 2024 22:33:29 +0100 Subject: [PATCH] Added node allocations ui and crud controller Multi actions are not done though --- .../Admin/Nodes/NodeAllocationsController.cs | 93 ++++++++++++++ .../Nodes/Modals/CreateAllocationModal.razor | 50 ++++++++ .../Nodes/Modals/UpdateAllocationModal.razor | 50 ++++++++ .../AdvancedNodeUpdate.razor | 26 ++++ .../AllocationsNodeUpdate.razor | 117 ++++++++++++++++++ .../GeneralNodeUpdate.razor | 32 +++++ .../UI/Views/Admin/Nodes/Index.razor | 10 +- .../UI/Views/Admin/Nodes/Update.razor | 22 +++- .../UI/Views/Admin/Stars/Index.razor | 12 +- .../CreateNodeAllocationRequest.cs | 14 +++ .../UpdateNodeAllocationRequest.cs | 14 +++ .../MoonlightServers.Shared.csproj | 4 - 12 files changed, 434 insertions(+), 10 deletions(-) create mode 100644 MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs create mode 100644 MoonlightServers.Frontend/UI/Components/Nodes/Modals/CreateAllocationModal.razor create mode 100644 MoonlightServers.Frontend/UI/Components/Nodes/Modals/UpdateAllocationModal.razor create mode 100644 MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AdvancedNodeUpdate.razor create mode 100644 MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor create mode 100644 MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/GeneralNodeUpdate.razor create mode 100644 MoonlightServers.Shared/Http/Requests/Admin/NodeAllocations/CreateNodeAllocationRequest.cs create mode 100644 MoonlightServers.Shared/Http/Requests/Admin/NodeAllocations/UpdateNodeAllocationRequest.cs diff --git a/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs new file mode 100644 index 0000000..2e5c445 --- /dev/null +++ b/MoonlightServers.ApiServer/Http/Controllers/Admin/Nodes/NodeAllocationsController.cs @@ -0,0 +1,93 @@ +using Microsoft.AspNetCore.Mvc; +using MoonCore.Attributes; +using MoonCore.Exceptions; +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.NodeAllocations; +using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations; + +namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes; + +[ApiController] +[Route("api/admin/servers/nodes")] +public class NodeAllocationsController : Controller +{ + private readonly CrudHelper CrudHelper; + private readonly DatabaseRepository NodeRepository; + private readonly DatabaseRepository AllocationRepository; + + private Node Node; + + public NodeAllocationsController(CrudHelper crudHelper, DatabaseRepository nodeRepository, DatabaseRepository allocationRepository) + { + CrudHelper = crudHelper; + NodeRepository = nodeRepository; + AllocationRepository = allocationRepository; + } + + private void ApplyNode(int id) + { + var node = NodeRepository + .Get() + .FirstOrDefault(x => x.Id == id); + + if (node == null) + throw new HttpApiException("A node with this id could not be found", 404); + + Node = node; + + CrudHelper.QueryModifier = variables => + variables.Where(x => x.Node.Id == node.Id); + } + + [HttpGet("{nodeId:int}/allocations")] + [RequirePermission("admin.servers.nodes.get")] + public async Task> Get([FromRoute] int nodeId, [FromQuery] int page, [FromQuery] int pageSize) + { + ApplyNode(nodeId); + + return await CrudHelper.Get(page, pageSize); + } + + [HttpGet("{nodeId:int}/allocations/{id:int}")] + [RequirePermission("admin.servers.nodes.get")] + public async Task GetSingle([FromRoute] int nodeId, [FromRoute] int id) + { + ApplyNode(nodeId); + + return await CrudHelper.GetSingle(id); + } + + [HttpPost("{nodeId:int}/allocations")] + [RequirePermission("admin.servers.nodes.create")] + public async Task Create([FromRoute] int nodeId, [FromBody] CreateNodeAllocationRequest request) + { + ApplyNode(nodeId); + + var allocation = Mapper.Map(request); + allocation.Node = Node; + + var finalVariable = AllocationRepository.Add(allocation); + + return CrudHelper.MapToResult(finalVariable); + } + + [HttpPatch("{nodeId:int}/allocations/{id:int}")] + public async Task Update([FromRoute] int nodeId, [FromRoute] int id, [FromBody] UpdateNodeAllocationRequest request) + { + ApplyNode(nodeId); + + return await CrudHelper.Update(id, request); + } + + [HttpDelete("{nodeId:int}/allocations/{id:int}")] + public async Task Delete([FromRoute] int nodeId, [FromRoute] int id) + { + ApplyNode(nodeId); + + await CrudHelper.Delete(id); + } +} \ No newline at end of file diff --git a/MoonlightServers.Frontend/UI/Components/Nodes/Modals/CreateAllocationModal.razor b/MoonlightServers.Frontend/UI/Components/Nodes/Modals/CreateAllocationModal.razor new file mode 100644 index 0000000..6fa5ad4 --- /dev/null +++ b/MoonlightServers.Frontend/UI/Components/Nodes/Modals/CreateAllocationModal.razor @@ -0,0 +1,50 @@ +@using MoonCore.Blazor.Tailwind.Modals.Components +@using MoonCore.Blazor.Tailwind.Components +@using MoonlightServers.Shared.Http.Requests.Admin.NodeAllocations + +@inherits BaseModal + +

Add a new allocation

+ + +
+
+ + +
+ +
+ + +
+
+
+ +
+ Cancel + Create +
+ +@code +{ + [Parameter] public Func OnSubmit { get; set; } + + private CreateNodeAllocationRequest Form; + private HandleForm HandleForm; + + protected override void OnInitialized() + { + Form = new() + { + IpAddress = "0.0.0.0" + }; + } + + private async Task OnValidSubmit() + { + await OnSubmit.Invoke(Form); + await Hide(); + } + + private Task Submit() => HandleForm.Submit(); +} diff --git a/MoonlightServers.Frontend/UI/Components/Nodes/Modals/UpdateAllocationModal.razor b/MoonlightServers.Frontend/UI/Components/Nodes/Modals/UpdateAllocationModal.razor new file mode 100644 index 0000000..b34d7b0 --- /dev/null +++ b/MoonlightServers.Frontend/UI/Components/Nodes/Modals/UpdateAllocationModal.razor @@ -0,0 +1,50 @@ +@using MoonCore.Blazor.Tailwind.Modals.Components +@using MoonCore.Blazor.Tailwind.Components +@using MoonCore.Helpers +@using MoonlightServers.Shared.Http.Requests.Admin.NodeAllocations +@using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations + +@inherits BaseModal + +

Update allocation

+ + +
+
+ + +
+ +
+ + +
+
+
+ +
+ Cancel + Update +
+ +@code +{ + [Parameter] public Func OnSubmit { get; set; } + [Parameter] public NodeAllocationDetailResponse Allocation { get; set; } + + private UpdateNodeAllocationRequest Form; + private HandleForm HandleForm; + + protected override void OnInitialized() + { + Form = Mapper.Map(Allocation); + } + + private async Task OnValidSubmit() + { + await OnSubmit.Invoke(Form); + await Hide(); + } + + private Task Submit() => HandleForm.Submit(); +} \ No newline at end of file diff --git a/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AdvancedNodeUpdate.razor b/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AdvancedNodeUpdate.razor new file mode 100644 index 0000000..571296a --- /dev/null +++ b/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AdvancedNodeUpdate.razor @@ -0,0 +1,26 @@ +@using MoonlightServers.Frontend.UI.Components.Forms +@using MoonlightServers.Shared.Http.Requests.Admin.Nodes + +
+
+
+
+ +
+
+
+
+
+
+ +
+
+
+
+ +@code +{ + [Parameter] public UpdateNodeRequest Request { get; set; } +} \ No newline at end of file diff --git a/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor b/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor new file mode 100644 index 0000000..923f0c5 --- /dev/null +++ b/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/AllocationsNodeUpdate.razor @@ -0,0 +1,117 @@ +@using MoonCore.Blazor.Tailwind.Alerts +@using MoonlightServers.Shared.Http.Responses.Admin.Nodes +@using MoonCore.Blazor.Tailwind.DataTable +@using MoonCore.Blazor.Tailwind.Modals +@using MoonCore.Blazor.Tailwind.Toasts +@using MoonCore.Helpers +@using MoonCore.Models +@using MoonlightServers.Frontend.UI.Components.Nodes.Modals +@using MoonlightServers.Shared.Http.Requests.Admin.NodeAllocations +@using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations + +@inject HttpApiClient ApiClient +@inject ModalService ModalService +@inject ToastService ToastService +@inject AlertService AlertService + +
+
+
+
+ Actions +
+
+
+ + + +
+
+
+
+
+ + + + + + + + + +
+
+ +@code +{ + [Parameter] public NodeDetailResponse Node { get; set; } + + private ItemDataTable Table; + + private async Task> Load(int page, int pageSize) + { + return await ApiClient.GetJson>( + $"api/admin/servers/nodes/{Node.Id}/allocations?page={page}&pageSize={pageSize}" + ); + } + + private async Task AddAllocation() + { + Func onSubmit = async request => + { + await ApiClient.Post($"api/admin/servers/nodes/{Node.Id}/allocations", request); + + await ToastService.Success("Successfully created allocation"); + await Table.Refresh(isSilent: false, bypassCache: true); + }; + + await ModalService.Launch(parameters => + { + parameters.Add("OnSubmit", onSubmit); + }); + } + + private async Task UpdateAllocation(NodeAllocationDetailResponse allocation) + { + Func onSubmit = async request => + { + await ApiClient.Patch($"api/admin/servers/nodes/{Node.Id}/allocations/{allocation.Id}", request); + + await ToastService.Success("Successfully updated allocation"); + await Table.Refresh(isSilent: false, bypassCache: true); + }; + + await ModalService.Launch(parameters => + { + parameters.Add("OnSubmit", onSubmit); + parameters.Add("Allocation", allocation); + }); + } + + private async Task DeleteAllocation(NodeAllocationDetailResponse allocation) + { + await AlertService.ConfirmDanger( + "Delete allocation", + "Do you really want to delete the selected allocation? This cannot be undone", + async () => + { + await ApiClient.Delete($"api/admin/servers/nodes/{Node.Id}/allocations/{allocation.Id}"); + + await ToastService.Success("Successfully deleted allocation"); + await Table.Refresh(isSilent: false, bypassCache: true); + } + ); + } +} diff --git a/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/GeneralNodeUpdate.razor b/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/GeneralNodeUpdate.razor new file mode 100644 index 0000000..4878509 --- /dev/null +++ b/MoonlightServers.Frontend/UI/Components/Nodes/UpdateNodePartials/GeneralNodeUpdate.razor @@ -0,0 +1,32 @@ +@using MoonlightServers.Shared.Http.Requests.Admin.Nodes + +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
+ +@code +{ + [Parameter] public UpdateNodeRequest Request { get; set; } +} \ No newline at end of file diff --git a/MoonlightServers.Frontend/UI/Views/Admin/Nodes/Index.razor b/MoonlightServers.Frontend/UI/Views/Admin/Nodes/Index.razor index 47d23f4..5a77a3c 100644 --- a/MoonlightServers.Frontend/UI/Views/Admin/Nodes/Index.razor +++ b/MoonlightServers.Frontend/UI/Views/Admin/Nodes/Index.razor @@ -22,7 +22,15 @@ - + + +