Open up new case page for moderators

This commit is contained in:
jvyden 2022-08-05 20:55:47 -04:00
commit d540a54cd8
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
6 changed files with 94 additions and 69 deletions

View file

@ -0,0 +1,26 @@
@page "/moderation/newCase"
@using LBPUnion.ProjectLighthouse.Administration
@model LBPUnion.ProjectLighthouse.Servers.Website.Pages.Admin.NewCasePage
@{
Layout = "Layouts/BaseLayout";
Model.Title = $"New {Model.Type.ToString()} Case";
}
<form method="post">
@Html.AntiForgeryToken()
<input type="hidden" name="type" value="@((int)Model.Type)"/>
<input type="hidden" name="affectedId" value="@Model.AffectedId"/>
<label for="reason">Reason: </label>
<input type="text" name="reason" id="reason"/><br/>
<label for="mod-notes">Mod Notes: </label>
<input type="text" name="modNotes" id="mod-notes"/><br/>
<label for="expires">Expires: </label>
<input type="datetime-local" name="expires" id="expires"/><br/>
<br/><input type="submit"/>
</form>

View file

@ -0,0 +1,56 @@
using LBPUnion.ProjectLighthouse.Administration;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using Microsoft.AspNetCore.Mvc;
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Admin;
public class NewCasePage : BaseLayout
{
public NewCasePage(Database database) : base(database)
{}
public CaseType Type { get; set; }
public int AffectedId { get; set; }
public IActionResult OnGet([FromQuery] CaseType? type)
{
User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null || !user.IsModerator) return this.Redirect("/login");
if (type == null) return this.BadRequest();
this.Type = (CaseType)type;
return this.Page();
}
public async Task<IActionResult> OnPost(CaseType? type, string reason, string modNotes, DateTime expires, int affectedId)
{
User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null || !user.IsModerator) return this.Redirect("/login");
if (type == null) return this.BadRequest();
// this is fucking ugly and so is the person who wrote it
// if id is invalid then return bad request
if (!(await ((CaseType)type).IsIdValid(affectedId, this.Database))) return this.BadRequest();
ModerationCase @case = new()
{
Type = (CaseType)type,
Reason = reason,
ModeratorNotes = modNotes,
ExpiresAt = expires,
CreatedAt = DateTime.Now,
CreatorId = user.UserId,
AffectedId = affectedId,
};
this.Database.Cases.Add(@case);
await this.Database.SaveChangesAsync();
return this.Redirect("/moderation/cases/0");
}
}

View file

@ -14,7 +14,6 @@
<input type="text" autocomplete="off" name="name" placeholder="Search cases..." value="@Model.SearchValue">
<i class="search icon"></i>
</div>
<a class="ui red button" href="/debug/newCase">DEBUG: Create Case</a>
</form>
<div class="ui divider"></div>

View file

@ -1,30 +0,0 @@
@page "/debug/newCase"
@using LBPUnion.ProjectLighthouse.Administration
@model LBPUnion.ProjectLighthouse.Servers.Website.Pages.Debug.NewCasePage
@{
Layout = "Layouts/BaseLayout";
Model.Title = "DEBUG - New Case";
}
<form method="post">
@Html.AntiForgeryToken()
<label for="type">Case Type: </label>
<select name="type" id="type">
@foreach (CaseType type in Enum.GetValues<CaseType>())
{
<option value="@((int)type)">@type.ToString()</option>
}
</select><br/>
<label for="description">Description: </label>
<input type="text" name="description" id="description"/><br/>
<label for="expires">Expires: </label>
<input type="datetime-local" name="expires" id="expires"/><br/>
<label for="affected-id">Affected ID: </label>
<input type="number" name="affectedId" id="affected-id"/><br/>
<br/><input type="submit"/>
</form>

View file

@ -1,38 +0,0 @@
using System.Diagnostics;
using LBPUnion.ProjectLighthouse.Administration;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Debug;
public class NewCasePage : BaseLayout
{
public NewCasePage(Database database) : base(database)
{}
public IActionResult OnGet() => this.Page();
public async Task<IActionResult> OnPost(
int type,
string description,
DateTime expires,
int affectedId
)
{
ModerationCase @case = new()
{
Type = (CaseType)type,
Reason = description,
ExpiresAt = expires,
CreatedAt = DateTime.Now,
CreatorId = 1,
AffectedId = affectedId,
};
this.Database.Cases.Add(@case);
await this.Database.SaveChangesAsync();
return this.Redirect("/moderation/cases/0");
}
}

View file

@ -1,3 +1,7 @@
using System;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Extensions;
namespace LBPUnion.ProjectLighthouse.Administration;
// Next available ID for use: 7
@ -38,4 +42,12 @@ public static class CaseTypeExtensions
_ => false,
};
}
public static Task<bool> IsIdValid(this CaseType type, int affectedId, Database database)
{
if (type.AffectsUser()) return database.Users.Has(u => u.UserId == affectedId);
if (type.AffectsLevel()) return database.Slots.Has(u => u.SlotId == affectedId);
throw new ArgumentOutOfRangeException();
}
}