public IActionResult Get(int page = 1, int pageSize = 20)
        {
            if (!string.IsNullOrEmpty(_httpContextAccessor.HttpContext.Request.Cookies["CookieMonster"]))
            {
                _logger.LogInformation(_httpContextAccessor.HttpContext.Request.Cookies["CookieMonster"]);
            }

            var rng   = new Random();
            var items = Enumerable.Range(0, 5000).Select(index => new WeatherForecast
            {
                Index        = index,
                Date         = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary      = Summaries[rng.Next(Summaries.Length)]
            })
                        .ToList();

            CookieOptions option = new CookieOptions();

            option.Expires  = DateTime.Now.AddDays(10);
            option.HttpOnly = true;
            option.Domain   = "/";
            _httpContextAccessor.HttpContext.Response.Cookies.Append("CoookieMonster", "test_test_test", option);

            var pager = new PaginationBuilder(items.Count, page, pageSize);

            var results = new { pager, data = items.Skip(pager.StartIndex).Take(pager.PageSize) };

            return(Ok(new JsonResult(results).Value));
        }
예제 #2
0
        public static IHtmlContent Pager <T>(this IHtmlHelper helper, PaginationBuilder <T> builder) where T : class
        {
            if (helper is null)
            {
                throw new ArgumentNullException(nameof(helper));
            }


            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (builder.Collection.TotalRecords < builder.Collection.PageSize &&
                builder.Collection.IsNumberPagination && builder.Collection.Page != null && int.Parse(
                    builder.Collection.Page,
                    CultureInfo.InvariantCulture) <= 1)
            {
                return(new HtmlString(string.Empty));
            }

            var html = new StringBuilder();

            html.Append(builder.BuildHeader());

            if (builder.Collection.HasPrevious())
            {
                html.Append(builder.BuildPrevious());
            }

            if (builder.Collection.PageCount > 1)
            {
                for (var i = 0; i <= builder.Collection.PageCount - 1; i++)
                {
                    var isCurrent = builder.Collection.Page == i.ToString(CultureInfo.InvariantCulture);

                    html.Append(builder.BuildItem(i, isCurrent));
                }
            }

            if (builder.Collection.HasNext())
            {
                html.Append(builder.BuildNext());
            }

            html.Append(builder.BuildFooter());

            html.Append(builder.BuildPagerScript());

            return(new HtmlString(html.ToString()));
        }
        PaginationBuilder <TSource> BuildQueryPaginationField <TSource, TReturn>(
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IQueryable <TReturn> >?resolve,
            int page,
            int row,
            Type?itemGraphType,
            string?description)
            where TReturn : class
        {
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            Guard.AgainstNegative(nameof(page), page);
            Guard.AgainstNegative(nameof(row), row);

            itemGraphType ??= GraphTypeFinder.FindGraphType <TReturn>();
            var fieldType = GetPaginationFieldType <TSource>(name, itemGraphType);

            var builder = PaginationBuilder <TSource> .Create <FakeGraph>(name);

            if (description != null)
            {
                builder.Description(description);
            }
            builder.Page(page).Row(row);
            SetField(builder, fieldType);

            if (resolve != null)
            {
                builder.Resolve(
                    context =>
                {
                    var efFieldContext = BuildContext(context);
                    var query          = resolve(efFieldContext);
                    query     = includeAppender.AddIncludes(query, context);
                    var names = GetKeyNames <TReturn>();
                    query     = query.ApplyGraphQlArguments(context, names);
                    return(query
                           .ApplyPaginationContext(
                               context.Page,
                               context.Row,
                               context,
                               context.CancellationToken,
                               efFieldContext.Filters));
                });
            }

            return(builder);
        }