/// <summary> /// Gets the generic object by page /// </summary> /// <typeparam name="T">generic type</typeparam> /// <param name="filter">filter to fetch the object</param> /// <param name="page">page</param> /// <param name="itemsPerPage">itens per page</param> /// <returns>An object with pagination</returns> public override async Task <ILightPaging <T> > GetByPageAsync <T>(Expression <Func <T, bool> > filter, int page, int itemsPerPage) { await base.GetByPageAsync <T>(filter, page, itemsPerPage); //Calls the base class because there may be some generic behavior in it _typeToCollectionMap.TryGetValue(typeof(T), out var _collection); List <T> list = new List <T>(); string continuationToken = null; FeedOptions options = new FeedOptions { MaxItemCount = itemsPerPage, EnableCrossPartitionQuery = true, PartitionKey = new PartitionKey(Undefined.Value) }; IQueryable <T> queryBase = _client.CreateDocumentQuery <T>((await _collection).DocumentsLink, options); if (filter != null) { queryBase = queryBase.Where(filter); } var finalQuery = queryBase.AsDocumentQuery(); int queryPage = 0; while (finalQuery.HasMoreResults && queryPage <= page) { var data = await finalQuery.ExecuteNextAsync <T>(); if (queryPage++ == page) { continuationToken = data.ResponseContinuation; list.AddRange(data.ToList()); } } LightPaging <T> result = new LightPaging <T>() { Data = list, Page = page + 1, ItemsPerPage = itemsPerPage, TotalCount = await CountAsync <T>(filter), ContinuationToken = continuationToken }; result.TotalPages = result.TotalCount / itemsPerPage; if (result.TotalCount % itemsPerPage > 0) { result.TotalPages++; } return(result); }
/// <summary> /// Gets the generic object by page /// </summary> /// <typeparam name="T">generic type</typeparam> /// <param name="filter">filter to fetch the object</param> /// <param name="page">page</param> /// <param name="itemsPerPage">itens per page</param> /// <returns>An object with pagination</returns> public override async Task <ILightPaging <T> > GetByPageAsync <T>(Expression <Func <T, bool> > filter, int page, int itemsPerPage) { await base.GetByPageAsync <T>(filter, page, itemsPerPage); //Calls the base class because there may be some generic behavior in it List <T> list = new List <T>(); string continuationToken = null; IQueryable <T> queryBase = null; if (filter != null) { queryBase = queryBase.Where(filter); } var finalQuery = default(dynamic); int queryPage = 0; if (finalQuery != null) { while (finalQuery.HasMoreResults && queryPage <= page) { var data = await finalQuery.ExecuteNextAsync <T>(); if (queryPage++ == page) { continuationToken = data.ResponseContinuation; list.AddRange(data.ToList()); } } } LightPaging <T> result = new LightPaging <T>() { Data = list, Page = page + 1, ItemsPerPage = itemsPerPage, TotalCount = await CountAsync <T>(), ContinuationToken = continuationToken }; result.TotalPages = result.TotalCount / itemsPerPage; if (result.TotalCount % itemsPerPage > 0) { result.TotalPages++; } return(result); }
/// <summary> /// Gets the generic object by page /// </summary> /// <typeparam name="T">generic type</typeparam> /// <param name="filter">filter to fetch the object</param> /// <param name="page">page</param> /// <param name="itemsPerPage">itens per page</param> /// <returns>An object with pagination</returns> public override async Task <ILightPaging <T> > GetByPageAsync <T>(Expression <Func <T, bool> > filter, int page, int itemsPerPage) { await base.GetByPageAsync <T>(filter, page, itemsPerPage); //Calls the base class because there may be some generic behavior in it List <T> list = new List <T>(); Query qry = _collection.OrderBy("Id"); IEnumerable <ExpressionAnalyzed> expressions = (IEnumerable <ExpressionAnalyzed>)_evaluatePredicate.Evaluate <T>(filter); expressions.ToList().ForEach(x => { switch (x.OperatorBetweenPropAndValue.ExpressionType) { case ExpressionType.NotEqual: qry.WhereEqualTo(x.PropertyName, x.PropertyValue); break; case ExpressionType.Equal: qry.WhereEqualTo(x.PropertyName, x.PropertyValue); break; case ExpressionType.GreaterThan: qry.WhereGreaterThan(x.PropertyName, x.PropertyValue); break; case ExpressionType.GreaterThanOrEqual: qry.WhereGreaterThanOrEqualTo(x.PropertyName, x.PropertyValue); break; case ExpressionType.LessThan: qry.WhereLessThan(x.PropertyName, x.PropertyValue); break; case ExpressionType.LessThanOrEqual: qry.WhereLessThanOrEqualTo(x.PropertyName, x.PropertyValue); break; default: qry.WhereEqualTo(x.PropertyName, x.PropertyValue); break; } }); qry.StartAfter((page == 1 ? 0 : page * itemsPerPage)).Limit(itemsPerPage); QuerySnapshot ret = await qry.GetSnapshotAsync(); foreach (var item in ret.Documents) { list.Add(HydrateEntity <T>(item)); } LightPaging <T> result = new LightPaging <T>() { Data = list, Page = page + 1, ItemsPerPage = itemsPerPage, TotalCount = await CountAsync <T>(), ContinuationToken = "" }; result.TotalPages = result.TotalCount / itemsPerPage; if (result.TotalCount % itemsPerPage > 0) { result.TotalPages++; } return(result); }