예제 #1
0
        public ProductPagingSpecification(ProductSpecParams specParams)
        {
            if (specParams.ProductTypeId.HasValue)
            {
                base.AddCreteria(p => p.ProductTypeId == specParams.ProductTypeId);
            }

            if (specParams.ProductBrandId.HasValue)
            {
                base.AddCreteria(p => p.ProductBrandId == specParams.ProductBrandId);
            }

            if (!string.IsNullOrEmpty(specParams.Search))
            {
                base.AddCreteria(p => p.Name.ToLower().Contains(specParams.Search));
            }
        }
예제 #2
0
        private void ApplyCreterias(ProductSpecParams spec)
        {
            if (spec.ProductBrandId.HasValue)
            {
                base.AddCreteria(p => p.ProductBrandId == spec.ProductBrandId);
            }

            if (spec.ProductTypeId.HasValue)
            {
                base.AddCreteria(p => p.ProductTypeId == spec.ProductTypeId);
            }

            if (!string.IsNullOrEmpty(spec.Search))
            {
                base.AddCreteria(p => p.Name.ToLower().Contains(spec.Search));
            }
        }
        // //parameter less constructor
        // public ProductsWithTypesAndBrandsSpecification(string sort)
        // {
        //     //use the AddInclude function in the BaseSpecification class to add the following 2 lambda expressions in the "Includes" list of expressions
        //     AddInclude(x => x.ProductType);
        //     AddInclude(x => x.ProductBrand);
        // }


        //re-write the above parameter-less constructor to be a constructor with parameters when for adding the sorting functionality and filtering:
        //the int? means that the int can be null and it is completely optional to pass avalue to it or no !
        //that because we may filter based on brand only or type only or both of them !
        //make the constructor with the :base() which means use also the derived (inherited) BaseSpecification class constructor
        //the BaseSpecification class constructor, if you take a look, has one parameter which sets the BaseSpecification "Criteria" attribute
        //the "Criteria" attribute was mainly built to have an expression such as x => x.Id when quering a specific product for example in the second constructor at the end of this file below
        //but since it is goingto be constructed in a .Where() lambda expression in the SpecificationEvaluator in Infrastructure project Data folder,
        //so we can use it as well to write an expression like: x => x.brandId and x => x.typeId or any one of these expressions alone
        //so do that in the :base() part to pass the criteria we want.
        //and for search part as well.
        //instead of:
        //public ProductsWithTypesAndBrandsSpecification(string sort, int? brandId, int? typeId, .....)
        //:base (x => (!brandId.HasValue || x.ProductBrandId == brandId) && (!typeId.HasValue || x.ProductTypeId == typeId))
        //we created a class ProductSpecParams where we store the parameters related to GetProducts() function in the ProductController
        //so use it here:
        public ProductsWithTypesAndBrandsSpecification(ProductSpecParams ProductParams)
            : base(x => (string.IsNullOrEmpty(ProductParams.Search) || x.Name.ToLower().Contains(ProductParams.Search)) &&
                   (!ProductParams.TypeId.HasValue || x.ProductTypeId == ProductParams.TypeId) && //it means, if the typeId has a value, example TypeId=2, the !HasValue returns false, so implement what is on the left of "elsewhere ||" (easier way other than using if)
                   (!ProductParams.BrandId.HasValue || x.ProductBrandId == ProductParams.BrandId))
        {
            //use the AddInclude function in the BaseSpecification class to add the following 2 lambda expressions in the "Includes" list of expressions
            AddInclude(x => x.ProductType);
            AddInclude(x => x.ProductBrand);



            //this is added to apply the sorting functionality
            //besides the above parameter string sort
            AddOrderBy(x => x.Name);
            if (!string.IsNullOrEmpty(ProductParams.sort))
            {
                switch (ProductParams.sort)
                {
                case "priceAsc": AddOrderBy(x => x.Price);
                    break;

                case "priceDesc": AddOrderByDescending(x => x.Price);
                    break;

                case "nameDesc": AddOrderByDescending(x => x.Name);
                    break;

                default: AddOrderBy(x => x.Name);
                    break;
                }
            }


            //this is added to paginate the Products returned into pages (6 Products per page for example)
            ApplyPaging(ProductParams.PageSize * (ProductParams.PageIndex - 1), ProductParams.PageSize);
            //remember that ApplyPaging(skip, take) is in BaseSpecification class (ISpecification interface implementation)
            //the -1 is requiered for the case of PageIndex = 1,
            //if it was not exxisted, we will have skip=5 and take=5 so we will get nothing !
            //with -1, and PageIndex=1, we will skip=0 and take=5
        }
