diff --git a/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml b/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml new file mode 100644 index 00000000..686ea3ec --- /dev/null +++ b/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml @@ -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"; +} + +
+ @Html.AntiForgeryToken() + + + + + +
+ + +
+ + +
+ +
+
\ No newline at end of file diff --git a/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml.cs b/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml.cs new file mode 100644 index 00000000..01253742 --- /dev/null +++ b/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml.cs @@ -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 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"); + } +} \ No newline at end of file diff --git a/ProjectLighthouse.Servers.Website/Pages/CasePage.cshtml b/ProjectLighthouse.Servers.Website/Pages/CasePage.cshtml index 425e1deb..c4f1df41 100644 --- a/ProjectLighthouse.Servers.Website/Pages/CasePage.cshtml +++ b/ProjectLighthouse.Servers.Website/Pages/CasePage.cshtml @@ -14,7 +14,6 @@ - DEBUG: Create Case
diff --git a/ProjectLighthouse.Servers.Website/Pages/Debug/NewCasePage.cshtml b/ProjectLighthouse.Servers.Website/Pages/Debug/NewCasePage.cshtml deleted file mode 100644 index 60eec7df..00000000 --- a/ProjectLighthouse.Servers.Website/Pages/Debug/NewCasePage.cshtml +++ /dev/null @@ -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"; -} - -
- @Html.AntiForgeryToken() - -
- - -
- - -
- - -
- -
-
\ No newline at end of file diff --git a/ProjectLighthouse.Servers.Website/Pages/Debug/NewCasePage.cshtml.cs b/ProjectLighthouse.Servers.Website/Pages/Debug/NewCasePage.cshtml.cs deleted file mode 100644 index f9ea1bf8..00000000 --- a/ProjectLighthouse.Servers.Website/Pages/Debug/NewCasePage.cshtml.cs +++ /dev/null @@ -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 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"); - } -} \ No newline at end of file diff --git a/ProjectLighthouse/Administration/CaseType.cs b/ProjectLighthouse/Administration/CaseType.cs index 31e608fd..c8a4cba1 100644 --- a/ProjectLighthouse/Administration/CaseType.cs +++ b/ProjectLighthouse/Administration/CaseType.cs @@ -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 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(); + } } \ No newline at end of file