/// <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); } }
/// <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)); } }