Skip to content

Commit

Permalink
Fix paging in DeletedMessages/Infractions
Browse files Browse the repository at this point in the history
Move RecordsPage to Modix.Models project
  • Loading branch information
calledude committed Sep 6, 2024
1 parent 36d37ac commit 026b671
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 49 deletions.
26 changes: 0 additions & 26 deletions src/Modix.Data/Models/RecordsPage.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/Modix.Data/Repositories/DeletedMessageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Modix.Data.Models;
using Modix.Data.Models.Moderation;
using Modix.Data.Utilities;
using Modix.Models;

namespace Modix.Data.Repositories
{
Expand Down
1 change: 1 addition & 0 deletions src/Modix.Data/Repositories/InfractionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Modix.Data.Models;
using Modix.Data.Models.Moderation;
using Modix.Data.Utilities;
using Modix.Models;
using Modix.Models.Moderation;

namespace Modix.Data.Repositories
Expand Down
23 changes: 23 additions & 0 deletions src/Modix.Models/RecordsPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Modix.Models;

/// <summary>
/// Describes paged subset of records, from a larger recordset.
/// </summary>
/// <typeparam name="T">The type of record contained in the page.</typeparam>
public class RecordsPage<T>
{
/// <summary>
/// The total number of records in the recordset.
/// </summary>
public long TotalRecordCount { get; set; }

/// <summary>
/// The number of records in the recordset, after applying an optional set of filtering criteria.
/// </summary>
public long FilteredRecordCount { get; set; }

/// <summary>
/// The current page of records, selected from the larger recordset.
/// </summary>
public IReadOnlyCollection<T> Records { get; set; } = null!;
}
10 changes: 5 additions & 5 deletions src/Modix.Services/Moderation/ModerationService.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#nullable enable
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.Net;
using Discord.WebSocket;
using Modix.Data.Models;
using Modix.Data.Models.Core;
using Modix.Data.Models.Moderation;
using Modix.Data.Repositories;
using Modix.Models;
using Modix.Models.Core;
using Modix.Models.Moderation;
using Modix.Services.Core;
using Modix.Services.Utilities;
using Serilog;
using System.Threading;
using Modix.Models.Core;
using Modix.Models.Moderation;

namespace Modix.Services.Moderation
{
Expand Down
11 changes: 6 additions & 5 deletions src/Modix.Web.Wasm/Components/Infractions/DeletedMessages.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using Modix.Web.Shared.Models.DeletedMessages
@using Modix.Models
@using Modix.Web.Shared.Models.DeletedMessages
@using MudBlazor

<PageTitle>Modix - Deletions</PageTitle>
Expand Down Expand Up @@ -66,7 +67,7 @@
</MudDialog>

<MudContainer MaxWidth="MaxWidth.False">
<MudTable @ref="@TableRef" Bordered="true" ServerData="LoadDeletedMessages" SortLabel="Sort By">
<MudTable @ref="@TableRef" Virtualize="true" Bordered="true" ServerData="LoadDeletedMessages" SortLabel="Sort By">
<ToolBarContent>
<MudButton Class="ml-1" Variant="Variant.Filled" OnClick="RefreshTable" StartIcon="@Icons.Material.Filled.Refresh" Color="MudBlazor.Color.Primary">Refresh</MudButton>
</ToolBarContent>
Expand Down Expand Up @@ -176,12 +177,12 @@

response.EnsureSuccessStatusCode();

var result = await response.Content.ReadFromJsonAsync<DeletedMessageBatchInformation[]>();
var result = await response.Content.ReadFromJsonAsync<RecordsPage<DeletedMessageBatchInformation>>();

return new TableData<DeletedMessageBatchInformation>
{
TotalItems = result.Length,
Items = result
TotalItems = (int)result.TotalRecordCount,
Items = result.Records
};
}

Expand Down
12 changes: 6 additions & 6 deletions src/Modix.Web.Wasm/Components/Infractions/Infractions.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using Microsoft.AspNetCore.Components.Authorization
@using Modix.Models
@using Modix.Models.Core
@using Modix.Web.Models;
@using Modix.Web.Shared.Models.Common
Expand Down Expand Up @@ -79,7 +80,6 @@

<MudContainer MaxWidth="MaxWidth.False">


<div class="d-flex mb-1 gap-sm-4 align-sm-center">
<div class="d-flex flex-sm-row flex-column align-start">
<MudButton Class="ml-1 mb-1" Variant="Variant.Filled" OnClick="ToggleDialog" StartIcon="@Icons.Material.Filled.Create" Color="Color.Primary">Create</MudButton>
Expand All @@ -92,7 +92,7 @@
</div>
</div>

<MudTable @ref="@TableRef" SortLabel="Sort By" Bordered="true" ServerData="LoadInfractions">
<MudTable Virtualize="true" @ref="@TableRef" SortLabel="Sort By" Bordered="true" ServerData="LoadInfractions">
<HeaderContent>
<MudTh Class="center-text vertical-top">
<MudTableSortLabel T="InfractionData" SortBy="x => x.Id">Id</MudTableSortLabel>
Expand Down Expand Up @@ -416,13 +416,13 @@
using var client = HttpClientFactory.CreateClient("api");
using var response = await client.PutAsJsonAsync("api/infractions", new InfractionsQuery(_tableFilter, tableState));
response.EnsureSuccessStatusCode();

var infractions = await response.Content.ReadFromJsonAsync<InfractionData[]>();
var infractions = await response.Content.ReadFromJsonAsync<RecordsPage<InfractionData>>();

return new TableData<InfractionData>
{
Items = infractions,
TotalItems = infractions.Length
Items = infractions.Records,
TotalItems = (int)infractions.TotalRecordCount
};
}

