Redesign the admin panel a little bit

This commit is contained in:
jvyden 2022-01-24 13:40:39 -05:00
commit ef84bf1d50
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
6 changed files with 116 additions and 63 deletions

View file

@ -1,7 +1,4 @@
#nullable enable #nullable enable
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace LBPUnion.ProjectLighthouse.Controllers.Website.Admin; namespace LBPUnion.ProjectLighthouse.Controllers.Website.Admin;
@ -16,14 +13,4 @@ public class AdminPanelController : ControllerBase
{ {
this.database = database; this.database = database;
} }
[HttpGet("testWebhook")]
public async Task<IActionResult> TestWebhook()
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null || !user.IsAdmin) return this.NotFound();
await WebhookHelper.SendWebhook("Testing 123", "Someone is testing the Discord webhook from the admin panel.");
return this.Redirect("/admin");
}
} }

View file

@ -0,0 +1,22 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using LBPUnion.ProjectLighthouse.Helpers;
namespace LBPUnion.ProjectLighthouse.Maintenance.Commands;
[UsedImplicitly]
public class TestWebhookCommand : ICommand
{
public async Task Run(string[] args)
{
await WebhookHelper.SendWebhook("Testing 123", "Someone is testing the Discord webhook from the admin panel.");
}
public string Name() => "Test Discord Webhook";
public string[] Aliases()
=> new[]
{
"testWebhook", "testDiscordWebhook",
};
public string Arguments() => "";
public int RequiredArgs() => 0;
}

View file

@ -1,7 +1,8 @@
@page "/admin" @page "/admin"
@using LBPUnion.ProjectLighthouse.Helpers @using LBPUnion.ProjectLighthouse.Helpers
@using LBPUnion.ProjectLighthouse.Helpers.Extensions
@using LBPUnion.ProjectLighthouse.Maintenance @using LBPUnion.ProjectLighthouse.Maintenance
@using LBPUnion.ProjectLighthouse.Types.Settings @using LBPUnion.ProjectLighthouse.Types
@model LBPUnion.ProjectLighthouse.Pages.Admin.AdminPanelPage @model LBPUnion.ProjectLighthouse.Pages.Admin.AdminPanelPage
@{ @{
@ -9,61 +10,63 @@
Model.Title = "Admin Panel"; Model.Title = "Admin Panel";
} }
<a href="/admin/users"> @if (!this.Request.IsMobile())
<div class="ui blue button">
View Users
</div>
</a>
@if (ServerSettings.Instance.DiscordWebhookEnabled)
{ {
<a href="/admin/testWebhook"> <div class="ui center aligned grid">
<div class="ui blue button"> @foreach (AdminPanelStatistic statistic in Model.Statistics)
Test Discord Webhook {
</div> @await Html.PartialAsync("Partials/AdminPanelStatisticPartial", statistic)
</a> }
</div>
<br>
}
else
{
@foreach (AdminPanelStatistic statistic in Model.Statistics)
{
@await Html.PartialAsync("Partials/AdminPanelStatisticPartial", statistic)
<br>
}
} }
<h2>Commands</h2> <h2>Commands</h2>
<div class="ui grid"> @foreach (ICommand command in MaintenanceHelper.Commands)
@foreach (ICommand command in MaintenanceHelper.Commands) {
{ <div class="ui blue segment">
<div class="four wide column"> <h3>@command.Name()</h3>
<div class="ui blue segment"> <form>
<h3>@command.Name()</h3> @if (command.RequiredArgs() > 0)
<form> {
<div class="ui input" style="width: 100%;"> <div class="ui input" style="width: @(Model.Request.IsMobile() ? 100 : 30)%;">
<input type="text" name="args" placeholder="@command.Arguments()"> <input type="text" name="args" placeholder="@command.Arguments()">
</div><br><br> </div>
<input type="text" name="command" style="display: none;" value="@command.FirstAlias"> <br>
<button type="submit" class="ui green button" style="width: 100%;"> <br>
<i class="play icon"></i> }
Execute <input type="text" name="command" style="display: none;" value="@command.FirstAlias">
</button> <button type="submit" class="ui green button">
</form> <i class="play icon"></i>
</div> Execute
</div> </button>
} </form>
</div> </div>
}
<h2>Maintenance Jobs</h2> <h2>Maintenance Jobs</h2>
<p> <p>
<b>Warning: Interrupting Lighthouse during maintenance may leave the database in an unclean state.</b> <b>Warning: Interrupting Lighthouse during maintenance may leave the database in an unclean state.</b>
</p> </p>
<div class="ui grid"> @foreach (IMaintenanceJob job in MaintenanceHelper.MaintenanceJobs)
@foreach (IMaintenanceJob job in MaintenanceHelper.MaintenanceJobs) {
{ <div class="ui red segment">
<div class="four wide column"> <h3>@job.Name()</h3>
<div class="ui red segment"> <p>@job.Description()</p>
<h3>@job.Name()</h3> <form>
<p>@job.Description()</p> <input type="text" name="maintenanceJob" style="display: none;" value="@job.GetType().Name">
<form> <button type="submit" class="ui green button">
<input type="text" name="maintenanceJob" style="display: none;" value="@job.GetType().Name"> <i class="play icon"></i>
<button type="submit" class="ui green button" style="width: 100%;"> Run Job
<i class="play icon"></i> </button>
Execute </form>
</button> </div>
</form> }
</div>
</div>
}
</div>

View file

@ -15,12 +15,18 @@ public class AdminPanelPage : BaseLayout
public AdminPanelPage(Database database) : base(database) public AdminPanelPage(Database database) : base(database)
{} {}
public List<AdminPanelStatistic> Statistics = new();
public async Task<IActionResult> OnGet([FromQuery] string? args, [FromQuery] string? command, [FromQuery] string? maintenanceJob) public async Task<IActionResult> OnGet([FromQuery] string? args, [FromQuery] string? command, [FromQuery] string? maintenanceJob)
{ {
User? user = this.Database.UserFromWebRequest(this.Request); User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login"); if (user == null) return this.Redirect("~/login");
if (!user.IsAdmin) return this.NotFound(); if (!user.IsAdmin) return this.NotFound();
this.Statistics.Add(new AdminPanelStatistic("Users", await StatisticsHelper.UserCount(), "users"));
this.Statistics.Add(new AdminPanelStatistic("Slots", await StatisticsHelper.SlotCount()));
this.Statistics.Add(new AdminPanelStatistic("Photos", await StatisticsHelper.PhotoCount()));
if (!string.IsNullOrEmpty(command)) if (!string.IsNullOrEmpty(command))
{ {
args ??= ""; args ??= "";

View file

@ -0,0 +1,17 @@
@model LBPUnion.ProjectLighthouse.Types.AdminPanelStatistic
<div class="three wide column">
<div class="ui center aligned blue segment">
@if (Model.ViewAllEndpoint != null)
{
<h2>
<a href="/admin/@Model.ViewAllEndpoint">@Model.StatisticNamePlural</a>
</h2>
}
else
{
<h2>@Model.StatisticNamePlural</h2>
}
<h3>@Model.Count</h3>
</div>
</div>

View file

@ -0,0 +1,18 @@
#nullable enable
namespace LBPUnion.ProjectLighthouse.Types;
public struct AdminPanelStatistic
{
public AdminPanelStatistic(string statisticNamePlural, int count, string? viewAllEndpoint = null)
{
this.StatisticNamePlural = statisticNamePlural;
this.Count = count;
this.ViewAllEndpoint = viewAllEndpoint;
}
public readonly string StatisticNamePlural;
public readonly int Count;
public readonly string? ViewAllEndpoint;
}