예제 #4
0
        public ProductsWithTypesAndBrandsSpecifications(ProductSpecParams productSpecParams)
            : base(x =>
                   (string.IsNullOrEmpty(productSpecParams.Search) || x.Name.ToLower().Contains(productSpecParams.Search)) &&
                   (!productSpecParams.BrandId.HasValue || x.ProductBrandId == productSpecParams.BrandId) &&
                   (!productSpecParams.TypeId.HasValue || x.ProductTypeId == productSpecParams.TypeId))
        {
            AddInclude(x => x.ProductType);
            AddInclude(x => x.ProductBrand);
            AddOrderBy(x => x.Name);
            ApplyPaging(productSpecParams.PageSize * (productSpecParams.PageIndex - 1), productSpecParams.PageSize);
            if (string.IsNullOrEmpty(productSpecParams.Sort) == false)
            {
                switch (productSpecParams.Sort)
                {
                case "priceAsc": AddOrderBy(p => p.Price); break;

                case "priceDesc": AddOrderByDescending(p => p.Price); break;

                default: AddOrderBy(p => p.Name); break;
                }
            }
        }
        public ProductsWithTypesAndBrandsSpecification(ProductSpecParams productParms)
            : base(x =>
                   (string.IsNullOrEmpty(productParms.Search) || x.Name.ToLower().Contains(productParms.Search))
                   &&
                   (!productParms.BrandId.HasValue || x.ProductBrandId == productParms.BrandId)
                   &&
                   (!productParms.TypeId.HasValue || x.ProductTypeId == productParms.TypeId)

                   )

        {
            AddInclude(x => x.ProductType);
            AddInclude(x => x.ProductBrand);
            AddOrderBy(x => x.Name);
            //skip operation
            ApplyPaging(productParms.PageSize * (productParms.PageIndex - 1), productParms.PageSize);



            //which sorting do we want for the apropertiate key
            if (!string.IsNullOrEmpty(productParms.Sort))
            {
                switch (productParms.Sort)
                {
                case "priceAsc":
                    AddOrderBy(p => p.Price);
                    break;

                case "priceDesc":
                    AddOrderByDescending(p => p.Price);
                    break;

                default:
                    AddOrderBy(n => n.Name);
                    break;
                }
            }
        }
        public ProductsWithTypesAndBrandsSpecification(ProductSpecParams productParams)
            : base(x =>
                   (!productParams.BrandId.HasValue || x.ProductBrandId == productParams.BrandId) &&
                   (!productParams.TypeId.HasValue || x.ProductTypeId == productParams.TypeId) &&
                   (string.IsNullOrEmpty(productParams.Search) || x.Name.ToLower().Contains(productParams.Search))
                   )
        {
            AddInclude(x => x.ProductBrand);
            AddInclude(x => x.ProductType);
            AddOrderBy(x => x.Name);
            Console.WriteLine(productParams.Sort);
            if (!string.IsNullOrEmpty(productParams.Sort))
            {
                switch (productParams.Sort)
                {
                case "nameAsc":
                    AddOrderBy(x => x.Name);
                    break;

                case "nameDesc":
                    AddOrderByDescending(x => x.Name);
                    break;

                case "priceAsc":
                    Console.WriteLine("dd");
                    AddOrderBy(x => x.Price);
                    break;

                case "priceDesc":
                    AddOrderByDescending(x => x.Price);
                    break;
                }
                ;
            }
            ApplyPaging(
                take: productParams.PageSize,
                skip: (productParams.PageIndex - 1) * productParams.PageSize);
        }
        // 60-1 adding sort parameter into specification
        // 64 -4 replace the gazillions parameters by custom parameter class productParams.
        public ProductsWithTypesAndBrandsSpecification(ProductSpecParams productParams) :
            base(x =>
                 // 66-2 pass expression to base to get search functionality
                 (string.IsNullOrEmpty(productParams.Search) || x.Name.ToLower().Contains(productParams.Search)) && // 66-2. Search Functionality
                 // 62-2. the where close is at baseSpecification, them we need to pass the ProductBrandId and
                 // the productTypeId filters to the base to be evaluated.
                 (!productParams.BrandId.HasValue || x.ProductBrandId == productParams.BrandId) &&
                 (!productParams.TypeId.HasValue || x.ProductTypeId == productParams.TypeId)
                 )
        {
            // 39-2 Start including ProductType and ProductBrand
            AddInclude(x => x.ProductType);
            AddInclude(x => x.ProductBrand);

            // 60-2 adding sort by name
            AddOrderBy(x => x.Name);

            // 64-6 adding pagination
            ApplyPaging(productParams.PageSize * (productParams.PageIndex - 1), productParams.PageSize);

            // 60-3 sort by price or name.
            if (!string.IsNullOrEmpty(productParams.Sort))
            {
                switch (productParams.Sort)
                {
                case "priceAsc":
                    AddOrderBy(p => p.Price);
                    break;

                case "priceDesc":
                    AddOrderByDescending(p => p.Price);
                    break;

                default: AddOrderBy(n => n.Name);
                    break;
                }
            }
        }
        public ProductsWithTypesAndBrandsSpecification(ProductSpecParams productParams)
            : base(x =>
                   // Search functionality
                   (string.IsNullOrEmpty(productParams.Search) || x.Name.ToLower().Contains(productParams.Search)) &&
                   // Filter by brand or type
                   (!productParams.BrandId.HasValue || x.ProductBrandId == productParams.BrandId) &&
                   (!productParams.TypeId.HasValue || x.ProductTypeId == productParams.TypeId)
                   )
        {
            // Include Product Type and Brand name
            AddInclude(x => x.ProductType);
            AddInclude(x => x.ProductBrand);
            // Default order by product name
            AddOrderBy(x => x.Name);
            // Add pagination
            ApplyPaging(productParams.PageSize * (productParams.PageIndex - 1), productParams.PageSize);

            // Sort by conditions
            if (!string.IsNullOrEmpty(productParams.Sort))
            {
                switch (productParams.Sort)
                {
                case "priceAsc":
                    AddOrderBy(p => p.Price);
                    break;

                case "priceDesc":
                    AddOrderByDescending(p => p.Price);
                    break;

                default:
                    AddOrderBy(n => n.Name);
                    break;
                }
            }
        }
 public ProductsWithFiltersForCountSpecification(ProductSpecParams productParams) : base(x =>
                                                                                         (string.IsNullOrEmpty(productParams.Search) || x.Name.ToLower().Contains(productParams.Search)) &&
                                                                                         (!productParams.BrandId.HasValue || x.ProductBrandId == productParams.BrandId) &&
                                                                                         (!productParams.TypeId.HasValue || x.ProductTypeId == productParams.TypeId))
 {
 }