Expand Down
14 changes: 10 additions & 4 deletions src/Modix.Web/Controllers/DeletedMessagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using Microsoft.AspNetCore.Mvc;
using Modix.Data.Models;
using Modix.Data.Models.Moderation;
using Modix.Models;
using Modix.Models.Core;
using Modix.Services.Moderation;
using Modix.Services.Utilities;
using Modix.Web.Shared.Models.DeletedMessages;
using MudBlazor;

namespace Modix.Web.Controllers;

Expand All @@ -27,7 +27,7 @@ public DeletedMessagesController(IModerationService moderationService, DiscordSo
}

[HttpPut]
public async Task<DeletedMessageBatchInformation[]> GetDeletedMessagesBatchAsync(DeletedMessagesQuery deletedMessagesQuery)
public async Task<RecordsPage<DeletedMessageBatchInformation>> GetDeletedMessagesBatchAsync(DeletedMessagesQuery deletedMessagesQuery)
{
var tableState = deletedMessagesQuery.TableState;
var tableFilter = deletedMessagesQuery.Filter;
Expand Down Expand Up @@ -60,10 +60,9 @@ public async Task<DeletedMessageBatchInformation[]> GetDeletedMessagesBatchAsync
PageSize = tableState.PageSize,
};


var deletedMessages = await _moderationService.SearchDeletedMessagesAsync(searchCriteria, [sortingCriteria], pagingCriteria);

return deletedMessages.Records
var deletedMessagesBatchInformation = deletedMessages.Records
.Select(x => new DeletedMessageBatchInformation(
x.Channel.Name,
x.Author.GetFullUsername(),
Expand All @@ -73,6 +72,13 @@ public async Task<DeletedMessageBatchInformation[]> GetDeletedMessagesBatchAsync
x.Reason,
x.BatchId))
.ToArray();

return new RecordsPage<DeletedMessageBatchInformation>
{
FilteredRecordCount = deletedMessagesBatchInformation.Length,
Records = deletedMessagesBatchInformation,
TotalRecordCount = deletedMessages.TotalRecordCount
};
}

[HttpGet("{batchId}")]
Expand Down
13 changes: 10 additions & 3 deletions src/Modix.Web/Controllers/InfractionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
using Microsoft.AspNetCore.Mvc;
using Modix.Data.Models;
using Modix.Data.Models.Moderation;
using Modix.Models;
using Modix.Models.Core;
using Modix.Services.Moderation;
using Modix.Web.Shared.Models.Infractions;
using MudBlazor;

namespace Modix.Web.Controllers;

Expand All @@ -25,7 +25,7 @@ public InfractionsController(IModerationService moderationService, DiscordSocket

[HttpPut]
[Authorize(Roles = nameof(AuthorizationClaim.ModerationRead))]
public async Task<InfractionData[]> GetInfractionsAsync(InfractionsQuery infractionsQuery)
public async Task<RecordsPage<InfractionData>> GetInfractionsAsync(InfractionsQuery infractionsQuery)
{
var tableState = infractionsQuery.TableState;
var tableFilter = infractionsQuery.Filter;
Expand Down Expand Up @@ -67,7 +67,7 @@ public async Task<InfractionData[]> GetInfractionsAsync(InfractionsQuery infract
outranksValues[subjectId] = await _moderationService.DoesModeratorOutrankUserAsync(guildId, SocketUser.Id, subjectId);
}

return infractions.Records
var infractionData = infractions.Records
.Select(x => new InfractionData(
x.Id,
x.GuildId,
Expand All @@ -90,6 +90,13 @@ x.DeleteAction is null
&& outranksValues[x.Subject.Id]
))
.ToArray();

return new RecordsPage<InfractionData>
{
FilteredRecordCount = infractions.Records.Count,
Records = infractionData,
TotalRecordCount = infractions.TotalRecordCount,
};
}

[Authorize(Roles = nameof(AuthorizationClaim.ModerationRescind))]
Expand Down

0 comments on commit 026b671

Please sign in to comment.