Esempio n. 1
0
        public async Task <List <BasketItemViewModel> > GetBasketItems(IReadOnlyCollection <BasketItem> basketItems)
        {
            var catalogItemsSpecification = new CatalogItemsSpecification(basketItems.Select(b => b.CatalogItemId).ToArray());
            var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification);

            var items = basketItems.Select(basketItem =>
            {
                var catalogItem = catalogItems.FirstOrDefault(c => c.id == basketItem.CatalogItemId);

                //if basketItem.CatalogItemId == catalogItem.id make a basketViewModel out of both and return it

                var basketItemViewModel = new BasketItemViewModel
                {
                    Id            = basketItem.id,
                    CatalogItemId = basketItem.CatalogItemId,
                    ProductName   = catalogItem.Name,
                    Quantity      = basketItem.Quantity,
                    UnitPrice     = basketItem.UnitPrice,
                    PictureUri    = _uriComposer.ComposePictureUri(catalogItem.PictureUri)
                };
                return(basketItemViewModel);
            }).ToList();

            return(items);
        }
Esempio n. 2
0
        public async Task <CatalogIndexViewModel> GetCatalogItems(int pageIndex, int itemsPage, int?brandId, int?typeId)
        { // pageIndex is current page
            _logger.LogInformation("GetCatalogItems called");

            var filterSpecification        = new CatalogFilterSpecification(brandId, typeId);                                            // this contains all catalogitems with matching brand&type ids
            var filterPaginatedSpecifition = new CatalogFilterPaginatedSpecification(itemsPage * pageIndex, itemsPage, brandId, typeId); // this contains all catalogItem per page
            var itemsPerPage = await _itemRepository.ListAsync(filterPaginatedSpecifition);

            var totalItems = await _itemRepository.CountAsync(filterSpecification);

            var vm = new CatalogIndexViewModel()
            {
                catalogItems = itemsPerPage.Select(i => new CatalogItemViewModel()
                {
                    id         = i.id,
                    name       = i.Name,
                    pictureUri = _uriComposer.ComposePictureUri(i.PictureUri),
                    price      = i.Price
                }).ToList(),
                //for List<SelectListItem> brands
                brands             = (List <SelectListItem>) await GetBrands(), // this is an explicit cast
                types              = (List <SelectListItem>) await GetTypes(),
                brandFilterApplied = brandId ?? 0,                              // if brandId not null pass it, else pass 0
                typeFilterApplied  = typeId ?? 0,

                paginationInfo = new PaginationInfoViewModel()
                {
                    totalItems   = totalItems,
                    itemsPerPage = itemsPerPage.Count,
                    actualPage   = pageIndex,
                    totalPages   = int.Parse(Math.Ceiling((decimal)totalItems / itemsPerPage.Count).ToString())
                                   // string next and previous are defined seperately below
                }
            };

            // "is-disabled" will be render into HTML that disables next/previous button
            vm.paginationInfo.next     = (vm.paginationInfo.actualPage == vm.paginationInfo.totalPages - 1)  ?  "is-disabled" : "";
            vm.paginationInfo.previous = (vm.paginationInfo.actualPage == 0) ? "is-disabled" : "";

            return(vm);
        }