Added missing relations to server db model. Started with server crud. Removed unused relations from detail responses

This commit is contained in:
2024-12-17 22:52:09 +01:00
parent 747712c5c4
commit a34a3ba8b4
20 changed files with 1131 additions and 40 deletions

View File

@@ -8,6 +8,8 @@ public class Server
public Star Star { get; set; }
public Node Node { get; set; }
public List<Allocation> Allocations { get; set; } = new();
public List<ServerVariable> Variables { get; set; } = new();
public List<ServerBackup> Backups { get; set; } = new();
// Meta
public string Name { get; set; }

View File

@@ -0,0 +1,453 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MoonlightServers.ApiServer.Database;
#nullable disable
namespace MoonlightServers.ApiServer.Database.Migrations
{
[DbContext(typeof(ServersDataContext))]
[Migration("20241215151257_AddedServerRelations")]
partial class AddedServerRelations
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasDefaultSchema("Servers")
.HasAnnotation("ProductVersion", "8.0.11")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Allocation", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<string>("IpAddress")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("NodeId")
.HasColumnType("int");
b.Property<int>("Port")
.HasColumnType("int");
b.Property<int?>("ServerId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("NodeId");
b.HasIndex("ServerId");
b.ToTable("Allocations", "Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Node", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<bool>("EnableDynamicFirewall")
.HasColumnType("tinyint(1)");
b.Property<bool>("EnableTransparentMode")
.HasColumnType("tinyint(1)");
b.Property<string>("Fqdn")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("FtpPort")
.HasColumnType("int");
b.Property<int>("HttpPort")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Token")
.IsRequired()
.HasColumnType("longtext");
b.Property<bool>("UseSsl")
.HasColumnType("tinyint(1)");
b.HasKey("Id");
b.ToTable("Nodes", "Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Server", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Bandwidth")
.HasColumnType("int");
b.Property<int>("Cpu")
.HasColumnType("int");
b.Property<int>("Disk")
.HasColumnType("int");
b.Property<int>("DockerImageIndex")
.HasColumnType("int");
b.Property<int>("Memory")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("NodeId")
.HasColumnType("int");
b.Property<int>("OwnerId")
.HasColumnType("int");
b.Property<int>("StarId")
.HasColumnType("int");
b.Property<string>("StartupOverride")
.HasColumnType("longtext");
b.Property<bool>("UseVirtualDisk")
.HasColumnType("tinyint(1)");
b.HasKey("Id");
b.HasIndex("NodeId");
b.HasIndex("StarId");
b.ToTable("Servers", "Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerBackup", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<bool>("Completed")
.HasColumnType("tinyint(1)");
b.Property<DateTime>("CompletedAt")
.HasColumnType("datetime(6)");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<int?>("ServerId")
.HasColumnType("int");
b.Property<long>("Size")
.HasColumnType("bigint");
b.Property<bool>("Successful")
.HasColumnType("tinyint(1)");
b.HasKey("Id");
b.HasIndex("ServerId");
b.ToTable("ServerBackups", "Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerVariable", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Key")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("ServerId")
.HasColumnType("int");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.HasIndex("ServerId");
b.ToTable("ServerVariables", "Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Star", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<bool>("AllowDockerImageChange")
.HasColumnType("tinyint(1)");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("DefaultDockerImage")
.HasColumnType("int");
b.Property<string>("DonateUrl")
.HasColumnType("longtext");
b.Property<string>("InstallDockerImage")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("InstallScript")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("InstallShell")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("OnlineDetection")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("ParseConfiguration")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("RequiredAllocations")
.HasColumnType("int");
b.Property<string>("StartupCommand")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("StopCommand")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("UpdateUrl")
.HasColumnType("longtext");
b.Property<string>("Version")
.IsRequired()
.HasColumnType("longtext");
b.HasKey("Id");
b.ToTable("Stars", "Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.StarDockerImage", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<bool>("AutoPulling")
.HasColumnType("tinyint(1)");
b.Property<string>("DisplayName")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Identifier")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("StarId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("StarId");
b.ToTable("StarDockerImages", "Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.StarVariable", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<bool>("AllowEditing")
.HasColumnType("tinyint(1)");
b.Property<bool>("AllowViewing")
.HasColumnType("tinyint(1)");
b.Property<string>("DefaultValue")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Filter")
.HasColumnType("longtext");
b.Property<string>("Key")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("StarId")
.HasColumnType("int");
b.Property<int>("Type")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("StarId");
b.ToTable("StarVariables", "Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Allocation", b =>
{
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Node", "Node")
.WithMany("Allocations")
.HasForeignKey("NodeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Server", "Server")
.WithMany("Allocations")
.HasForeignKey("ServerId");
b.Navigation("Node");
b.Navigation("Server");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Server", b =>
{
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Node", "Node")
.WithMany("Servers")
.HasForeignKey("NodeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Star", "Star")
.WithMany()
.HasForeignKey("StarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Node");
b.Navigation("Star");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerBackup", b =>
{
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Server", null)
.WithMany("Backups")
.HasForeignKey("ServerId");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerVariable", b =>
{
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Server", "Server")
.WithMany("Variables")
.HasForeignKey("ServerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Server");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.StarDockerImage", b =>
{
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Star", "Star")
.WithMany("DockerImages")
.HasForeignKey("StarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Star");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.StarVariable", b =>
{
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Star", "Star")
.WithMany("Variables")
.HasForeignKey("StarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Star");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Node", b =>
{
b.Navigation("Allocations");
b.Navigation("Servers");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Server", b =>
{
b.Navigation("Allocations");
b.Navigation("Backups");
b.Navigation("Variables");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Star", b =>
{
b.Navigation("DockerImages");
b.Navigation("Variables");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,55 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MoonlightServers.ApiServer.Database.Migrations
{
/// <inheritdoc />
public partial class AddedServerRelations : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "ServerId",
schema: "Servers",
table: "ServerBackups",
type: "int",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_ServerBackups_ServerId",
schema: "Servers",
table: "ServerBackups",
column: "ServerId");
migrationBuilder.AddForeignKey(
name: "FK_ServerBackups_Servers_ServerId",
schema: "Servers",
table: "ServerBackups",
column: "ServerId",
principalSchema: "Servers",
principalTable: "Servers",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ServerBackups_Servers_ServerId",
schema: "Servers",
table: "ServerBackups");
migrationBuilder.DropIndex(
name: "IX_ServerBackups_ServerId",
schema: "Servers",
table: "ServerBackups");
migrationBuilder.DropColumn(
name: "ServerId",
schema: "Servers",
table: "ServerBackups");
}
}
}

View File

@@ -161,6 +161,9 @@ namespace MoonlightServers.ApiServer.Database.Migrations
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<int?>("ServerId")
.HasColumnType("int");
b.Property<long>("Size")
.HasColumnType("bigint");
@@ -169,6 +172,8 @@ namespace MoonlightServers.ApiServer.Database.Migrations
b.HasKey("Id");
b.HasIndex("ServerId");
b.ToTable("ServerBackups", "Servers");
});
@@ -377,10 +382,17 @@ namespace MoonlightServers.ApiServer.Database.Migrations
b.Navigation("Star");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerBackup", b =>
{
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Server", null)
.WithMany("Backups")
.HasForeignKey("ServerId");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerVariable", b =>
{
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Server", "Server")
.WithMany()
.WithMany("Variables")
.HasForeignKey("ServerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@@ -420,6 +432,10 @@ namespace MoonlightServers.ApiServer.Database.Migrations
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Server", b =>
{
b.Navigation("Allocations");
b.Navigation("Backups");
b.Navigation("Variables");
});
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Star", b =>

View File

@@ -25,18 +25,6 @@ public class NodesController : Controller
{
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]

View File

@@ -0,0 +1,174 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
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.Servers;
using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations;
using MoonlightServers.Shared.Http.Responses.Admin.Servers;
using MoonlightServers.Shared.Http.Responses.Admin.ServerVariables;
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Servers;
[ApiController]
[Route("api/admin/servers")]
public class ServersController : Controller
{
private readonly CrudHelper<Server, ServerDetailResponse> CrudHelper;
private readonly DatabaseRepository<Star> StarRepository;
private readonly DatabaseRepository<Node> NodeRepository;
private readonly DatabaseRepository<Allocation> AllocationRepository;
private readonly DatabaseRepository<ServerVariable> VariableRepository;
private readonly DatabaseRepository<Server> ServerRepository;
public ServersController(
CrudHelper<Server, ServerDetailResponse> crudHelper,
DatabaseRepository<Star> starRepository,
DatabaseRepository<Node> nodeRepository,
DatabaseRepository<Allocation> allocationRepository,
DatabaseRepository<ServerVariable> variableRepository,
DatabaseRepository<Server> serverRepository
)
{
CrudHelper = crudHelper;
StarRepository = starRepository;
NodeRepository = nodeRepository;
AllocationRepository = allocationRepository;
VariableRepository = variableRepository;
ServerRepository = serverRepository;
CrudHelper.QueryModifier = servers => servers
.Include(x => x.Node)
.Include(x => x.Allocations)
.Include(x => x.Variables)
.Include(x => x.Star);
CrudHelper.LateMapper = (server, response) =>
{
response.NodeId = server.Node.Id;
response.StarId = server.Star.Id;
return response;
};
}
[HttpGet]
[RequirePermission("admin.servers.get")]
public async Task<IPagedData<ServerDetailResponse>> Get([FromQuery] int page, [FromQuery] int pageSize)
{
return await CrudHelper.Get(page, pageSize);
}
[HttpGet("{id:int}")]
[RequirePermission("admin.servers.get")]
public async Task<ServerDetailResponse> GetSingle([FromRoute] int id)
{
return await CrudHelper.GetSingle(id);
}
[HttpPost]
[RequirePermission("admin.servers.create")]
public async Task<ServerDetailResponse> Create([FromBody] CreateServerRequest request)
{
// Construct model
var server = Mapper.Map<Server>(request);
var star = StarRepository
.Get()
.Include(x => x.Variables)
.Include(x => x.DockerImages)
.FirstOrDefault(x => x.Id == request.StarId);
if (star == null)
throw new HttpApiException("No star with this id found", 400);
var node = NodeRepository
.Get()
.FirstOrDefault(x => x.Id == request.NodeId);
if (node == null)
throw new HttpApiException("No node with this id found", 400);
var allocations = new List<Allocation>();
// Fetch specified allocations from the request
foreach (var allocationId in request.AllocationIds)
{
var allocation = AllocationRepository
.Get()
.Where(x => x.Server == null)
.Where(x => x.Node.Id == node.Id)
.FirstOrDefault(x => x.Id == allocationId);
if (allocation == null)
continue;
allocations.Add(allocation);
}
// Check if the specified allocations are enough for the star
if (allocations.Count < star.RequiredAllocations)
{
var amountRequiredToSatisfy = star.RequiredAllocations - allocations.Count;
var freeAllocations = AllocationRepository
.Get()
.Where(x => x.Server == null)
.Where(x => x.Node.Id == node.Id)
.Take(amountRequiredToSatisfy);
allocations.AddRange(freeAllocations);
if (allocations.Count < star.RequiredAllocations)
{
throw new HttpApiException(
$"Unable to find enough free allocations. Found: {allocations.Count}, Required: {star.RequiredAllocations}",
400
);
}
}
// Set allocations
server.Allocations = allocations;
// Variables
foreach (var variable in star.Variables)
{
var serverVar = new ServerVariable()
{
Key = variable.Key,
Value = request.Variables.TryGetValue(variable.Key, out var value)
? value
: variable.DefaultValue
};
server.Variables.Add(serverVar);
}
// Set relations
server.Node = node;
server.Star = star;
// TODO: Call node
var finalServer = ServerRepository.Add(server);
return CrudHelper.MapToResult(finalServer);
}
[HttpPatch("{id:int}")]
public async Task<ServerDetailResponse> Update([FromRoute] int id, [FromBody] UpdateServerRequest request)
{
return await CrudHelper.Update(id, request);
}
[HttpDelete("{id:int}")]
public async Task Delete([FromRoute] int id)
{
await CrudHelper.Delete(id);
}
}

View File

@@ -24,23 +24,6 @@ public class StarsController : Controller
{
CrudHelper = crudHelper;
StarRepository = starRepository;
CrudHelper.QueryModifier = stars => stars
.Include(x => x.Variables)
.Include(x => x.DockerImages);
CrudHelper.LateMapper = (star, response) =>
{
response.DockerImages = star.DockerImages
.Select(x => Mapper.Map<StarDockerImageDetailResponse>(x))
.ToArray();
response.Variables = star.Variables
.Select(x => Mapper.Map<StarVariableDetailResponse>(x))
.ToArray();
return response;
};
}
[HttpGet]

View File

@@ -20,7 +20,6 @@
<ItemGroup>
<Folder Include="Helpers\"/>
<Folder Include="Interfaces\"/>
<Folder Include="UI\Views\Admin\All\" />
<Folder Include="wwwroot\"/>
</ItemGroup>

View File

@@ -23,8 +23,8 @@
</div>
<div class="card-body">
<div class="flex flex-col gap-y-3">
<button @onclick="AddAllocation" class="btn btn-primary">Create allocation</button>
<button @onclick="AddAllocationRange" class="btn btn-tertiary">Add multiple</button>
<button @onclick="AddAllocation" class="btn btn-primary">Create</button>
<button @onclick="AddAllocationRange" class="btn btn-tertiary">Create multiple</button>
<button @onclick="DeleteAllAllocations" class="btn btn-danger">Delete all</button>
</div>
</div>

View File

@@ -0,0 +1,23 @@
@using MoonlightServers.Shared.Http.Requests.Admin.Servers
@using MoonlightServers.Frontend.UI.Components.Forms
<div class="grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div class="sm:col-span-2">
<label class="block text-sm font-medium leading-6 text-white">Use Virtual Disk</label>
<div class="mt-2">
<Switch @bind-Value="Request.UseVirtualDisk"/>
</div>
</div>
<div class="sm:col-span-2">
<label class="block text-sm font-medium leading-6 text-white">Bandwidth</label>
<div class="mt-2">
<input @bind="Request.Bandwidth" type="number" autocomplete="off" class="form-input w-full">
</div>
</div>
</div>
@code
{
[Parameter] public CreateServerRequest Request { get; set; }
}

View File

@@ -0,0 +1,89 @@
@using MoonlightServers.Shared.Http.Requests.Admin.Servers
@using MoonlightServers.Frontend.UI.Components.Forms
@using MoonCore.Helpers
@using MoonCore.Models
@using MoonlightServers.Shared.Http.Responses.Admin.Nodes
@using MoonlightServers.Shared.Http.Responses.Admin.Stars
@using MoonCore.Blazor.Tailwind.Inputs
@inject HttpApiClient ApiClient
<div class="grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div class="sm:col-span-2">
<label class="block text-sm font-medium leading-6 text-white">Name</label>
<div class="mt-2">
<input @bind="Request.Name" type="text" autocomplete="off" class="form-input w-full">
</div>
</div>
<div class="sm:col-span-2">
<label class="block text-sm font-medium leading-6 text-white">Star</label>
<div class="mt-2">
<InputItemSelect TItem="StarDetailResponse"
TProperty="int"
@bind-Value="Request.StarId"
DisplayProperty="@(x => x.Name)"
ValueProperty="@(x => x?.Id ?? -1)"
Loader="LoadStars"/>
</div>
</div>
<div class="sm:col-span-2">
<label class="block text-sm font-medium leading-6 text-white">Node</label>
<div class="mt-2">
<InputItemSelect TItem="NodeDetailResponse"
TProperty="int"
@bind-Value="Request.NodeId"
DisplayProperty="@(x => x.Name)"
ValueProperty="@(x => x?.Id ?? -1)"
Loader="LoadNodes"/>
</div>
</div>
</div>
<div class="border-t border-gray-100/10 pt-6 my-8">
<div class="mb-8"><h2 class="text-base font-semibold leading-7 text-gray-100">
Resources
</h2>
<p class="mt-1 text-sm leading-6 text-gray-400">Define the servers resource limit</p></div>
<div class="grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div class="sm:col-span-2">
<label class="block text-sm font-medium leading-6 text-white">Cpu</label>
<div class="mt-2">
<input @bind="Request.Cpu" type="number" autocomplete="off" class="form-input w-full">
</div>
</div>
<div class="sm:col-span-2">
<label class="block text-sm font-medium leading-6 text-white">Memory</label>
<div class="mt-2">
<input @bind="Request.Memory" type="number" autocomplete="off" class="form-input w-full">
</div>
</div>
<div class="sm:col-span-2">
<label class="block text-sm font-medium leading-6 text-white">Disk</label>
<div class="mt-2">
<input @bind="Request.Disk" type="number" autocomplete="off" class="form-input w-full">
</div>
</div>
</div>
</div>
@code
{
[Parameter] public CreateServerRequest Request { get; set; }
private async Task<StarDetailResponse[]> LoadStars()
{
var starData = await ApiClient.GetJson<PagedData<StarDetailResponse>>("api/admin/servers/stars?page=0&pageSize=50");
return starData.Items;
}
private async Task<NodeDetailResponse[]> LoadNodes()
{
var nodeData = await ApiClient.GetJson<PagedData<NodeDetailResponse>>("api/admin/servers/nodes?page=0&pageSize=50");
return nodeData.Items;
}
}

View File

@@ -0,0 +1,58 @@
@page "/admin/servers/all/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.Servers
@using MoonlightServers.Frontend.UI.Components.Servers.CreateServerPartials
@inject HttpApiClient ApiClient
@inject NavigationManager Navigation
@inject ToastService ToastService
<PageHeader Title="Create Server">
<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">
<Tabs>
<Tab Name="General">
<GeneralServerCreate Request="Request" />
</Tab>
<Tab Name="Advanced">
<AdvancedServerCreate Request="Request" />
</Tab>
</Tabs>
</HandleForm>
</div>
@code
{
private HandleForm Form;
private CreateServerRequest Request;
protected override void OnInitialized()
{
Request = new();
}
private async Task OnSubmit()
{
await ApiClient.Post("api/admin/servers", Request);
await ToastService.Success("Successfully created Server");
GoBack();
}
private void GoBack()
=> Navigation.NavigateTo(ComponentHelper.GetRouteOfComponent<Index>()!);
}

View File

@@ -0,0 +1,94 @@
@page "/admin/servers/all"
@using MoonCore.Blazor.Tailwind.MinimalCrud
@using MoonCore.Blazor.Tailwind.DataTable
@using MoonCore.Helpers
@using MoonCore.Models
@using MoonlightServers.Shared.Http.Responses.Admin.Nodes
@using MoonlightServers.Shared.Http.Responses.Admin.Servers
@using MoonlightServers.Shared.Http.Responses.Admin.Stars
@using MoonCore.Blazor.Tailwind.Components
@inject HttpApiClient ApiClient
<div class="mb-3">
<NavTabs Index="1" Names="@UiConstants.AdminNavNames" Links="@UiConstants.AdminNavLinks"/>
</div>
<MinimalCrud TItem="ServerDetailResponse" OnConfigure="OnConfigure">
<ChildContent>
<DataColumn TItem="ServerDetailResponse" Field="@(x => x.Id)" Title="Id" IsSortable="true"/>
<DataColumn TItem="ServerDetailResponse" Field="@(x => x.Name)" Title="Name"/>
<DataColumn TItem="ServerDetailResponse" Field="@(x => x.NodeId)" Title="Node">
<Template>
@{
var node = Nodes.FirstOrDefault(x => x.Id == context.NodeId);
}
@if (node != null)
{
<span>@node.Name</span>
}
else
{
<span>N/A</span>
}
</Template>
</DataColumn>
<DataColumn TItem="ServerDetailResponse" Field="@(x => x.StarId)" Title="Star">
<Template>
@{
var star = Stars.FirstOrDefault(x => x.Id == context.StarId);
}
@if (star != null)
{
<span>@star.Name</span>
}
else
{
<span>N/A</span>
}
</Template>
</DataColumn>
</ChildContent>
</MinimalCrud>
@code
{
private List<StarDetailResponse> Stars = new();
private List<NodeDetailResponse> Nodes = new();
private void OnConfigure(MinimalCrudOptions<ServerDetailResponse> options)
{
options.Title = "Servers";
options.ItemLoader = LoadItems;
options.CreateUrl = ComponentHelper.GetRouteOfComponent<Create>();
options.UpdateUrl = item => ComponentHelper.GetRouteOfComponent<Update>(item.Id)!;
options.DeleteFunction = async item => await ApiClient.Delete($"api/admin/servers/{item.Id}");
}
private async Task<IPagedData<ServerDetailResponse>> LoadItems(int page, int pageSize)
{
// Clear potential previous data
var data = await ApiClient.GetJson<PagedData<ServerDetailResponse>>($"api/admin/servers?page={page}&pageSize={pageSize}");
foreach (var item in data.Items)
{
if (Nodes.All(x => x.Id != item.NodeId))
{
var node = await ApiClient.GetJson<NodeDetailResponse>($"api/admin/servers/nodes/{item.NodeId}");
Nodes.Add(node);
}
if (Stars.All(x => x.Id != item.StarId))
{
var star = await ApiClient.GetJson<StarDetailResponse>($"api/admin/servers/stars/{item.StarId}");
Stars.Add(star);
}
}
return data;
}
}

View File

@@ -0,0 +1,61 @@
@page "/admin/servers/all/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.Servers
@using MoonlightServers.Shared.Http.Responses.Admin.Servers
@inject HttpApiClient ApiClient
@inject NavigationManager Navigation
@inject ToastService ToastService
<LazyLoader Load="Load">
<PageHeader Title="Update Server">
<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="UpdateServerRequest" Model="Request" OnConfigure="OnConfigure"/>
</HandleForm>
</div>
</LazyLoader>
@code
{
[Parameter] public int Id { get; set; }
private HandleForm Form;
private UpdateServerRequest Request;
private async Task Load(LazyLoader _)
{
var detail = await ApiClient.GetJson<ServerDetailResponse>($"api/admin/servers/{Id}");
Request = Mapper.Map<UpdateServerRequest>(detail);
}
private void OnConfigure(FormConfiguration<UpdateServerRequest> configuration)
{
}
private async Task OnSubmit()
{
await ApiClient.Patch($"api/admin/servers/{Id}", Request);
await ToastService.Success("Successfully updated Server");
GoBack();
}
private void GoBack()
=> Navigation.NavigateTo(ComponentHelper.GetRouteOfComponent<Index>()!);
}

View File

@@ -0,0 +1,35 @@
using System.ComponentModel.DataAnnotations;
namespace MoonlightServers.Shared.Http.Requests.Admin.Servers;
public class CreateServerRequest
{
[Required(ErrorMessage = "You need to provide a name for the server")]
public string Name { get; set; }
public int OwnerId { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "You need to provide a valid cpu percent amount")]
public int Cpu { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "You need to provide a valid memory amount")]
public int Memory { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "You need to provide a valid disk amount")]
public int Disk { get; set; }
public bool UseVirtualDisk { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "You need to provide a valid bandwidth amount")]
public int Bandwidth { get; set; }
public string? StartupOverride { get; set; }
public int DockerImageIndex { get; set; }
public int StarId { get; set; }
public int NodeId { get; set; }
public int[] AllocationIds { get; set; } = [];
public Dictionary<string, string> Variables { get; set; } = new();
}

View File

@@ -0,0 +1,35 @@
using System.ComponentModel.DataAnnotations;
namespace MoonlightServers.Shared.Http.Requests.Admin.Servers;
public class UpdateServerRequest
{
[Required(ErrorMessage = "You need to provide a name for the server")]
public string Name { get; set; }
public int OwnerId { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "You need to provide a valid cpu percent amount")]
public int Cpu { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "You need to provide a valid memory amount")]
public int Memory { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "You need to provide a valid disk amount")]
public int Disk { get; set; }
public bool UseVirtualDisk { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "You need to provide a valid bandwidth amount")]
public int Bandwidth { get; set; }
public string? StartupOverride { get; set; }
public int DockerImageIndex { get; set; }
public int StarId { get; set; }
public int NodeId { get; set; }
public int[] AllocationIds { get; set; } = [];
public Dictionary<string, string> Variables { get; set; } = new();
}

View File

@@ -6,9 +6,6 @@ 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; }

View File

@@ -0,0 +1,8 @@
namespace MoonlightServers.Shared.Http.Responses.Admin.ServerVariables;
public class ServerVariableDetailResponse
{
public int Id { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}

View File

@@ -0,0 +1,25 @@
using MoonlightServers.Shared.Http.Responses.Admin.NodeAllocations;
using MoonlightServers.Shared.Http.Responses.Admin.ServerVariables;
namespace MoonlightServers.Shared.Http.Responses.Admin.Servers;
public class ServerDetailResponse
{
public int Id { get; set; }
public string Name { get; set; }
public int OwnerId { get; set; }
public int Cpu { get; set; }
public int Memory { get; set; }
public int Disk { get; set; }
public bool UseVirtualDisk { get; set; }
public int Bandwidth { get; set; }
public string? StartupOverride { get; set; }
public int DockerImageIndex { get; set; }
public int StarId { get; set; }
public int NodeId { get; set; }
}

View File

@@ -7,10 +7,6 @@ public class StarDetailResponse
{
public int Id { get; set; }
// References
public StarVariableDetailResponse[] Variables { get; set; }
public StarDockerImageDetailResponse[] DockerImages { get; set; }
// Meta
public string Name { get; set; }
public string Version { get; set; }