public async Task <IActionResult> ReadRecipesAsync(RecipesParameters parameters)
        {
            if (!string.IsNullOrWhiteSpace(parameters.Fields) && !PropertyManager.PropertiesExists <RecipeDto>(parameters.Fields))
            {
                return(BadRequest());
            }

            var recipes = await _recipeService.GetAsync(parameters, true);

            var recipesDto = _mapper.Map <IEnumerable <RecipeDto> >(recipes);

            return(Ok(recipesDto.ShapeData(parameters.Fields)));
        }
Beispiel #2
0
        public async Task <IEnumerable <Recipe> > GetUserRecipesAsync(int id, RecipesParameters parameters, bool asNoTracking = false)
        {
            if (!await _context.Users.ExistsInDatabaseAsync(id))
            {
                throw new CorruptedOperationException("Invalid user id.");
            }

            var recipes = await _recipeService.GetAsync(parameters, asNoTracking);

            recipes = recipes.Where(x => x.User.Id == id);

            return(recipes);
        }
Beispiel #3
0
        public async Task <IEnumerable <Recipe> > GetAsync(RecipesParameters parameters, bool asNoTracking = false)
        {
            var recipes = _context.Recipes.AsQueryable();

            if (parameters.CreatedAt != default(DateTime))
            {
                recipes = recipes.Where(x => x.CreatedAt.Date == parameters.CreatedAt.Date);
            }

            if (!string.IsNullOrEmpty(parameters.Query))
            {
                var nameForQuery = parameters.Query.ToLowerInvariant();

                recipes = recipes.Where(x => x.Name.ToLowerInvariant().Contains(nameForQuery));
            }

            if (parameters.IsLactoseFree != null)
            {
                recipes = recipes.Where(x => x.IsLactoseFree == parameters.IsLactoseFree);
            }

            if (parameters.IsGlutenFree != null)
            {
                recipes = recipes.Where(x => x.IsGlutenFree == parameters.IsGlutenFree);
            }

            if (parameters.IsVegan != null)
            {
                recipes = recipes.Where(x => x.IsVegan == parameters.IsVegan);
            }

            if (parameters.IsVegetarian != null)
            {
                recipes = recipes.Where(x => x.IsVegetarian == parameters.IsVegetarian);
            }

            recipes = recipes
                      .Include(x => x.User)
                      .Include(x => x.Rates)
                      .Include(x => x.Components)
                      .Include(x => x.RecipeImage)
                      .Include(x => x.RecipeCategories).ThenInclude(y => y.Category);

            if (asNoTracking)
            {
                recipes = recipes.AsNoTracking();
            }

            return(await recipes.ToListAsync());
        }
Beispiel #4
0
        public async Task <IActionResult> ReadRecipesAsync(int id, [FromQuery] RecipesParameters parameters)
        {
            if (id != AccountID)
            {
                return(Forbid());
            }

            if (!string.IsNullOrWhiteSpace(parameters.Fields) &&
                !PropertyManager.PropertiesExists <Recipe>(parameters.Fields))
            {
                return(BadRequest());
            }

            var recipes = await _userService.GetUserRecipesAsync(id, parameters, true);

            var recipesDto = _mapper.Map <IEnumerable <RecipeDto> >(recipes);

            return(Ok(recipesDto.ShapeData(parameters.Fields)));
        }