Esempio n. 1
0
        public StoreAppResult GetApps(Guid?categoryId = null, int pageSize = 60, int page = 1)
        {
            IList <StoreApp> apps = new List <StoreApp>();
            int    count          = 0;
            string categoryStr    = string.Empty;

            if (categoryId.HasValue && categoryId.Value != Guid.Empty)
            {
                count = _context.StoreApp.Include(m => m.Categories).Where(m => m.Categories.Id == categoryId.Value).Count();

                var category = _context.StoreCategories.FirstOrDefault(m => m.Id == categoryId);

                if (category != null)
                {
                    categoryStr = category.Title;
                }

                // Fix random SQL Errors due to bad offset
                if (page < 1)
                {
                    page = 1;
                }
                if (pageSize > 100)
                {
                    pageSize = 100;
                }
                apps = _context.StoreApp.Include(m => m.Categories).Where(m => m.Categories.Id == categoryId.Value)
                       .OrderByDescending(s => s.AppName)
                       .Skip(pageSize * (page - 1))
                       .Take(pageSize)
                       .ToList();
            }
            else
            {
                count = _context.StoreApp.Count();
                if (page < 1)
                {
                    page = 1;
                }
                if (pageSize > 100)
                {
                    pageSize = 100;
                }
                apps = _context.StoreApp
                       .OrderByDescending(s => s.AppName)
                       .Skip(pageSize * (page - 1))
                       .Take(pageSize)
                       .ToList();
            }
            var result = new StoreAppResult()
            {
                CurrentPage  = page,
                TotalResults = count,
                TotalPages   = CalculatePages(count, pageSize),
                Apps         = apps,
                Category     = categoryStr
            };

            return(result);
        }
Esempio n. 2
0
        public IActionResult Pager(Guid id, int page)
        {
            StoreListViewModel vm = new StoreListViewModel();
            var    category_cacheKey = $"Store_Pager_Category";
            var    app_cacheKey = $"Store_Pager_{id}_{page}";
            string category_cached, app_cached;
            IList <StoreCategories> categories = null;
            StoreAppResult          apps       = null;

            if (!_memoryCache.TryGetValue(category_cacheKey, out category_cached))
            {
                categories      = _storeRepository.GetCategories();
                category_cached = JsonConvert.SerializeObject(categories);
                _memoryCache.Set(category_cacheKey, category_cached, new MemoryCacheEntryOptions()
                {
                    SlidingExpiration = TimeSpan.FromHours(6)
                });
            }
            else
            {
                try
                {
                    categories = JsonConvert.DeserializeObject <IList <StoreCategories> >(category_cached);
                }
                catch
                {
                    categories = _storeRepository.GetCategories();
                }
            }
            if (!_memoryCache.TryGetValue(app_cacheKey, out app_cached))
            {
                apps       = _storeRepository.GetApps(id, _pageSize, page);
                app_cached = JsonConvert.SerializeObject(categories);
                _memoryCache.Set(category_cacheKey, app_cached, new MemoryCacheEntryOptions()
                {
                    SlidingExpiration = TimeSpan.FromHours(6)
                });
            }
            else
            {
                try
                {
                    apps = JsonConvert.DeserializeObject <StoreAppResult>(app_cached);
                }
                catch
                {
                    apps = _storeRepository.GetApps(id, _pageSize, page);
                }
            }
            vm.StoreCategories = categories;
            vm.AppResult       = apps;
            return(View("Index", vm));
        }