104 lines
4.5 KiB
C#
104 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
|
|
namespace HRM.BO
|
|
{
|
|
/// <summary>
|
|
/// What <app-virtual-grid> sends to a page endpoint: the page's own filters plus the grid's columns,
|
|
/// 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/DA/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. Only these are selected, searched and sortable.</summary>
|
|
public List<VirtualPageColumn> Columns { get; set; } = new List<VirtualPageColumn>();
|
|
|
|
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 when the totals must be counted (Preview, new search). Scrolling and sorting do not change
|
|
/// the totals, so they send false and the COUNT is skipped.
|
|
/// </summary>
|
|
public bool CountTotal { get; set; }
|
|
|
|
/// <summary>
|
|
/// True for Export to Excel: every row matching the search, in the grid's sort, comes back as an .xlsx
|
|
/// file (VirtualPageResult.ExcelFile) instead of JSON.
|
|
/// </summary>
|
|
public bool ExportExcel { 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 grid column: Field = a column alias of the page's query; Type = how it is shown.</summary>
|
|
public class VirtualPageColumn
|
|
{
|
|
public string Field { get; set; }
|
|
|
|
/// <summary>"text" (default), "number", "money", "date" or "datetime".</summary>
|
|
public string Type { get; set; }
|
|
|
|
/// <summary>Header text in the Excel file.</summary>
|
|
public string Title { get; set; }
|
|
|
|
/// <summary>Column width in the grid (px); the Excel column gets a matching width.</summary>
|
|
public int Width { get; set; }
|
|
}
|
|
|
|
/// <summary>One window of rows for the grid.</summary>
|
|
public class VirtualPageResult
|
|
{
|
|
/// <summary>Rows matching the search (all rows when there is no search). Null when not counted (CountTotal = false).</summary>
|
|
public int? Total { get; set; }
|
|
|
|
/// <summary>All rows, ignoring the search. Null when not counted.</summary>
|
|
public int? TotalWithoutSearch { get; set; }
|
|
|
|
/// <summary>The rows of this window, holding only the requested columns.</summary>
|
|
public List<Dictionary<string, object>> Data { get; set; } = new List<Dictionary<string, object>>();
|
|
|
|
/// <summary>The .xlsx file, set only for ExportExcel requests. The controller returns it as a file.</summary>
|
|
public byte[] ExcelFile { get; set; }
|
|
|
|
public const string ExcelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
}
|
|
}
|