/// <summary> /// Get Paginated items. /// </summary> /// <param name="pageModel">the PagingParameterInModel. Must not be null.</param> /// <param name="orderBy">an optionnal orderByClause.</param> /// <returns>The corresponding borders paginated an optionnaly</returns> public async Task <PagingParameterOutModel <HRBorder> > GetBordersAsync(PagingParameterInModel pageModel, HRSortingParamModel orderBy = null) { PagingParameterOutModel <HRBorder> retour = null; if (_workflow == null) { if (_logger != null) { _logger.LogError("_workflow is null in HRCoreBordersServices"); } throw new MemberAccessException(); } if (pageModel == null) { if (_logger != null) { _logger.LogError("pageModel is null in HRCoreBordersServices : GetBordersAsync"); } throw new ArgumentNullException(); } using (Task <PagingParameterOutModel <HRBorder> > retourTask = _workflow.GetQueryResultsAsync(pageModel, orderBy)) { await retourTask; retour = retourTask.Result; } return(retour); }
/// <summary> /// 1- Check for consistency. /// </summary> /// <param name="query">Can not be null</param> /// <param name="pageModel">Can not be null</param> /// <param name="orderBy">Can be null</param> /// <returns></returns> public async Task <PagingParameterOutModel <HRBirdMainOutput> > GetMainRecordsAsync(HRBirdMainInput query, PagingParameterInModel pageModel, HRSortingParamModel orderBy) { //1- if (query == null || pageModel == null) { throw new ArgumentNullException("At least one of arguments is null"); } //2- PagingParameterOutModel <HRBirdMainOutput> retour = new PagingParameterOutModel <HRBirdMainOutput>(); using (Task <IEnumerable <HRBirdMainOutput> > mainRecordsAction = GetMainRecordsWithDapperAsync(query, pageModel, orderBy)) { await mainRecordsAction; if (mainRecordsAction.IsCompleted) { retour.CurrentPage = pageModel.PageNumber; retour.PageSize = pageModel.PageSize; retour.PageItems = mainRecordsAction.Result; using (var countTask = GetMainRecordsCountWithDapperAsync(query, pageModel, orderBy)) { await countTask; if (countTask.IsCompleted) { retour.TotalItemsCount = countTask.Result; } } } else { throw new Exception("Something get wrong in PostGisrepository : GetAsync(IEnumerable<string> borderIDs)"); } } return(retour); }
public void PaginateEmptyListsReturnSinglePagePaginationWithEmptyResult() { HRPaginer <String> paginer = new HRPaginer <String>(); PagingParameterInModel model = new PagingParameterInModel(); model.PageNumber = 0; model.PageSize = 20; List <String> paginerItems = new List <String>(); PagingParameterOutModel <String> retour = paginer.GetPaginationFromFullList(model, paginerItems); Assert.NotNull(retour); Assert.True(retour.TotalItemsCount == 0); Assert.True(retour.TotalPages == 1); Assert.True(retour.PageSize == 20); Assert.NotNull(retour.PageItems); int itemsCount = 0; IEnumerator <String> enumerator = retour.PageItems.GetEnumerator(); while (enumerator.MoveNext()) { itemsCount++; } Assert.True(itemsCount == 0); Assert.False(retour.HasPreviousPage); Assert.False(retour.HasNextPage); Assert.True(retour.CurrentPage == 0); }
/// <summary> /// Apply query orederd and paginated on PostGIS DB. /// </summary> /// <param name="pageModel">the PageModel. Can not be null.</param> /// <param name="orderBy">The order. Can be null, will be set to default to avoid a fuzzy Pagination.</param> /// <returns>The corresponding PagingParameterOutModel</returns> public async Task <PagingParameterOutModel <HRBorder> > GetOrderedAndPaginatedsAsync(PagingParameterInModel pageModel, HRSortingParamModel orderBy) { if (pageModel == null) { throw new ArgumentNullException(); } if (_paginer == null) { throw new MemberAccessException("Paginer can not be null."); } PagingParameterOutModel <HRBorder> retour = null; String cxString = _config.GetConnectionString(CONNECTION_STRING_KEY); cxString = String.Format(cxString, _config[_DBUSER], _config[_DBPASSWORD]); String queryString = GetSQLQuery(true, null, orderBy, pageModel); using (var conn = new NpgsqlConnection(cxString)) { conn.Open(); try { uint totalItemsCount = 0; using (Task <int> totalCountTask = conn.ExecuteScalarAsync <int>("SELECT COUNT(*) FROM boundaries")) { await totalCountTask; if (totalCountTask.Result >= 0 && totalCountTask.Result < Int32.MaxValue) { totalItemsCount = (uint)(totalCountTask.Result); } } if (_paginer.IsValid(pageModel, totalItemsCount)) { using (Task <IEnumerable <HRBorder> > queryTask = conn.QueryAsync <HRBorder>(queryString)) { await queryTask; retour = new PagingParameterOutModel <HRBorder>() { PageItems = queryTask.Result, PageSize = pageModel.PageSize, TotalItemsCount = totalItemsCount, CurrentPage = pageModel.PageNumber }; } } else { throw new IndexOutOfRangeException("Pagination out of existing range."); } } catch (Exception ex) { if (_logger != null) { _logger.Error(ex.Message); } throw; } return(retour); } }
public void PaginateMultiplePageOnLastPageExpectOutModelIsConsistent() { HRPaginer <String> paginer = new HRPaginer <String>(); PagingParameterInModel model = new PagingParameterInModel(); model.PageSize = 50; model.PageNumber = 4; PagingParameterOutModel <String> result = paginer.GetPaginationFromFullList(model, CreateItems(210)); Assert.NotNull(result); Assert.False(result.HasNextPage); Assert.True(result.HasPreviousPage); Assert.True(result.TotalItemsCount == 210); Assert.True(result.CurrentPage == 4); IEnumerable <String> items = result.PageItems; Assert.NotNull(items); Assert.NotEmpty(items); IEnumerator <String> enumerator = items.GetEnumerator(); int i = 200; String valueExceptedi = String.Empty; while (enumerator.MoveNext()) { valueExceptedi = "Items number " + i.ToString(); Assert.Equal(valueExceptedi, enumerator.Current); i++; } Assert.True(i == 210); }
/// <summary> /// Call GetOrderedAndPaginatedsAsync with orederBy set to null. /// </summary> /// <param name="pageModel">the pageModel.</param> /// <returns>The HRBorders corresponding Page</returns> public async Task <PagingParameterOutModel <HRBorder> > GetPaginatedsAsync(PagingParameterInModel pageModel) { PagingParameterOutModel <HRBorder> retour = null; using (Task <PagingParameterOutModel <HRBorder> > retourTask = GetOrderedAndPaginatedsAsync(pageModel, null)) { await retourTask; retour = retourTask.Result; } return(retour); }
public async Task <PagingParameterOutModel <HRCountry> > GetPaginatedsAsync(PagingParameterInModel pageModel) { await Task.Delay(1); PagingParameterOutModel <HRCountry> retour = new PagingParameterOutModel <HRCountry>(); retour.PageItems = _countries; retour.PageSize = 50; retour.CurrentPage = 0; retour.TotalItemsCount = 50; return(retour); }
public async Task <PagingParameterOutModel <HRBorder> > GetPaginatedBordersAsync(PagingParameterInModel pageModel) { await Task.Delay(1); PagingParameterOutModel <HRBorder> retour = new PagingParameterOutModel <HRBorder>() { CurrentPage = pageModel.PageNumber, PageItems = _borders, PageSize = pageModel.PageSize, TotalItemsCount = (uint)_borders.Count }; return(retour); }
public async Task <PagingParameterOutModel <HRBorder> > GetOrderedAndPaginatedBordersAsync(PagingParameterInModel pageModel, HRSortingParamModel orderBy) { orderBy.ToString(); await Task.Delay(1); PagingParameterOutModel <HRBorder> retour = new PagingParameterOutModel <HRBorder>() { CurrentPage = pageModel.PageNumber, PageItems = _borders, PageSize = pageModel.PageSize, TotalItemsCount = (uint)_borders.Count }; return(retour); }
public async Task <PagingParameterOutModel <HRBorder> > GetBordersAsync(PagingParameterInModel pageModel, HRSortingParamModel orderBy) { await Task.Delay(1); if (ThrowException) { throw ExceptionToThrow; } PagingParameterOutModel <HRBorder> retour = new PagingParameterOutModel <HRBorder>() { PageItems = _list, TotalItemsCount = (uint)_list.Count, PageSize = pageModel.PageSize }; return(retour); }
/// <summary> /// Create OutPutPagination from InputPagination. /// </summary> /// <param name="model">The input Model</param> /// <param name="items">The full list of items to paginate</param> /// <returns>ArgumentNullException if input args are null, Exception if input is Invalid else return the PagingParameterOutModel expected. </returns> public PagingParameterOutModel <T> GetPaginationFromFullList(PagingParameterInModel model, IEnumerable <T> items, ushort maxPageSize = ushort.MaxValue) { if (model == null || items == null) { throw new ArgumentNullException(); } else if (!IsValid(model, items)) { throw new Exception("Invalid PagingParameterInModel"); } else { PagingParameterInModel reworkedPagingIn = PagingParameterInLimiter.LimitPagingIn(model, maxPageSize); PagingParameterOutModel <T> retour = new PagingParameterOutModel <T>(); IEnumerator <T> enumerator = items.GetEnumerator(); uint itemsCount = GetEnumerableCount(enumerator); retour.TotalItemsCount = itemsCount; retour.PageSize = reworkedPagingIn.PageSize; List <T> pageItems = new List <T>(); int iterator = 0; int startIndex = reworkedPagingIn.PageSize * (reworkedPagingIn.PageNumber); int endIndex = startIndex + reworkedPagingIn.PageSize; enumerator.Reset(); while (enumerator.MoveNext()) { if (iterator >= endIndex) { break; } else if (iterator < endIndex && iterator >= startIndex) { pageItems.Add(enumerator.Current); } iterator++; } retour.PageItems = pageItems; retour.CurrentPage = reworkedPagingIn.PageNumber; return(retour); } }
/// <summary> /// Method used to paginate results. The query in can be ordered or not. /// 1- Check that context is consistant before processing. /// 1.1- Internal member /// 1.2- Input parameters /// 2- Process query on repository /// 2.1- if OrderBy is supplied /// 2.1.1- Is repository is sortable /// 2.1.1.1- If repository IsPaginable : Full Query Repository for Pagination and Order and return result /// 2.1.1.2- Else : Process partial query on ordering repository capacity for internal pagination /// 2.1.2- Else throw NotSupportedException /// 2.2- else /// 2.2.1- If repository is Paginable, Full Query and return result. /// 2.2.2- else Partial repository Query for internal Pagination /// 3- This step means that internal Pagination is needed /// 3-1- Check that Pagination is valid /// 3.2- Process pagination and return. /// </summary> /// <param name="pageModel">The Input Pagination, can not be null.</param> /// <param name="orderBy">The ordering query. Can be null.</param> /// <returns>The expected results.Can throw MemberAccessException, ArgumentNullException, NotSupportedException and InvalidOperationException.</returns> public async Task <PagingParameterOutModel <T> > GetQueryResultsAsync(PagingParameterInModel pageModel, HRSortingParamModel orderBy) { //1.1- if (_repository == null || _paginer == null) { throw new MemberAccessException(); } //1.2- if (pageModel == null) { throw new ArgumentNullException(); } //2- IEnumerable <T> internalPagination = null; //2.1- if (orderBy != null && orderBy.IsInitialised()) { //2.1.1- if (_repository.IsSortable()) { //2.1.1.1- if (_repository.IsPaginable()) { PagingParameterOutModel <T> retourPaginable = null; using (Task <PagingParameterOutModel <T> > itemsTask = _repository.GetOrderedAndPaginatedsAsync(pageModel, orderBy)) { await itemsTask; retourPaginable = itemsTask.Result; } return(retourPaginable); } //2.1.1.2- else { using (Task <IEnumerable <T> > taskForInternalPagination = _repository.GetOrderedsAsync(orderBy)) { await taskForInternalPagination; internalPagination = taskForInternalPagination.Result; } } } //2.1.2- else { throw new NotSupportedException("Linq orderBy is not yet implemented."); } } //2.2- else { //2.2.1- if (_repository.IsPaginable()) { PagingParameterOutModel <T> retourPaginable = null; using (Task <PagingParameterOutModel <T> > bordersTask = _repository.GetPaginatedsAsync(pageModel)) { await bordersTask; retourPaginable = bordersTask.Result; } return(retourPaginable); } //2.2.2- else { using (Task <IEnumerable <T> > taskForInternalPagination = _repository.GetFullsAsync()) { await taskForInternalPagination; internalPagination = taskForInternalPagination.Result; } } } //3- //3.1- if (!_paginer.IsValid(pageModel, internalPagination)) { throw new InvalidProgramException(); } //3.2- else { return(_paginer.GetPaginationFromFullList(pageModel, internalPagination, _maxPageSize)); } }