ProjectLighthouse/ProjectLighthouse.Servers.Website/Pages/Moderation/NewCasePage.cshtml.cs
koko 027173b9c7
Redesign case creation page (#854)
* Redesign case creation page

* Fix user id placeholder text

* Handle empty moderation history

* Mod history dropdown nitpick

* Fix styling issue with dropdown

ui fluid dropdown inherits from ui form

* Potentially fix NRE

* Un-require reason/mod notes

* Display username in moderation history view

* Order mod history by creation time descending

* Nitpick no moderation history string

* Handle AffectedUser null check within controller

* Fix styling issues

* Move moderation history segment above create case button segment

* Link back to affected user

* Move expiration field in with the other entries

* Grammatical consistency nitpick in history dropdown

* Handle empty case reasons in mod history

* This is the last nitpick, I swear!

* I lied. Variable naming consistency :trollface:

* Consolidate setPermanent function into button onclick

* Use HTML details and Fomantic list instead of dropdown

* Fix padding issue with details list

* Format history and user/id nicely

* Styling fixes and nitpicks of list items/links

* Apply suggestions from code review

* Clarification with code review suggestion
2023-08-07 04:45:46 +00:00

89 lines
No EOL
3.1 KiB
C#

using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Localization.StringLists;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types.Entities.Moderation;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Moderation.Cases;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Moderation;
public class NewCasePage : BaseLayout
{
public NewCasePage(DatabaseContext database) : base(database)
{ }
public CaseType Type { get; set; }
public int AffectedId { get; set; }
public UserEntity? AffectedUser { get; set; }
public List<ModerationCaseEntity> AffectedHistory { get; set; } = new();
public string? Error { get; private set; }
public async Task<IActionResult> OnGet([FromQuery] CaseType? type, [FromQuery] int? affectedId)
{
UserEntity? user = this.Database.UserFromWebRequest(this.Request);
if (user == null || !user.IsModerator) return this.Redirect("/login");
if (type == null) return this.BadRequest();
if (affectedId == null) return this.BadRequest();
this.Type = type.Value;
this.AffectedId = affectedId.Value;
this.AffectedUser = await this.Database.Users.FirstOrDefaultAsync(u => u.UserId == this.AffectedId);
if (this.AffectedUser == null) return this.BadRequest();
this.AffectedHistory = await this.Database.Cases.Where(c => c.AffectedId == this.AffectedId)
.OrderByDescending(c => c.CreatedAt)
.ToListAsync();
return this.Page();
}
public async Task<IActionResult> OnPost(CaseType? type, string? reason, string? modNotes, DateTime expires, int? affectedId)
{
UserEntity? user = this.Database.UserFromWebRequest(this.Request);
if (user == null || !user.IsModerator) return this.Redirect("/login");
if (type == null) return this.BadRequest();
if (affectedId == null) return this.BadRequest();
reason ??= string.Empty;
modNotes ??= string.Empty;
// if id is invalid then return bad request
if (!await type.Value.IsIdValid((int)affectedId, this.Database)) return this.BadRequest();
UserEntity? affectedUser =
await this.Database.Users.FirstOrDefaultAsync(u => u.UserId == affectedId.Value);
if (affectedUser == null) return this.NotFound();
if (affectedUser.IsModerator)
{
this.Error = this.Translate(ErrorStrings.ActionNoPermission);
return this.Page();
}
ModerationCaseEntity @case = new()
{
Type = type.Value,
Reason = reason,
ModeratorNotes = modNotes,
ExpiresAt = expires,
CreatedAt = DateTime.Now,
CreatorId = user.UserId,
CreatorUsername = user.Username,
AffectedId = affectedId.Value,
};
this.Database.Cases.Add(@case);
await this.Database.SaveChangesAsync();
return this.Redirect("/moderation/cases/0");
}
}