예제 #10
0
 public ProductWithFiltersForCountSpecification(ProductSpecParams productParams)
     : base(x =>
            (!productParams.BrandId.HasValue || x.ProductBrandId == productParams.BrandId) &&
            (!productParams.TypeId.HasValue || x.ProductTypeId == productParams.TypeId))
 {
 }
        public ProductsWithDeptCatProdTypeSpec(ProductSpecParams productSpecParams)
            : base(x =>
                   (string.IsNullOrEmpty(productSpecParams.Search) ||
                    x.Name.ToLower().Contains(productSpecParams.Search)) &&
                   (!productSpecParams.DepartmentId.HasValue ||
                    x.DepartmentId == (int)productSpecParams.DepartmentId.Value) &&
                   (!productSpecParams.CategoryId.HasValue ||
                    x.CategoryId == (int)productSpecParams.CategoryId.Value) &&
                   (!productSpecParams.ProductTypeId.HasValue ||
                    x.ProductTypeId == (int)productSpecParams.ProductTypeId.Value))
        {
            AddInclude(x => x.Department);
            AddInclude(x => x.Category);
            AddInclude(x => x.ProductType);
            AddOrderBy(x => x.Name); //default sort
            ApplyPaging(productSpecParams.PageSize * (productSpecParams.PageIndex - 1), productSpecParams.PageSize);

            if (!string.IsNullOrEmpty(productSpecParams.Sort))
            {
                switch (productSpecParams.Sort)
                {
                case AppConstants.deptAsc:
                    AddOrderBy(d => d.Department.Name);
                    break;

                case AppConstants.deptDesc:
                    AddOrderByDescending(d => d.Department.Name);
                    break;

                case AppConstants.categoryAsc:
                    AddOrderBy(c => c.Category.Name);
                    break;

                case AppConstants.categoryDesc:
                    AddOrderByDescending(c => c.Category.Name);
                    break;

                case AppConstants.prodTypeAsc:
                    AddOrderBy(p => p.ProductType.Name);
                    break;

                case AppConstants.prodTypeDesc:
                    AddOrderByDescending(p => p.ProductType.Name);
                    break;

                case AppConstants.priceAsc:
                    AddOrderBy(p => p.Price);
                    break;

                case AppConstants.priceDesc:
                    AddOrderByDescending(p => p.Price);
                    break;

                case AppConstants.nameAsc:
                    AddOrderBy(n => n.Name);
                    break;

                case AppConstants.nameDesc:
                    AddOrderByDescending(n => n.Name);
                    break;

                default:
                    AddOrderBy(n => n.Name);
                    break;
                }
            }
        }
        public Products_w_Types_Brands_Spec(ProductSpecParams productParams)
            : base(x =>
                   (string.IsNullOrEmpty(productParams.Search) || x.Name.ToLower().Contains(productParams.Search)) &&
                   (!productParams.BrandId.HasValue || x.ProductBrandId == productParams.BrandId) &&
                   (!productParams.TypeId.HasValue || x.ProductTypeId == productParams.TypeId)
                   )
        {
            AddInclude(x => x.ProductType);
            AddInclude(x => x.ProductBrand);
            AddOrderBy(x => x.Name);
            AddInclude(x => x.Photos);
            ApplyPaging(productParams.PageSize * (productParams.PageIndex - 1), productParams.PageSize);

            if (!string.IsNullOrEmpty(productParams.Sort))
            {
                switch (productParams.Sort)
                {
                case "idAsc":
                    AddOrderBy(p => p.Id);
                    break;

                case "idDesc":
                    AddOrderByDescending(p => p.Id);
                    break;

                case "priceAsc":
                    AddOrderBy(p => p.Price);
                    break;

                case "priceDesc":
                    AddOrderByDescending(p => p.Price);
                    break;

                case "nameAsc":
                    AddOrderBy(p => p.Name);
                    break;

                case "nameDesc":
                    AddOrderByDescending(p => p.Name);
                    break;

                case "brandAsc":
                    AddOrderBy(p => p.ProductBrand.Name);
                    break;

                case "brandDesc":
                    AddOrderByDescending(p => p.ProductBrand.Name);
                    break;

                case "typeAsc":
                    AddOrderBy(p => p.ProductType.Name);
                    break;

                case "typeDesc":
                    AddOrderByDescending(p => p.ProductType.Name);
                    break;

                default:
                    AddOrderBy(n => n.Name);
                    break;
                }
            }
        }
 public ProductWithFilterForCountSpecifications(ProductSpecParams productSpec) : base(a =>
                                                                                      (string.IsNullOrWhiteSpace(productSpec.Search) || a.Name.ToLower().Contains(productSpec.Search)) &&
                                                                                      (!productSpec.BrandId.HasValue || a.ProductBrandId == productSpec.BrandId) &&
                                                                                      (!productSpec.TypeId.HasValue || a.ProductTypeId == productSpec.TypeId))
 {
 }
 public ProductWithFiltersForCountSpec(ProductSpecParams specParams)
     : base(x => (string.IsNullOrEmpty(specParams.Search) || x.Name.ToLower().Contains(specParams.Search)) &&
            (!specParams.BrandId.HasValue || x.ProductBrandId == specParams.BrandId) &&
            (!specParams.TypeId.HasValue || x.ProductTypeId == specParams.TypeId))
 {
 }