Exemplo n.º 1
0
        private IQueryable <Product> FilterProduct(IQueryable <Product> products, GetProductSearchQuery query)
        {
            if (!string.IsNullOrEmpty(query.CategoryId))
            {
                if (query.CategoryType == "main")
                {
                    products = products.Where(x => x.MainCategoryId == query.CategoryId);
                }
                else if (query.CategoryType == "sub")
                {
                    products = products.Where(x => x.SubCategoryId == query.CategoryId);
                }
                else if (query.CategoryType == "tri")
                {
                    products = products.Where(x => x.TertiaryCategoryId == query.CategoryId);
                }
            }

            if (!string.IsNullOrEmpty(query.SearchString))
            {
                products = products.Where(x => EF.Functions.Like(x.SearchString, $"%{query.SearchString}%"));
            }

            return(products);
        }
Exemplo n.º 2
0
        public IEnumerable <ProductViewModel> Do(GetProductSearchQuery query)
        {
            var discounts = _ctx.AccountUsers
                            .Include(x => x.Account)
                            .ThenInclude(x => x.Discounts)
                            .FirstOrDefault(x => x.UserId == query.UserId && x.Active)?
                            .Account?
                            .Discounts;

            var realProducts = _ctx.Products.Include(x => x.TertiaryCategory).Where(x => x.Published != false);

            var actualRealProducts = FilterProduct(realProducts, query).ToList();

            var products = actualRealProducts.Select(x => new ProductViewModel
            {
                Id                 = x.Id,
                Name               = x.Name,
                Description        = x.Description,
                Price              = GetPrice(x, null),
                PriceDec           = x.Price ?? 0m,
                Discount           = discounts?.Any(y => y.ProductId == x.Id) ?? false,
                DiscountedValue    = GetPrice(x, discounts),
                ImageUrl           = x.ImageUrl,
                UOM                = x.Unit,
                Colour             = x.Colour,
                Code               = x.ExternalId,
                TertiaryCategory   = x.TertiaryCategory.Name,
                TertiaryCategoryId = x.TertiaryCategoryId,
                ValueAdded         = x.ValueAddedProduct,
                OutOfStock         = x.OutOfStock,
                brand              = x.Brand
            }).OrderBy(x => x.Name);

            return(products);
        }