public async Task <List <CategoryProduct> > GetAllWithFilter(CategoryProductFilter categoryProductFilter)
        {
            Expression <Func <CategoryProduct, bool> > filter = null;
            Func <IQueryable <CategoryProduct>, IOrderedQueryable <CategoryProduct> > ordeBy = null;
            int?skip = null;
            int?take = 25;

            if (!string.IsNullOrWhiteSpace(categoryProductFilter.Name))
            {
                if (filter == null)
                {
                    filter = PredicateBuilder.New <CategoryProduct>(true);
                }

                filter = filter.And(x => x.Name == categoryProductFilter.Name);
            }

            if (!string.IsNullOrWhiteSpace(categoryProductFilter.Description))
            {
                if (filter == null)
                {
                    filter = PredicateBuilder.New <CategoryProduct>(true);
                }

                filter = filter.And(x => x.Description == categoryProductFilter.Description);
            }

            if (!string.IsNullOrWhiteSpace(categoryProductFilter.Order))
            {
                switch (categoryProductFilter.Order)
                {
                case "Id":
                    ordeBy = x => x.OrderBy(n => n.Id);
                    break;

                case "Name":
                    ordeBy = x => x.OrderBy(n => n.Name);
                    break;

                case "Description":
                    ordeBy = x => x.OrderBy(n => n.Description);
                    break;

                case "CreatedAt":
                    ordeBy = x => x.OrderBy(n => n.CreatedAt);
                    break;
                }
            }

            if (categoryProductFilter.Id != Guid.Empty)
            {
                if (filter == null)
                {
                    filter = PredicateBuilder.New <CategoryProduct>(true);
                }

                filter = filter.And(x => x.Id == categoryProductFilter.Id);
            }

            if (categoryProductFilter.CreatedAt.Year > 1)
            {
                if (filter == null)
                {
                    filter = PredicateBuilder.New <CategoryProduct>(true);
                }

                filter = filter.And(x => x.Id == categoryProductFilter.Id);
            }

            if (categoryProductFilter.PageIndex != null)
            {
                if (categoryProductFilter.PageIndex > 1)
                {
                    skip = categoryProductFilter.PageIndex * 25;
                }
            }

            if (categoryProductFilter.PageSize != null)
            {
                if (categoryProductFilter.PageSize > 0)
                {
                    take = categoryProductFilter.PageSize;
                }
            }

            return(await _unitOfWork
                   .RepositoryFactory
                   .CategoryProductRepository
                   .Search(filter, ordeBy, skip, take));
        }
Example #2
0
 public async Task <IEnumerable <CategoryProductViewModel> > GetAllWithFilter([FromQuery] CategoryProductFilter filter)
 {
     return(_mapper.Map <IEnumerable <CategoryProductViewModel> >(await _categoryProductService.GetAllWithFilter(filter)));
 }