Refactor Audit service and view: implement GetCount and LoadPage methods, update OnReadData for improved data handling
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -61,29 +61,56 @@ namespace FoodsharingSiegen.Server.Data.Service
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Method Load
|
#region Public Method GetCount
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads the count (a. beging, 23.05.2022)
|
/// Gets the total count (ab)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="count">The count</param>
|
|
||||||
/// <param name="type">The type</param>
|
/// <param name="type">The type</param>
|
||||||
/// <returns>A task containing an operation result of list audit</returns>
|
/// <returns>A task containing an operation result of count</returns>
|
||||||
public async Task<OperationResult<List<Audit>>> Load(int count, AuditType? type = null)
|
public async Task<OperationResult<int>> GetCount(AuditType? type = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.CompletedTask;
|
var query = Context.Audits?.AsQueryable();
|
||||||
|
|
||||||
var query = Context.Audits?.Include(x => x.User).OrderByDescending(x => x.Created).AsQueryable();
|
|
||||||
|
|
||||||
if (count > 0)
|
|
||||||
query = query?.Take(count);
|
|
||||||
|
|
||||||
if (type != null)
|
if (type != null)
|
||||||
query = query?.Where(x => x.Type == type);
|
query = query?.Where(x => x.Type == type);
|
||||||
|
|
||||||
var mat = query?.ToList();
|
if (query == null) return new(0);
|
||||||
|
|
||||||
|
var count = await query.CountAsync();
|
||||||
|
return new(count);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return new(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Public Method LoadPage
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads the page of audits (ab)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="skip">The skip count</param>
|
||||||
|
/// <param name="take">The take count</param>
|
||||||
|
/// <param name="type">The type</param>
|
||||||
|
/// <returns>A task containing an operation result of list audit</returns>
|
||||||
|
public async Task<OperationResult<List<Audit>>> LoadPage(int skip, int take, AuditType? type = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = Context.Audits?.Include(x => x.User).OrderByDescending(x => x.Created).AsQueryable();
|
||||||
|
|
||||||
|
if (type != null)
|
||||||
|
query = query?.Where(x => x.Type == type);
|
||||||
|
|
||||||
|
query = query?.Skip(skip).Take(take);
|
||||||
|
|
||||||
|
var mat = await query!.ToListAsync();
|
||||||
|
|
||||||
if (mat != null) return new(mat);
|
if (mat != null) return new(mat);
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,8 @@
|
|||||||
|
|
||||||
<DataGrid TItem="Audit"
|
<DataGrid TItem="Audit"
|
||||||
Data="@Audits"
|
Data="@Audits"
|
||||||
|
ReadData="@OnReadData"
|
||||||
|
TotalItems="@TotalAudits"
|
||||||
VirtualizeOptions="@(new() { DataGridHeight = "100%", DataGridMaxHeight = "100%"})"
|
VirtualizeOptions="@(new() { DataGridHeight = "100%", DataGridMaxHeight = "100%"})"
|
||||||
Virtualize="true"
|
Virtualize="true"
|
||||||
Responsive>
|
Responsive>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using Blazorise.DataGrid;
|
||||||
using FoodsharingSiegen.Contracts.Entity;
|
using FoodsharingSiegen.Contracts.Entity;
|
||||||
using FoodsharingSiegen.Server.Data.Service;
|
using FoodsharingSiegen.Server.Data.Service;
|
||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
@@ -26,16 +27,44 @@ namespace FoodsharingSiegen.Server.Pages
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private List<Audit>? Audits { get; set; }
|
private List<Audit>? Audits { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the value of the total audits (ab)
|
||||||
|
/// </summary>
|
||||||
|
private int TotalAudits { get; set; }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Override InitializeDataAsync
|
#region Override InitializeDataAsync
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override async Task InitializeDataAsync()
|
protected override Task InitializeDataAsync()
|
||||||
{
|
{
|
||||||
var loadR = await AuditService?.Load(100)!;
|
return Task.CompletedTask;
|
||||||
if (loadR.Success)
|
}
|
||||||
Audits = loadR.Data;
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Private Method OnReadData
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called when data is read (ab)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e">The params</param>
|
||||||
|
private async Task OnReadData(DataGridReadDataEventArgs<Audit> e)
|
||||||
|
{
|
||||||
|
var countLoad = await AuditService?.GetCount()!;
|
||||||
|
if (countLoad.Success)
|
||||||
|
TotalAudits = countLoad.Data;
|
||||||
|
|
||||||
|
// Default fallback if VirtualizeCount is not set, though Blazor shouldn't do this usually
|
||||||
|
var limit = e.VirtualizeCount > 0 ? e.VirtualizeCount : 50;
|
||||||
|
var offset = e.VirtualizeOffset;
|
||||||
|
|
||||||
|
var itemsLoad = await AuditService?.LoadPage(offset, limit)!;
|
||||||
|
if (itemsLoad.Success)
|
||||||
|
Audits = itemsLoad.Data;
|
||||||
|
|
||||||
|
await InvokeAsync(StateHasChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
Reference in New Issue
Block a user