public CatalogViewModel GetItems(int?currentPage, int?itemsPerPage, string currentBrand, SortBy?sortBy)
        {
            var filter = new FilterViewModel
            {
                FilterByBrand = new FilterByBrandViewModel
                {
                    Brands       = GetBrands(),
                    CurrentBrand = GetBrands().Contains(currentBrand) ? currentBrand : "All"
                },
                SortingBy = sortBy
            };

            var catalogFilter = new CatalogFilterViewModel
            {
                Filter     = filter,
                Pagination = new PaginationViewModel
                {
                    CurrentPage  = currentPage ?? 1,
                    ItemsPerPage = itemsPerPage ?? 4,
                    TotalItems   = GetTotalItems(filter)
                }
            };

            var catalogItems = GetCatalogItems(catalogFilter);

            var viewmodel = new CatalogViewModel
            {
                CatalogItems = catalogItems,
                Filter       = catalogFilter
            };

            return(viewmodel);
        }
        public CatalogViewModel GetCatalogModel()
        {
            var catalogFilter = new CatalogFilterViewModel
            {
                Filter = new FilterViewModel
                {
                    FilterByBrand = new FilterByBrandViewModel
                    {
                        Brands       = GetBrands(),
                        CurrentBrand = "All"
                    },
                    SortingBy = SortBy.None
                },
                Pagination = new PaginationViewModel
                {
                    CurrentPage  = 1,
                    ItemsPerPage = 4,
                    TotalItems   = _catalogService.GetAll().Count()
                }
            };
            var catalogItems = GetCatalogItems(catalogFilter);

            var viewmodel = new CatalogViewModel
            {
                CatalogItems = catalogItems,
                Filter       = catalogFilter
            };

            return(viewmodel);
        }
Ejemplo n.º 3
0
        public async Task <CatalogIndexViewModel> GetCatalogItems(CatalogFilterViewModel model)
        {
            string cacheKey = String.Format(_itemsKeyTemplate, model.PaginationInfo.ActualPage, model.PaginationInfo.ItemsPerPage, model.BrandFilterApplied, model.TypesFilterApplied, model.TextSearch);

            return(await _cache.GetOrCreateAsync(cacheKey, async entry =>
            {
                entry.SlidingExpiration = _defaultCacheDuration;
                return await _catalogService.GetCatalogItems(model);
            }));
        }
        public CatalogViewModel GetItemsByFilter(CatalogFilterViewModel catalogFilter)
        {
            var viewmodel = new CatalogViewModel
            {
                CatalogItems = GetCatalogItems(catalogFilter),
                Filter       = catalogFilter
            };

            return(viewmodel);
        }
        public async Task <CatalogIndexViewModel> GetCatalogItems(CatalogFilterViewModel model)
        {
            CatalogFilterSpecification filterSpecification = new CatalogFilterSpecification(model.BrandFilterApplied, model.TypesFilterApplied, model.TextSearch);
            var root = _itemRepository.List(filterSpecification);

            var totalItems = root.Count();

            var itemsOnPage = root
                              .Skip(model.PaginationInfo.ItemsPerPage * model.PaginationInfo.ActualPage)
                              .Take(model.PaginationInfo.ItemsPerPage)
                              .ToList();

            itemsOnPage.ForEach(x =>
            {
                x.PictureUri = _uriComposer.ComposePicUri(x.PictureUri);
            });

            CatalogIndexViewModel vm = new CatalogIndexViewModel()
            {
                CatalogItems = itemsOnPage.Select(i => new CatalogItemViewModel()
                {
                    Id         = i.Id,
                    Name       = i.Name,
                    PictureUri = i.PictureUri,
                    Price      = i.Price
                }),
                Brands = await GetBrands(),
                Types  = await GetTypes()
            };

            vm.CatalogFilter.BrandFilterApplied = model.BrandFilterApplied ?? 0;
            vm.CatalogFilter.TypesFilterApplied = model.TypesFilterApplied ?? 0;
            vm.CatalogFilter.PaginationInfo     = new PaginationInfoViewModel()
            {
                ActualPage   = model.PaginationInfo.ActualPage,
                ItemsPerPage = itemsOnPage.Count,
                TotalItems   = totalItems,
                TotalPages   = int.Parse(Math.Ceiling(((decimal)totalItems / model.PaginationInfo.ItemsPerPage)).ToString())
            };
            vm.CatalogFilter.PaginationInfo.Next     = (vm.CatalogFilter.PaginationInfo.ActualPage == vm.CatalogFilter.PaginationInfo.TotalPages - 1) ? "is-disabled" : "";
            vm.CatalogFilter.PaginationInfo.Previous = (vm.CatalogFilter.PaginationInfo.ActualPage == 0) ? "is-disabled" : "";
            vm.CatalogFilter.TextSearch = model.TextSearch ?? string.Empty;

            return(vm);
        }
Ejemplo n.º 6
0
 public IViewComponentResult Invoke(CatalogFilterViewModel model) =>
 View(model);
 private IEnumerable <CatalogItemViewModel> GetCatalogItems(CatalogFilterViewModel catalogFilter)
 => _catalogService.GetAll()
 .BrandFilter(catalogFilter.Filter.FilterByBrand.CurrentBrand)
 .SortByFilter(catalogFilter.Filter.SortingBy)
 .PageFilter(catalogFilter.Pagination)
 .Select(ci => CreateCatalogItemVM(ci));