using Dapper; using GB5Shared.DTO.Framework.Login; using GB5Shared.ListQuery; using QMSDAL.DTO; namespace QMSDAL.Query.Matrix { // ── MatrixListQB ─────────────────────────────────────────────────────────── // Builds the paged matrix list SQL by delegating to MatrixQB.BuildMatrixList. // This class owns only the sort-column whitelist and the Build() wiring. // All SQL text lives in MatrixQB to keep query strings in one place. // ────────────────────────────────────────────────────────────────────────── public sealed class MatrixListQB : IQueryBuilder { // ── Allowed sort columns ─────────────────────────────────────────────── // Keys are the client-facing field names (case-insensitive). // Values are the qualified SQL column expressions passed to ORDER BY. // FIX: matrixid added — the default fallback in BuildMatrixList is // MX.MATRIXID DESC; callers can now explicitly request it too. private static readonly IReadOnlyDictionary _sort = new Dictionary(StringComparer.OrdinalIgnoreCase) { ["matrixid"] = "MX.MATRIXID", ["matrixcode"] = "MX.MATRIXCODE", ["matrixstatus"] = "MX.MATRIXSTATUS", ["setfrom"] = "MX.SETFROM", ["activatedon"] = "MX.ACTIVATEDON", }; // ── Build ────────────────────────────────────────────────────────────── public (string Sql, DynamicParameters Params) Build( MatrixListQuery q, LoginDTO login, ISqlDialect d) { // ── Null guards ──────────────────────────────────────────────────── ArgumentNullException.ThrowIfNull(q); ArgumentNullException.ThrowIfNull(login); ArgumentNullException.ThrowIfNull(d); if (q.Criteria is null) throw new InvalidOperationException( "MatrixListQuery.Criteria must be set before building the query."); // ── Safe ORDER BY ────────────────────────────────────────────────── // SqlClauses.OrderBy validates the caller's SortBy against _sort // and falls back to the default if the value is absent or not allowed. var orderBy = SqlClauses.OrderBy( q.Criteria.SortBy, q.Criteria.SortDesc, _sort, defaultCol: "MX.MATRIXID DESC" ); // ── Delegate to MatrixQB ─────────────────────────────────────────── return MatrixQB.BuildMatrixList( q.Criteria, login, d, orderBy ); } } }