Updated the dynamic sorting in collection controller
This commit is contained in:
parent
8f5a49deed
commit
457e3b411e
@ -24,6 +24,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Document = Marco.Pms.Model.DocumentManager.Document;
|
using Document = Marco.Pms.Model.DocumentManager.Document;
|
||||||
|
using Invoice = Marco.Pms.Model.Collection.Invoice;
|
||||||
|
|
||||||
namespace Marco.Pms.Services.Controllers
|
namespace Marco.Pms.Services.Controllers
|
||||||
{
|
{
|
||||||
@ -165,10 +166,9 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
_logger.LogDebug("Project filter applied: {ProjectId}", projectId.Value);
|
_logger.LogDebug("Project filter applied: {ProjectId}", projectId.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (advanceFilter != null)
|
query = query.ApplyCustomFilters(advanceFilter, "InvoiceDate");
|
||||||
{
|
|
||||||
query = query.ApplyCustomFilters(advanceFilter);
|
if (advanceFilter?.SearchFilters != null)
|
||||||
if (advanceFilter.SearchFilters != null)
|
|
||||||
{
|
{
|
||||||
var invoiceSearchFilter = advanceFilter.SearchFilters.Where(f => f.Column != "ProjectName").ToList();
|
var invoiceSearchFilter = advanceFilter.SearchFilters.Where(f => f.Column != "ProjectName").ToList();
|
||||||
if (invoiceSearchFilter.Any())
|
if (invoiceSearchFilter.Any())
|
||||||
@ -176,23 +176,11 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
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 totalItems = await query.CountAsync();
|
||||||
|
var totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
|
||||||
_logger.LogInfo("Total invoices found: {TotalItems}", totalItems);
|
_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
|
var pagedInvoices = await query
|
||||||
.Skip((pageNumber - 1) * pageSize)
|
.Skip((pageNumber - 1) * pageSize)
|
||||||
.Take(pageSize)
|
.Take(pageSize)
|
||||||
@ -241,15 +229,6 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
results.Add(vm);
|
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);
|
_logger.LogInfo("Returning {Count} invoices (page {PageNumber} of {TotalPages}).", results.Count, pageNumber, totalPages);
|
||||||
|
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(
|
return Ok(ApiResponse<object>.SuccessResponse(
|
||||||
|
|||||||
@ -8,10 +8,10 @@ namespace Marco.Pms.Services.Extensions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class QueryableExtensions
|
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)
|
// 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)
|
foreach (var advanceFilter in filter.AdvanceFilters)
|
||||||
{
|
{
|
||||||
@ -41,7 +41,7 @@ namespace Marco.Pms.Services.Extensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. Apply Sorting
|
// 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"
|
// Build a comma-separated sort string: "Column1 desc, Column2 asc"
|
||||||
var sortExpressions = new List<string>();
|
var sortExpressions = new List<string>();
|
||||||
@ -53,7 +53,19 @@ namespace Marco.Pms.Services.Extensions
|
|||||||
|
|
||||||
query = query.OrderBy(string.Join(", ", sortExpressions));
|
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;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,9 +88,9 @@ namespace Marco.Pms.Services.Extensions
|
|||||||
|
|
||||||
public static IQueryable<T> ApplyGroupByFilters<T>(this IQueryable<T> query, string groupByColumn)
|
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;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user