コード例 #1
0
        [HttpGet] // Pobieranie wartości
        public async Task <IActionResult> GetRecipes([FromQuery] RecipeParams recipeParams)
        {
            var recipes = await _repository.GetRecipes(recipeParams);

            var recipesToReturn = _mapper.Map <IEnumerable <RecipeForListDto> >(recipes);

            Response.AddPagination(recipes.CurrentPage,
                                   recipes.PageSize,
                                   recipes.TotalCount,
                                   recipes.TotalPages);

            return(Ok(recipesToReturn));
        }
コード例 #2
0
        public async Task <IActionResult> GetRecipes([FromQuery] RecipeParams recipeParams)
        {
            var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var recipesFromRepo = await _repo.GetRecipes(userId, recipeParams);

            var recipesToReturn = _mapper.Map <IEnumerable <RecipeForUserListingDto> >(recipesFromRepo);

            Response.AddPagination(recipesFromRepo.CurrentPage, // add pagination headers
                                   recipesFromRepo.PageSize,
                                   recipesFromRepo.ItemsCount,
                                   recipesFromRepo.TotalPages);

            return(Ok(recipesToReturn));
        }
コード例 #3
0
ファイル: CookRepository.cs プロジェクト: oogo17/cookApp
        public async Task <PagedList <Recipe> > GetRecipes(int id, RecipeParams recipeParams)
        {
            var recipes = _context.Recipe.Where(x => x.UserId == id);

            if (!string.IsNullOrEmpty(recipeParams.Type))
            {
                recipes = recipes.Where(x => x.Type == recipeParams.Type);
            }

            if (!string.IsNullOrEmpty(recipeParams.RecipeName))
            {
                recipes = recipes.Where(x => x.Name.Contains(recipeParams.RecipeName));
            }

            return(await PagedList <Recipe> .CreateAsync(recipes, recipeParams.PageNumber, recipeParams.PageSize));
        }
コード例 #4
0
        public async Task <PagedList <Recipe> > GetRecipes(RecipeParams recipeParams)
        {
            var recipes = _context.Recipes
                          .Include(c => c.Categories)
                          .Include(p => p.RecipePhotos)
                          .Include(u => u.User).AsQueryable();



            if (!string.IsNullOrEmpty(recipeParams.Category))
            {
                recipes = recipes.Where(u => u.Categories.Name == recipeParams.Category);
            }

            if (recipeParams.MinTime != 2 || recipeParams.MaxTime != 360)
            {
                recipes = recipes.Where(r => r.PreparationTime >= recipeParams.MinTime &&
                                        r.PreparationTime <= recipeParams.MaxTime);
            }

            return(await PagedList <Recipe> .CreateAsync(recipes, recipeParams.PageNumber, recipeParams.PageSize));
        }
コード例 #5
0
        public async Task <PagedList <Recipe> > GetRecipes(int loggedUserId, RecipeParams recipeParams)
        {
            var recipes = _context.Recipes
                          .Include(i => i.Ingredients)
                          .OrderByDescending(r => r.DateAdded)
                          .AsQueryable();

            if (recipeParams.IsVegan == true)
            {
                recipes = recipes.Where(r => r.IsVegan == true);
            }

            if (recipeParams.IsVegetarian == true)
            {
                recipes = recipes.Where(r => r.IsVegetarian == true);
            }

            if (recipeParams.SearchQuery != null && recipeParams.SearchQuery != "null") // check for ingredient
            {
                recipes = recipes.Where(r => r.Name.Contains(recipeParams.SearchQuery) || r.Ingredients.Any(i => i.Name.Contains(recipeParams.SearchQuery)));
            }

            if (recipeParams.Followers) // people that logged user follows
            {
                var userLikers = await GetUserFollows(loggedUserId, recipeParams.Followers);

                recipes = recipes.Where(u => userLikers.Contains(u.UserId));
            }

            if (recipeParams.Followees) // people that are followed by logged user
            {
                var userLikees = await GetUserFollows(loggedUserId, recipeParams.Followers);

                recipes = recipes.Where(u => userLikees.Contains(u.UserId));
            }

            return(await PagedList <Recipe> .CreateAsync(recipes, recipeParams.PageNumber, recipeParams.PageSize));
        }