Files
Servers/MoonlightServers.Frontend/UI/Views/Admin/All/Update.razor

102 lines
3.3 KiB
Plaintext

@page "/admin/servers/all/update/{Id:int}"
@using MoonCore.Blazor.FlyonUi.Components
@using MoonCore.Blazor.FlyonUi.Toasts
@using MoonCore.Helpers
@using Moonlight.Shared.Http.Responses.Admin.Users
@using MoonlightServers.Shared.Http.Requests.Admin.Servers
@using MoonlightServers.Shared.Http.Responses.Admin.Servers
@using MoonlightServers.Frontend.UI.Components.Servers.UpdateServerPartials
@using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations
@inject HttpApiClient ApiClient
@inject NavigationManager Navigation
@inject ToastService ToastService
@attribute [Authorize(Policy = "permissions:admin.servers.update")]
<LazyLoader Load="Load">
<PageHeader Title="Update Server">
<a href="/admin/servers/all" class="btn btn-secondary">
<i class="icon-chevron-left mr-1"></i>
Back
</a>
<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">
<Tabs>
<Tab Name="General">
<GeneralServerUpdate Request="Request" Owner="Owner"/>
</Tab>
<Tab Name="Allocations">
<AllocationsServerUpdate Request="Request" Server="Server" Allocations="Allocations"/>
</Tab>
<Tab Name="Variables">
<VariablesServerUpdate Request="Request" Server="Server"/>
</Tab>
<Tab Name="Advanced">
<AdvancedServerUpdate Request="Request"/>
</Tab>
</Tabs>
</HandleForm>
</div>
</LazyLoader>
@code
{
[Parameter] public int Id { get; set; }
private HandleForm Form;
private UpdateServerRequest Request;
private ServerResponse Server;
private List<NodeAllocationResponse> Allocations = new();
private UserResponse Owner;
private async Task Load(LazyLoader _)
{
Server = await ApiClient.GetJson<ServerResponse>($"api/admin/servers/{Id}");
Request = new()
{
Name = Server.Name,
AllocationIds = Server.AllocationIds,
OwnerId = Server.OwnerId,
Bandwidth = Server.Bandwidth,
Cpu = Server.Cpu,
Disk = Server.Disk,
DockerImageIndex = Server.DockerImageIndex,
Memory = Server.Memory,
StartupOverride = Server.StartupOverride
};
foreach (var allocationId in Server.AllocationIds)
{
var allocation = await ApiClient.GetJson<NodeAllocationResponse>(
$"api/admin/servers/nodes/{Server.NodeId}/allocations/{allocationId}"
);
Allocations.Add(allocation);
}
Owner = await ApiClient.GetJson<UserResponse>(
$"api/admin/users/{Server.OwnerId}"
);
}
private async Task OnSubmit()
{
Request.AllocationIds = Allocations.Select(x => x.Id).ToArray();
Request.OwnerId = Owner.Id;
await ApiClient.Patch($"api/admin/servers/{Id}", Request);
await ToastService.Success("Successfully updated server");
Navigation.NavigateTo("/admin/servers/all");
}
}