Пример #1
0
        //internal Settings Settings
        //{
        //    get
        //    {
        //        if (_settings == null) _settings = new Settings(db);
        //        return _settings;
        //    }
        //}
        //internal User CurrentUser
        //{
        //    get
        //    {
        //        if (_currentUser == null) _currentUser = UserManager.FindByName(User.Identity.Name);
        //        return _currentUser;
        //    }
        //}
        //internal ModelFactory ModelFactory
        //{
        //    get
        //    {
        //        if (_modelFactory == null) _modelFactory = new ModelFactory();
        //        return _modelFactory;
        //    }
        //}
        //internal AppUserManager UserManager
        //{
        //    get
        //    {
        //        if (_userManager == null) _userManager = HttpContext.Current.GetOwinContext().GetUserManager<AppUserManager>();
        //        return _userManager;
        //    }
        //}
        //private Settings _settings;
        //private User _currentUser;
        //private ModelFactory _modelFactory;
        //private AppUserManager _userManager;

        //internal bool CurrentUserIsInRole(Roles role)
        //{
        //    return UserManager.IsInRole(CurrentUser.Id, role.ToString());
        //}

        //public BaseApiController() : base()
        //{
        //    // added this to force the user to load before any other calls, else there were errors that an async call was already being made...
        //    _currentUser = UserManager.FindByName(User.Identity.Name);
        //}

        protected async Task <List <T> > GetPaginatedResponse <T>(IQueryable <T> query, PagingOptions pagingOptions)
        {
            if (pagingOptions == null)
            {
                pagingOptions = new PagingOptions();
            }
            if (pagingOptions.PageIndex < 0)
            {
                pagingOptions.PageIndex = 0;
            }

            var totalRecords = query.Count();
            var totalPages   = (int)Math.Ceiling((double)totalRecords / pagingOptions.PageSize);

            var results = await(pagingOptions.PageSize <= 0
                ? query.ToListAsync()
                : query.Skip(pagingOptions.PageSize * pagingOptions.PageIndex)
                                .Take(pagingOptions.PageSize)
                                .ToListAsync());

            var paginationHeader = new
            {
                pageIndex    = pagingOptions.PageIndex,
                pageSize     = pagingOptions.PageSize,
                records      = results.Count,
                totalRecords = totalRecords,
                totalPages   = totalPages,
                first        = pagingOptions.PageIndex * pagingOptions.PageSize
            };

            HttpContext.Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));

            return(results);
        }
Пример #2
0
        protected async Task <List <T> > GetPaginatedResponse <T>(IQueryable <T> query, PagingOptions pagingOptions)
        {
            if (pagingOptions == null)
            {
                pagingOptions = new PagingOptions();
            }

            var totalRecords = query.Count();
            var totalPages   = (int)Math.Ceiling((double)totalRecords / pagingOptions.PageSize);

            //var prevLink = pagingOptions.PageIndex > 0 ? Url.Link("DefaultApi", new { pagingOptions.PageSize, page = pagingOptions.PageIndex - 1 }) : "";
            //var nextLink = pagingOptions.PageIndex < totalPages - 1 ? Url.Link("DefaultApi", new { pagingOptions.PageSize, page = pagingOptions.PageIndex + 1 }) : "";
            //var firstLink = Url.Link("DefaultApi", new { pagingOptions.PageSize, page = 0 });
            //var lastLink = Url.Link("DefaultApi", new { pagingOptions.PageSize, page = totalPages - 1 });

            pagingOptions.PageIndex = pagingOptions.PageIndex < 0 ? 0 : pagingOptions.PageIndex;

            var results = await(pagingOptions.PageSize <= 0
                ? query.ToListAsync()
                : query.Skip(pagingOptions.PageSize * pagingOptions.PageIndex)
                                .Take(pagingOptions.PageSize)
                                .ToListAsync());

            var paginationHeader = new
            {
                pageIndex    = pagingOptions.PageIndex,
                pageSize     = pagingOptions.PageSize,
                records      = results.Count,
                totalRecords = totalRecords,
                totalPages   = totalPages //,
                                          //FirstPageLink = firstLink,
                                          //PrevPageLink = prevLink,
                                          //NextPageLink = nextLink,
                                          //LastPageLink = lastLink
            };

            HttpContext.Current.Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));

            return(results);
        }