Example #1
0
        public IList <Dish> GetSorted(DishSorting parameters)
        {
            Expression <Func <Dish, bool> > param = g => !g.IsDeleted;
            var conditionExpressions = new List <Expression <Func <Dish, bool> > >
            {
                param
            };

            if (!string.IsNullOrEmpty(parameters.CategoryId))
            {
                param = dish => dish.CategoryId == parameters.CategoryId;
                conditionExpressions.Add(param);
            }

            if (parameters.NewOnly)
            {
                param = dish => dish.IsNew;
                conditionExpressions.Add(param);
            }

            if (parameters.Allergies != null && parameters.Allergies.Any())
            {
                param = g => !g.Allergies
                        .Any(gn => parameters.Allergies
                             .Contains(gn.AllergyId));

                conditionExpressions.Add(param);
            }

            var condition  = ExpressionActions.CombinePredicates(conditionExpressions, Expression.AndAlso);
            var repository = _unitOfWork.GetRepository <Dish>();
            var result     = condition != null
                ? repository.GetMany(condition, null, TrackingState.Disabled, "Allergies.Allergy", "Category", "Images")
                : repository.GetMany(source => !source.IsDeleted, null, TrackingState.Disabled, "Allergies.Allergy", "Category", "Images");

            switch (parameters.SortingMethod)
            {
            case SortingMethod.PriceAsc:
                result = result.OrderBy(g => g.Price);
                break;

            case SortingMethod.PriceDesc:
                result = result.OrderByDescending(g => g.Price);
                break;

            case SortingMethod.Popular:
                result = result.OrderByDescending(g => g.OrderedCount);
                break;
            }

            return(result.ToList());
        }
        public IActionResult Get(DishSorting parameters)
        {
            var user      = _userService.Get(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var allergies = user.Allergies.Select(source => source.AllergyId);

            parameters.Allergies = allergies;
            var items = _dishService.GetSorted(parameters);

            var dishesListViewModel = new DishesListViewModel
            {
                Filter = parameters,
                Dishes = items
            };

            return(View(dishesListViewModel));
        }