Exemplo n.º 1
0
        public IActionResult Shop(int?sectionId, int?brandId, int page = 1)
        {
            int?pageSize = int.TryParse(_configuration["PageSize"], out int page_size) ? page_size : (int?)null;

            PageProductsDTO products = _catalogData.GetProducts(new ProductFilter
            {
                BrandId   = brandId,
                SectionId = sectionId,
                Page      = page,
                PageSize  = pageSize
            });

            CatalogViewModel catalogViewModel = new CatalogViewModel
            {
                BrandId       = brandId,
                SectionId     = sectionId,
                Products      = products.Products.FromDTO().ToView(),
                PageViewModel = new PageViewModel()
                {
                    PageNumber = page,
                    TotalItems = products.TotalCount,
                    PageSize   = pageSize.GetValueOrDefault()
                }
            };

            return(View(catalogViewModel));
        }
Exemplo n.º 2
0
        public PageProductsDTO GetProducts(ProductFilter Filter = null)
        {
            IQueryable <Product> query = _db.Products;

            if (Filter?.BrandId != null)
            {
                query = query.Where(product => product.BrandId == Filter.BrandId);
            }

            if (Filter?.SectionId != null)
            {
                query = query.Where(product => product.SectionId == Filter.SectionId);
            }

            if (Filter?.Ids?.Count > 0)
            {
                query = query.Where(product => Filter.Ids.Contains(product.Id));
            }

            var totalCount = query.Count();

            if (Filter.PageSize.HasValue)
            {
                query = query.Skip((Filter.Page - 1) * Filter.PageSize.Value)
                        .Take(Filter.PageSize.Value);
            }

            var res = new PageProductsDTO
            {
                Products   = query.Select(_Mapper.Map <ProductDTO>).AsEnumerable(),
                TotalCount = totalCount
            };

            return(res);
        }
Exemplo n.º 3
0
        public void CartServiceTransformFromCart()
        {
            var cart = new Cart
            {
                Items = new List <CartItem>
                {
                    { new CartItem {
                          ProductId = 1, Quantity = 1
                      } },
                    { new CartItem {
                          ProductId = 2, Quantity = 5
                      } }
                }
            };

            var products = new PageProductsDTO
            {
                Products = new List <ProductDTO> {
                    {
                        new ProductDTO
                        {
                            Id       = 1,
                            Name     = "Product 1",
                            ImageUrl = "Product1.png",
                            Order    = 0,
                            Price    = 1.1m,
                            Brand    = new BrandDTO
                            {
                                Id   = 1,
                                Name = "Brand of product 1"
                            },
                            Section = new SectionDTO
                            {
                                Id   = 1,
                                Name = "Section of product 1"
                            }
                        }
                    },
                    {
                        new ProductDTO
                        {
                            Id       = 2,
                            Name     = "Product 2",
                            ImageUrl = "Product2.png",
                            Order    = 0,
                            Price    = 2.2m,
                            Brand    = new BrandDTO
                            {
                                Id   = 2,
                                Name = "Brand of product 2"
                            },
                            Section = new SectionDTO
                            {
                                Id   = 2,
                                Name = "Section of product 2"
                            }
                        }
                    }
                },
                TotalCount = 2
            };

            var productDataMock = new Mock <IProductData>();
            var cartStoreMock   = new Mock <ICartStore>();

            productDataMock
            .Setup(c => c.GetProducts(It.IsAny <ProductFilter>()))
            .Returns(products);

            cartStoreMock
            .Setup(c => c.Cart)
            .Returns(cart);

            var cartService = new CartService(productDataMock.Object, cartStoreMock.Object);

            var res = cartService.TransformFromCart();

            Assert.Equal(6, cart.ItemsCount);
            Assert.Equal(1.1m, res.Items.First().Key.Price);
        }