Updated the dynamic sorting in collection controller

This commit is contained in:
ashutosh.nehete 2025-11-24 11:04:53 +05:30
parent 8f5a49deed
commit 457e3b411e
2 changed files with 25 additions and 34 deletions

View File

@ -24,6 +24,7 @@ using Microsoft.EntityFrameworkCore;
using MongoDB.Driver;
using System.Text.Json;
using Document = Marco.Pms.Model.DocumentManager.Document;
using Invoice = Marco.Pms.Model.Collection.Invoice;
namespace Marco.Pms.Services.Controllers
{
@ -165,34 +166,21 @@ namespace Marco.Pms.Services.Controllers
_logger.LogDebug("Project filter applied: {ProjectId}", projectId.Value);
}
if (advanceFilter != null)
query = query.ApplyCustomFilters(advanceFilter, "InvoiceDate");
if (advanceFilter?.SearchFilters != null)
{
query = query.ApplyCustomFilters(advanceFilter);
if (advanceFilter.SearchFilters != null)
var invoiceSearchFilter = advanceFilter.SearchFilters.Where(f => f.Column != "ProjectName").ToList();
if (invoiceSearchFilter.Any())
{
var invoiceSearchFilter = advanceFilter.SearchFilters.Where(f => f.Column != "ProjectName").ToList();
if (invoiceSearchFilter.Any())
{
query = query.ApplySearchFilters(invoiceSearchFilter);
}
query = query.ApplySearchFilters(invoiceSearchFilter);
}
}
var hasSortFilters = advanceFilter?.SortFilters?.Any() ?? false;
if (!hasSortFilters)
{
query = query.OrderByDescending(i => i.InvoiceDate);
}
var totalItems = await query.CountAsync();
var totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
_logger.LogInfo("Total invoices found: {TotalItems}", totalItems);
string groupByColumn = "ProjectId";
if (!string.IsNullOrWhiteSpace(advanceFilter?.GroupByColumn))
{
groupByColumn = advanceFilter.GroupByColumn;
}
query = query.ApplyGroupByFilters(groupByColumn);
var pagedInvoices = await query
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
@ -241,15 +229,6 @@ namespace Marco.Pms.Services.Controllers
results.Add(vm);
}
var totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
//string groupByColumn = "Project";
//if (!string.IsNullOrWhiteSpace(advanceFilter?.GroupByColumn))
//{
// groupByColumn = advanceFilter.GroupByColumn;
//}
//var resultQuery = results.AsQueryable();
//object finalResponseData = resultQuery.ApplyGroupByFilters(groupByColumn);
_logger.LogInfo("Returning {Count} invoices (page {PageNumber} of {TotalPages}).", results.Count, pageNumber, totalPages);
return Ok(ApiResponse<object>.SuccessResponse(

View File

@ -8,10 +8,10 @@ namespace Marco.Pms.Services.Extensions
/// </summary>
public static class QueryableExtensions
{
public static IQueryable<T> ApplyCustomFilters<T>(this IQueryable<T> query, AdvanceFilter filter)
public static IQueryable<T> ApplyCustomFilters<T>(this IQueryable<T> query, AdvanceFilter? filter, string defaultSortColumn)
{
// 1. Apply Advanced Filters (Arithmetic/Logic)
if (filter.AdvanceFilters != null && filter.AdvanceFilters.Any())
if (filter?.AdvanceFilters != null && filter.AdvanceFilters.Any())
{
foreach (var advanceFilter in filter.AdvanceFilters)
{
@ -41,7 +41,7 @@ namespace Marco.Pms.Services.Extensions
}
// 2. Apply Sorting
if (filter.SortFilters != null && filter.SortFilters.Any())
if (filter?.SortFilters != null && filter.SortFilters.Any())
{
// Build a comma-separated sort string: "Column1 desc, Column2 asc"
var sortExpressions = new List<string>();
@ -53,7 +53,19 @@ namespace Marco.Pms.Services.Extensions
query = query.OrderBy(string.Join(", ", sortExpressions));
}
else
{
// Default sorting
query = query.OrderBy($"{defaultSortColumn} desc");
}
// 3.Apply GroupBy
if (!string.IsNullOrEmpty(filter?.GroupByColumn))
{
query.GroupBy(filter.GroupByColumn, "it")
.Select("new (Key, it as Items)"); // Reshape to { Key: "Value", Items: [...] }
}
return query;
}
@ -76,9 +88,9 @@ namespace Marco.Pms.Services.Extensions
public static IQueryable<T> ApplyGroupByFilters<T>(this IQueryable<T> query, string groupByColumn)
{
query.GroupBy(groupByColumn).Select("new (Key, ToList() as Items)"); // Reshape to { Key: "Value", Items: [...] }
query.GroupBy(groupByColumn, "it")
.Select("new (Key, it as Items)"); // Reshape to { Key: "Value", Items: [...] }
return query;
}
}
}