Skip to content

Commit

Permalink
Added infractions display to admin panel
Browse files Browse the repository at this point in the history
- Also, Riok.Mapperly experiment
  • Loading branch information
Atulin committed Apr 26, 2024
1 parent 1d3e86b commit 6adb278
Show file tree
Hide file tree
Showing 12 changed files with 679 additions and 1,137 deletions.
6 changes: 3 additions & 3 deletions Ogma3.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ogma3", "Ogma3\Ogma3.csproj", "{82836CC4-2B02-485E-9E98-E49F178652ED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "Utils\Utils.csproj", "{9EC165CA-0939-4CBA-8DFA-6F7DA8DEC51E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "Utils\Utils.csproj", "{9EC165CA-0939-4CBA-8DFA-6F7DA8DEC51E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils.Tests", "Utils.Tests\Utils.Tests.csproj", "{45A4BB8B-BA00-401C-B888-6016A51E9DE1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils.Tests", "Utils.Tests\Utils.Tests.csproj", "{45A4BB8B-BA00-401C-B888-6016A51E9DE1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ogma3.Tests", "Ogma3.Tests\Ogma3.Tests.csproj", "{3B4D50E6-4FFD-4262-8CF7-CAD4BB3EC715}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ogma3.Tests", "Ogma3.Tests\Ogma3.Tests.csproj", "{3B4D50E6-4FFD-4262-8CF7-CAD4BB3EC715}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
26 changes: 8 additions & 18 deletions Ogma3/Api/V1/Infractions/Commands/CreateInfraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,31 @@ public CommandValidator()
}
}

public class Handler : BaseHandler, IRequestHandler<Command, ActionResult<Response>>
public class Handler(ApplicationDbContext context, IUserService userService, IMemoryCache cache) : BaseHandler, IRequestHandler<Command, ActionResult<Response>>
{
private readonly ApplicationDbContext _context;
private readonly long? _uid;
private readonly IMemoryCache _cache;

public Handler(ApplicationDbContext context, IUserService userService, IMemoryCache cache)
{
_context = context;
_cache = cache;
_uid = userService.User?.GetNumericId();
}

public async ValueTask<ActionResult<Response>> Handle(Command request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();
if (userService.User?.GetNumericId() is not { } uid) return Unauthorized();

var (userId, reason, dateTime, type) = request;
var infraction = new Infraction
{
IssuedById = (long)_uid,
IssuedById = uid,
UserId = userId,
Reason = reason,
ActiveUntil = dateTime,
Type = type
Type = type,
};
_context.Infractions.Add(infraction);
await _context.SaveChangesAsync(cancellationToken);
context.Infractions.Add(infraction);
await context.SaveChangesAsync(cancellationToken);

if (infraction.Type == InfractionType.Ban)
{
_cache.Set(UserBanMiddleware.CacheKey(infraction.UserId), infraction.ActiveUntil);
cache.Set(UserBanMiddleware.CacheKey(infraction.UserId), infraction.ActiveUntil);
}

return Ok(new Response(infraction.Id, (long)_uid, userId));
return Ok(new Response(infraction.Id, uid, userId));
}
}

Expand Down
21 changes: 6 additions & 15 deletions Ogma3/Api/V1/Infractions/Commands/DeactivateInfraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,24 @@ public static class DeactivateInfraction
{
public sealed record Command(long InfractionId) : IRequest<ActionResult<Response>>;

public class Handler : BaseHandler, IRequestHandler<Command, ActionResult<Response>>
public class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Command, ActionResult<Response>>
{
private readonly ApplicationDbContext _context;
private readonly long? _uid;

public Handler(ApplicationDbContext context, IUserService userService)
{
_context = context;
_uid = userService.User?.GetNumericId();
}

public async ValueTask<ActionResult<Response>> Handle(Command request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();
if (userService.User?.GetNumericId() is not { } _uid) return Unauthorized();

var infraction = await _context.Infractions
var infraction = await context.Infractions
.Where(i => i.Id == request.InfractionId)
.FirstOrDefaultAsync(cancellationToken);

if (infraction is null) return NotFound();

infraction.RemovedAt = DateTime.Now;
infraction.RemovedById = (long)_uid;
infraction.RemovedById = _uid;

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

return Ok(new Response(infraction.Id, (long)_uid, infraction.UserId));
return Ok(new Response(infraction.Id, _uid, infraction.UserId));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Infractions/Queries/GetInfractionDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async ValueTask<ActionResult<Result>> Handle(Query request, CancellationT
Reason = i.Reason,
Type = i.Type,
IssuedByName = i.IssuedBy.UserName,
RemovedByName = i.RemovedBy == null ? null : i.RemovedBy.UserName
RemovedByName = i.RemovedBy == null ? null : i.RemovedBy.UserName,
})
.FirstOrDefaultAsync(cancellationToken);

Expand Down
31 changes: 31 additions & 0 deletions Ogma3/Areas/Admin/Pages/Infractions.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@page
@using Ogma3.Data.Infractions
@model Ogma3.Areas.Admin.Pages.InfractionsModel
@{
ViewData["Title"] = "Infractions";
ViewData["ActivePage"] = NavPages.Infractions;
}

<table class="o-table">
<tr>
<th>Username</th>
<th>Type</th>
<th>Reason</th>
<th>Duration</th>
<th>Issuer</th>
<th>Lifted</th>
<th>Lifter</th>
</tr>
@foreach (var infraction in Model.Infractions)
{
<tr>
<td>@infraction.UserUserName</td>
<td>@infraction.Type.ToStringFast()</td>
<td>@infraction.Reason</td>
<td>@infraction.ActiveUntil</td>
<td>@infraction.IssuedByUserName</td>
<td>@infraction.RemovedAt</td>
<td>@infraction.RemovedByUserName</td>
</tr>
}
</table>
50 changes: 50 additions & 0 deletions Ogma3/Areas/Admin/Pages/Infractions.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore;
using Ogma3.Areas.Identity.Pages.Account.Manage;
using Ogma3.Data;
using Ogma3.Data.Infractions;
using Riok.Mapperly.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utils.Extensions;

namespace Ogma3.Areas.Admin.Pages;

public class InfractionsModel(ApplicationDbContext context) : PageModel
{
public required List<InfractionDto> Infractions { get; set; }

public async Task<IActionResult> OnGetAsync()
{
Infractions = await context.Infractions
.OrderBy(i => i.IssueDate)
.ToInfractionDtos()
.ToListAsync();

return Page();
}
}

[Mapper(PreferParameterlessConstructors = false)]
public static partial class InfractionMapper
{
public static partial IQueryable<InfractionDto> ToInfractionDtos(this IQueryable<Infraction> infraction);
}

public record InfractionDto(
string UserUserName,
long UserId,
DateTime IssueDate,
DateTime ActiveUntil,
DateTime? RemovedAt,
string Reason,
InfractionType Type,
string IssuedByUserName,
long IssuedById,
string RemovedByUserName,
long RemovedById
);
1 change: 1 addition & 0 deletions Ogma3/Areas/Admin/Pages/NavPages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class NavPages
public static string ModLog => "ModLog";
public static string Reports => "Reports";
public static string Faq => "FAQ";
public static string Infractions => "Infractions";

public static string PageNavClass(ViewContext viewContext, string page)
{
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Areas/Admin/Pages/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@{
@{
Layout = "/Pages/Shared/_Layout.cshtml";
ViewData["is-admin"] = true;
}
Expand Down
3 changes: 2 additions & 1 deletion Ogma3/Areas/Admin/Pages/_ManageNav.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

<partial name="Shared/_NavItemPartial" model="@(new NavItemPartial(NavPages.InviteCodes))"/>
<partial name="Shared/_NavItemPartial" model="@(new NavItemPartial(NavPages.Roles, "User roles"))"/>
<partial name="Shared/_NavItemPartial" model="@(new NavItemPartial(NavPages.Users, "User management"))"/>
<partial name="Shared/_NavItemPartial" model="@(new NavItemPartial(NavPages.Users, "User management"))" />
<partial name="Shared/_NavItemPartial" model="@(new NavItemPartial(NavPages.Infractions, "Infractions"))" />

<li class="nav-item">
Other
Expand Down
Loading

0 comments on commit 6adb278

Please sign in to comment.