Files
Moonlight/Moonlight/Shared/Views/Admin/Servers/View/Allocations.razor
2023-06-20 19:18:18 +02:00

85 lines
2.9 KiB
Plaintext

@using Moonlight.App.Database.Entities
@using Moonlight.App.Repositories
@using Microsoft.EntityFrameworkCore
@using Moonlight.App.Services
@using Moonlight.App.Services.Interop
@using BlazorTable
@inject Repository<Server> ServerRepository
@inject Repository<NodeAllocation> NodeAllocationRepository
@inject AlertService AlertService
@inject SmartTranslateService SmartTranslateService
<div class="row">
<div class="col-12 col-md-6 mb-5">
<div class="card card-body">
<WButton Text="@(SmartTranslateService.Translate("Add allocation"))"
WorkingText="@(SmartTranslateService.Translate("Searching"))"
CssClasses="btn-primary"
OnClick="AddAllocation">
</WButton>
</div>
</div>
<div class="col-12 col-md-6">
<div class="card card-body">
<div class="table-responsive">
<table class="table table-bordered">
<tbody>
@foreach (var allocation in Server.Allocations)
{
<tr>
<td class="align-middle fs-5">
@(Server.Node.Fqdn + ":" + allocation.Port)
</td>
<td>
<WButton Text="@(SmartTranslateService.Translate("Delete"))"
WorkingText="@(SmartTranslateService.Translate("Deleting"))"
CssClasses="btn-danger"
OnClick="() => DeleteAllocation(allocation)">
</WButton>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
@code
{
[CascadingParameter]
public Server Server { get; set; }
private async Task AddAllocation()
{
// We have sadly no choice to use entity framework to do what the sql call does, there
// are only slower ways, so we will use a raw sql call as a exception
var freeAllocation = NodeAllocationRepository
.Get()
.FromSqlRaw($"SELECT * FROM `NodeAllocations` WHERE ServerId IS NULL AND NodeId={Server.Node.Id} LIMIT 1")
.FirstOrDefault();
if (freeAllocation == null)
{
await AlertService.Error(
SmartTranslateService.Translate("No free allocation found"));
return;
}
Server.Allocations.Add(freeAllocation);
ServerRepository.Update(Server);
await InvokeAsync(StateHasChanged);
}
private async Task DeleteAllocation(NodeAllocation nodeAllocation)
{
Server.Allocations.Remove(nodeAllocation);
ServerRepository.Update(Server);
await InvokeAsync(StateHasChanged);
}
}