Ejemplo n.º 1
0
        //public async Task<IList<Ingredients>> MapIngredients(IList<string> ingredientsList)
        //{
        //    var result = new List<Ingredients>();

        //    foreach (var ingredient in ingredientsList)
        //    {
        //        result.Add(await _ingredientsRepo.GetByName(ingredient));
        //    }

        //    return result;
        //}

        public async Task <PaginatedList <TotalRecipeModel> > GetRecipiesByQuery(Guid idUser, IList <string> ingredientsList, SearchModel model)
        {
            var spec = model.ToSpecification <Persistance.Models.Recipes>();

            List <Ingredients> ingredients = null;

            if (ingredientsList.Count != 0)
            {
                ingredients = new List <Ingredients>();
                foreach (var ingredient in ingredientsList)
                {
                    var temp = await _ingredientsRepo.GetByName(ingredient);

                    if (temp != null)
                    {
                        ingredients.Add(temp);
                    }
                    else
                    {
                        return(new PaginatedList <TotalRecipeModel>(1, 0, 0, new List <TotalRecipeModel>()));
                    }
                }
            }

            var result = await _recipeRepo.GetRecipiesByQuery(idUser, ingredients, spec);

            //result.ToList().ForEach(c => c.Ingredients = c.RecipesIngredients.Select(x => x.Ingredient).ToList());
            //result.ToList().ForEach(c => c.Filters = c.RecipesFilters.Select(x => x.Filter).ToList());

            return(new PaginatedList <TotalRecipeModel>(
                       model.PageIndex,
                       model.PageSize,
                       result.Count,
                       _mapper.Map <IList <TotalRecipeModel> >(result)));
        }
Ejemplo n.º 2
0
        public void Create(Ingredient ingredient)
        {
            var exists = ingredientsRepository.GetByName(ingredient.Name);

            if (exists == null)
            {
                var ing = new Ingredient();
                ing.Name = ingredient.Name;

                ingredientsRepository.Create(ing);
            }
        }
Ejemplo n.º 3
0
        public async Task <TotalRecipeModel> Add(UpsertRecipeModel model)
        {
            model.IdUser = Guid.Parse(_accessor.HttpContext.User.Claims.First(c => c.Type == "IdUser").Value);
            var recipe = _mapper.Map <Persistance.Models.Recipes>(model);

            model.Ingredients = model.Ingredients.Distinct().ToList();
            foreach (var ingredient in model.Ingredients)
            {
                var ing = await _ingredients.GetByName(ingredient);

                if (ing == null)
                {
                    recipe.Ingredients.Add(new RecipesIngredients(recipe, new Ingredients(ingredient)));
                }
                else
                {
                    recipe.Ingredients.Add(new RecipesIngredients(recipe, ing));
                }
            }

            var fil = await _filters.GetByName(model.Filter);

            if (fil == null)
            {
                recipe.Filters.Add(new RecipesFilters(recipe, new Filters(model.Filter)));
            }
            else
            {
                recipe.Filters.Add(new RecipesFilters(recipe, fil));
            }

            //recipe.Ingredients = recipe.RecipesIngredients.Select(x => x.Ingredient).ToList();
            //recipe.Filters = recipe.RecipesFilters.Select(x => x.Filter).ToList();

            await _repository.Add(recipe);

            await _repository.SaveChanges();

            return(_mapper.Map <TotalRecipeModel>(recipe));
        }
Ejemplo n.º 4
0
 public async Task <IActionResult> Get(string name)
 {
     return(Ok(await _ingredientsRepository.GetByName(name)));
 }