ProjectLighthouse/ProjectLighthouse.Servers.Website/Pages/Moderation/NewCasePage.cshtml.cs
Josh 4559d26a54
Fix bug where users can't be deleted (#648)
* Add username to mod cases if user is deleted

* Add timezone package to docker container

* Remove extra space in migration sql statement

* Changes from self-review
2023-01-29 22:10:36 -06:00

61 lines
No EOL
2 KiB
C#

using LBPUnion.ProjectLighthouse.Administration;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using Microsoft.AspNetCore.Mvc;
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Moderation;
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, [FromQuery] int? affectedId)
{
User? 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;
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();
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();
ModerationCase @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");
}
}