From 4bb96b205813f774c7592fc57004828e75b372e5 Mon Sep 17 00:00:00 2001 From: David Lebee Date: Sun, 21 Oct 2018 14:15:26 -0500 Subject: [PATCH] grouping result is ready, next step is to flatten the aggregates that match. --- PoweredSoft.DynamicQuery.Cli/Program.cs | 2 +- PoweredSoft.DynamicQuery/QueryHandler.cs | 46 ++++++++++++++------ PoweredSoft.DynamicQuery/QueryHandlerBase.cs | 12 +++++ PoweredSoft.DynamicQuery/QueryResult.cs | 13 +----- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/PoweredSoft.DynamicQuery.Cli/Program.cs b/PoweredSoft.DynamicQuery.Cli/Program.cs index 0060abf..9addeee 100644 --- a/PoweredSoft.DynamicQuery.Cli/Program.cs +++ b/PoweredSoft.DynamicQuery.Cli/Program.cs @@ -79,7 +79,7 @@ namespace PoweredSoft.DynamicQuery.Cli var queryable = list.AsQueryable(); var criteria = new QueryCriteria(); criteria.Page = 1; - criteria.PageSize = 2; + criteria.PageSize = 10; criteria.Groups = new List() { diff --git a/PoweredSoft.DynamicQuery/QueryHandler.cs b/PoweredSoft.DynamicQuery/QueryHandler.cs index 16c0992..6065e22 100644 --- a/PoweredSoft.DynamicQuery/QueryHandler.cs +++ b/PoweredSoft.DynamicQuery/QueryHandler.cs @@ -24,8 +24,9 @@ namespace PoweredSoft.DynamicQuery protected virtual IQueryExecutionResult ExecuteGrouping() { - var result = new GroupedQueryExecutionResult(); + var result = new QueryExecutionResult(); result.TotalRecords = CurrentQueryable.LongCount(); + CalculatePageCount(result); // intercept groups in advance to avoid doing it more than once :) var finalGroups = Criteria.Groups.Select(g => InterceptGroup(g)).ToList(); @@ -48,7 +49,7 @@ namespace PoweredSoft.DynamicQuery { var groupKeyIndex = -1; previousGroups.ForEach(pg => sb.Key($"Key_{++groupKeyIndex}")); - sb.Key($"Key_{++groupKeyIndex}"); + sb.Key($"Key_{++groupKeyIndex}", $"Key_{groupKeyIndex}"); Criteria.Aggregates.ForEach(a => { var selectType = ResolveSelectFrom(a.Type); @@ -80,24 +81,51 @@ namespace PoweredSoft.DynamicQuery CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => finalGroups.ForEach((fg, index) => gb.Path(fg.Path, $"Key_{index}"))); CurrentQueryable = CurrentQueryable.Select(sb => { - finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}")); + finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}", $"Key_{index}")); sb.ToList("Records"); }); - var temp = CurrentQueryable.ToDynamicClassList(); + var groupRecords = CurrentQueryable.ToDynamicClassList(); + result.Data = groupRecords.Select((groupRecord, groupRecordIndex) => + { + var groupRecordResult = new GroupQueryResult(); + GroupQueryResult previous = null; + + Criteria.Groups.ForEach((g, gi) => + { + bool isFirst = gi == 0; + bool isLast = Criteria.Groups.Count - 1 == gi; + var cgrr = isFirst ? groupRecordResult : new GroupQueryResult(); + cgrr.GroupPath = g.Path; + cgrr.GroupValue = groupRecord.GetDynamicPropertyValue($"Key_{gi}"); + + + if (!isLast) + cgrr.Data = new List(); + else + cgrr.Data = groupRecord.GetDynamicPropertyValue>("Records").Cast().ToList(); + + if (previous != null) + previous.Data.Add(cgrr); + + previous = cgrr; + }); + + return (object)groupRecordResult; + }).ToList(); return result; } - protected virtual IQueryExecutionResult ExecuteNoGrouping() { var result = new QueryExecutionResult(); // total records. result.TotalRecords = CurrentQueryable.LongCount(); + CalculatePageCount(result); // sorts and paging. ApplySorting(); @@ -106,14 +134,6 @@ namespace PoweredSoft.DynamicQuery // the data. result.Data = CurrentQueryable.ToObjectList(); - // if there is paging. - if (HasPaging) - { - if (result.TotalRecords < Criteria.PageSize) - result.NumberOfPages = 1; - else - result.NumberOfPages = result.TotalRecords / Criteria.PageSize + (result.TotalRecords % Criteria.PageSize != 0 ? 1 : 0); - } return result; } diff --git a/PoweredSoft.DynamicQuery/QueryHandlerBase.cs b/PoweredSoft.DynamicQuery/QueryHandlerBase.cs index 0c31ab2..50d0b50 100644 --- a/PoweredSoft.DynamicQuery/QueryHandlerBase.cs +++ b/PoweredSoft.DynamicQuery/QueryHandlerBase.cs @@ -75,6 +75,18 @@ namespace PoweredSoft.DynamicQuery }); } + protected virtual void CalculatePageCount(IQueryExecutionResult result) + { + if (!HasPaging) + return; + + if (result.TotalRecords < Criteria.PageSize) + result.NumberOfPages = 1; + else + result.NumberOfPages = result.TotalRecords / Criteria.PageSize + (result.TotalRecords % Criteria.PageSize != 0 ? 1 : 0); + } + + protected virtual List InterceptSort(ISort sort) { var original = new List() diff --git a/PoweredSoft.DynamicQuery/QueryResult.cs b/PoweredSoft.DynamicQuery/QueryResult.cs index 729ff92..6ba3114 100644 --- a/PoweredSoft.DynamicQuery/QueryResult.cs +++ b/PoweredSoft.DynamicQuery/QueryResult.cs @@ -25,26 +25,17 @@ namespace PoweredSoft.DynamicQuery public bool ShouldSerializeAggregates() => Aggregates != null; } - // not grouped. + // just result public class QueryExecutionResult : QueryResult, IQueryExecutionResult { public long TotalRecords { get; set; } public long? NumberOfPages { get; set; } } - // grouped. + // group result. public class GroupQueryResult : QueryResult, IGroupQueryResult { public string GroupPath { get; set; } public object GroupValue { get; set; } - - public IEnumerable GroupItems => Data.Cast(); - public bool ShouldSerializeGroupItems() => false; - } - - public class GroupedQueryExecutionResult : GroupQueryResult, IQueryExecutionResult - { - public long TotalRecords { get; set; } - public long? NumberOfPages { get; set; } } }