Beispiel #1
0
        public async Task <IEnumerable <DishPreviewDto> > GetDishesByIngredients(FilterRequest filterRequest)
        {
            var dishComparer = GenericEqualityComparer <Dish> .GetEqualityComparer((d1, d2) => d1.Id == d2.Id, d => d.Id);

            var query = (await(filterRequest.IncludeIngredients?.Any() ?? false
                ? _unitOfWork.Dishes.GetByIngredientsAsync(filterRequest.IncludeIngredients)
                : _unitOfWork.Dishes.GetDishesWithIngredientsCategoriesAndRatingsAsync(null, null))).AsQueryable();

            query = query.Where(d => d.Name.IndexOf(filterRequest.Name, StringComparison.InvariantCultureIgnoreCase) != -1);

            query = filterRequest.ExcludeIngredients?.Any() ?? false
                ? query.Except(await _unitOfWork.Dishes.GetByIngredientsAsync(filterRequest.ExcludeIngredients), dishComparer)
                : query;


            query = filterRequest.IncludeIngredients?.Any() ?? false
                ? query.Where(
                res => res.DishIngredients.Select(di => di.IngredientId)
                .Intersect(filterRequest.IncludeIngredients)
                .Count()
                / (double)filterRequest.IncludeIngredients.Count()
                >= 0.5)
                : query;

            return(query.Select(_mapper.Map <Dish, DishPreviewDto>)
                   .ToList());
        }