Exemplo n.º 1
0
        /// <summary>
        /// Gets the pagination token
        /// </summary>
        /// <param name="criteria">Criteria of the pagination token</param>
        /// <param name="currentPage">Current page</param>
        /// <returns>Pagination token or null if there is no pagination</returns>
        protected string GetPaginationToken(FetchCriteria <TModel> criteria, int currentPage = 0)
        {
            var pageSize = criteria?.PageSize;

            if (!pageSize.HasValue || pageSize.Value == 0)
            {
                return(null);
            }

            return(PaginationTokenManager.CreateToken(new PaginationTokenInfo <TModel> {
                Criteria = criteria,
                CurrentPage = currentPage
            }));
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public virtual Task <FetchResult <TModel> > FetchNext(string paginationToken)
        {
            if (string.IsNullOrEmpty(paginationToken))
            {
                throw new ArgumentNullException(nameof(paginationToken));
            }

            var paginationInfo = (PaginationTokenInfo <TModel>)PaginationTokenManager.DecodeToken(paginationToken);

            if (paginationInfo == null)
            {
                throw new ApplicationException($"Pagination token is not valid. Current value is '{paginationToken}'");
            }

            Logger.LogDebug("Loading next page for file {FileName}, pagination token {PaginationToken}", FileName, paginationToken);

            var criteria = paginationInfo.Criteria;

            var items = FetchAll();
            var count = items.Count;

            items = ApplyCriteria(items, criteria);
            var criteriaCount = items.Count;

            var currentPage = paginationInfo.CurrentPage.GetValueOrDefault() + 1;

            items = ApplyPagination(items, criteria.PageSize, currentPage);
            var paginatedCount = items.Count;

            var token = GetPaginationToken(criteria, currentPage);

            Logger.LogDebug("Read {RecordCount} records, where {CriteriaCount} match criteria, returned {PaginatedCount} after pagination, " +
                            "new pagination token {Token}",
                            count, criteriaCount, paginatedCount, token);

            return(Task.FromResult(new FetchResult <TModel> (items, token)));
        }