Exemplo n.º 1
0
        private Dictionary <string, string> GetSortQueryParams(SortParams sortParams)
        {
            if (sortParams.IsNotNull())
            {
                return(new Dictionary <string, string>
                {
                    { nameof(SortParams.ColumnNo), $"{ sortParams.ColumnNo}" },
                    { nameof(SortParams.Direction), $"{ sortParams.ColumnNo}" },
                });
            }


            return(new Dictionary <string, string>());
        }
Exemplo n.º 2
0
        private void BuildUrl(long total, PaginationParams paginationParams, SortParams sortParams, IEnumerable <TData> data)
        {
            Uri baseUri = new Uri(paginationParams.Host);
            Uri uri     = new Uri(baseUri, paginationParams.Path);

            var lastPage = (int)Math.Ceiling(total / (decimal)paginationParams.PerPage);

            if (paginationParams.Page + 1 <= lastPage)
            {
                var queryNext = GetSortQueryParams(sortParams);
                NextPageUrl = QueryHelpers.AddQueryString(uri.ToString(), queryNext);
            }

            if (paginationParams.Page - 1 >= 1)
            {
                var queryPrevious = GetSortQueryParams(sortParams);
                PreviousPageUrl = QueryHelpers.AddQueryString(uri.ToString(), queryPrevious);
            }
        }
Exemplo n.º 3
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            StringValues sorting;

            bindingContext.HttpContext.Request.Query.TryGetValue("sort", out sorting);

            int?          l_columnNo  = null;
            SortDirection?e_direction = null;

            if (sorting.Count > 0)
            {
                var split = sorting[0].Split('|');
                l_columnNo  = split.ElementAtOrDefault(1) != null ? split[0].ToN <int>() : null;
                e_direction = split.ElementAtOrDefault(1) != null ? split[1].ToEnum <SortDirection>() : null;
            }

            var result = new SortParams()
            {
                ColumnNo  = l_columnNo ?? 0,
                Direction = e_direction ?? SortDirection.DESC
            };

            var validationResult = new SortParamsValidator().Validate(result);

            if (validationResult.IsValid)
            {
                bindingContext.Result = ModelBindingResult.Success(result);
            }
            else
            {
                bindingContext.Result = ModelBindingResult.Failed();
                throw new ValidationException(validationResult.Errors);
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 4
0
 public PaginatedResults(long total, PaginationParams paginationParams, SortParams sortParams, IEnumerable <TData> data)
 {
     BuildUrl(total, paginationParams, sortParams, data);
     CalculateProperties(total, paginationParams);
     Data = data.ToList();
 }