HRM-10/HRM.BO/VirtualPaging/VirtualPaging.cs
2026-11-09 14:54:13 +06:00

75 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
namespace HRM.BO
{
/// <summary>
/// What &lt;app-virtual-grid&gt; sends to a page endpoint: the page's own filters plus the grid's
/// scroll position, search text and sort. The same class is used by every page, so no page needs a
/// request class of its own. See HRM.DA/Service/VirtualPaging/README.md.
/// </summary>
public class VirtualPageRequest
{
/// <summary>The page's filters, e.g. { fromDate: "2025-01-01", toDate: "2025-12-31" }. Read them with GetDate / GetInt / GetString.</summary>
public Dictionary<string, string> Filters { get; set; } = new Dictionary<string, string>();
/// <summary>The grid's columns (property names). Only these are sent back and searched.</summary>
public List<string> Fields { get; set; } = new List<string>();
public int Skip { get; set; }
/// <summary>Rows wanted. 0 = all rows (used by Export to Excel).</summary>
public int Take { get; set; } = 100;
public string Search { get; set; }
public string SortField { get; set; }
/// <summary>"asc" or "desc".</summary>
public string SortDir { get; set; }
/// <summary>True on Preview: load the data again. False on scroll/sort/search/export: use the kept copy.</summary>
public bool Refresh { get; set; }
/// <summary>A filter as text, or null when it was not sent.</summary>
public string GetString(string name)
{
string value;
if (Filters != null && Filters.TryGetValue(name, out value) && !string.IsNullOrWhiteSpace(value))
return value.Trim();
return null;
}
/// <summary>A "yyyy-MM-dd" filter. Throws a message fit for the user when it is missing or invalid.</summary>
public DateTime GetDate(string name, string label = null)
{
DateTime value;
if (!DateTime.TryParseExact(GetString(name), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out value))
throw new ArgumentException("Please select a valid " + (label ?? name) + ".");
return value;
}
/// <summary>A whole-number filter. Throws a message fit for the user when it is missing or invalid.</summary>
public int GetInt(string name, string label = null)
{
int value;
if (!int.TryParse(GetString(name), NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
throw new ArgumentException("Please select a valid " + (label ?? name) + ".");
return value;
}
}
/// <summary>One window of rows for the grid.</summary>
public class VirtualPageResult
{
/// <summary>Rows matching the search (all rows when there is no search).</summary>
public int Total { get; set; }
/// <summary>All rows, ignoring the search.</summary>
public int TotalWithoutSearch { get; set; }
/// <summary>The rows of this window, holding only the requested fields.</summary>
public List<Dictionary<string, object>> Data { get; set; } = new List<Dictionary<string, object>>();
}
}