예제 #1
3
        public void CreateRecipe(RecipeViewModel viewModel)
        {
            var user = _unitOfWork.Users.Get(_ => _.Id == viewModel.CreatedBy);

            var recipe = new Recipe()
            {
                Id = Guid.NewGuid(),
                Name = viewModel.Name,
                ImageUrl = viewModel.ImageUrl,
                Created = DateTime.Now,
                User = user,
                Rate = viewModel.Rate,
                Description = viewModel.Description,
                Instructions = viewModel.Instructions,
                Time = viewModel.Time,
                Severity = viewModel.Severity,
                Amounts = new List<Amount>()
            };

            var ingredients = viewModel.Ingredients;

            foreach (var ingredient in ingredients)
            {
                Ingredient relationIngredient;
                if (ingredient.Id == Guid.Empty)
                {
                    relationIngredient = new Ingredient
                    {
                        Id = Guid.NewGuid(),
                        Name = ingredient.Name,
                        Recipes = new List<Recipe> { recipe },
                    };

                    _unitOfWork.Ingredients.Add(relationIngredient);
                }
                else
                {
                    relationIngredient = _unitOfWork.Ingredients.Get(_ => _.Id == ingredient.Id);
                    relationIngredient.Recipes.Add(recipe);
                }

                recipe.Amounts.Add(new Amount
                {
                    Id = Guid.NewGuid(),
                    AmountOf = ingredient.AmountOf,
                    Unit = ingredient.Unit,
                    Recipe = recipe,
                    Ingredient = relationIngredient
                });
            }

            foreach (var tagViewModel in viewModel.Tags)
            {
                var tag = _unitOfWork.RecipeTags.Get(_ => _.Name.ToLower().Equals(tagViewModel.Text.ToLower()));
                if (tagViewModel.Id == Guid.Empty && tag == null)
                {
                    _unitOfWork.RecipeTags.Add(new RecipeTags
                    {
                        Id = Guid.NewGuid(),
                        IsCategory = false,
                        Name = tagViewModel.Text,
                        Recipes = new List<Recipe> { recipe }
                    });

                    continue;
                }

                tag.Recipes.Add(recipe);
            }

            _unitOfWork.Recipies.Add(recipe);
            _unitOfWork.Commit();
        }
예제 #2
0
 public void Add(Ingredient ingredient, double amountInGrams)
 {
     MealComponents.Add(new MealComponent(ingredient, amountInGrams));
 }
예제 #3
0
 public Meal(string name, Ingredient ingredient, double amountInGrams)
     : this(name)
 {
     Add(ingredient, amountInGrams);
 }
예제 #4
0
        public RecipeViewModel UpdateRecipe(RecipeViewModel viewModel)
        {
            var recipeToUpdate = _unitOfWork.Recipies.Get(_ => _.Id == viewModel.Id);

            recipeToUpdate.Name = viewModel.Name;
            recipeToUpdate.ImageUrl = viewModel.ImageUrl;
            recipeToUpdate.Description = viewModel.Description;
            recipeToUpdate.Instructions = viewModel.Instructions;
            recipeToUpdate.Time = viewModel.Time;
            recipeToUpdate.Severity = viewModel.Severity;

            recipeToUpdate.Tags.Clear();

            foreach (var tagViewModel in viewModel.Tags)
            {
                var tag = _unitOfWork.RecipeTags.Get(_ => _.Name.ToLower().Equals(tagViewModel.Text.ToLower()));
                if (tagViewModel.Id == Guid.Empty && tag == null)
                {
                    _unitOfWork.RecipeTags.Add(new RecipeTags
                    {
                        Id = Guid.NewGuid(),
                        IsCategory = false,
                        Name = tagViewModel.Text,
                        Recipes = new List<Recipe> { recipeToUpdate }
                    });

                    continue;
                }

                tag.Recipes.Add(recipeToUpdate);
            }

            foreach (var ingredient in viewModel.Ingredients)
            {
                if (ingredient.Id == Guid.Empty)
                {
                    var newIngredient = new Ingredient
                    {
                        Id = Guid.NewGuid(),
                        Name = ingredient.Name,
                        Recipes = new List<Recipe> { recipeToUpdate }
                    };

                    _unitOfWork.Ingredients.Add(newIngredient);

                    var amount = new Amount
                    {
                        AmountOf = ingredient.AmountOf,
                        Ingredient = newIngredient,
                        Recipe = recipeToUpdate,
                        Unit = ingredient.Unit,
                        IngredientId = newIngredient.Id
                    };

                    recipeToUpdate.Amounts.Add(amount);
                }
                else
                {
                    if (ingredient.RecipeId == Guid.Empty)
                    {
                        var ingredientToInclude = _unitOfWork.Ingredients.Get(_ => _.Id == ingredient.Id);
                        recipeToUpdate.Ingredients.Add(ingredientToInclude);

                        var amount = new Amount
                        {
                            Id = Guid.NewGuid(),
                            AmountOf = ingredient.AmountOf,
                            Ingredient = ingredientToInclude,
                            Recipe = recipeToUpdate,
                            Unit = ingredient.Unit,
                            IngredientId = ingredientToInclude.Id
                        };

                        recipeToUpdate.Amounts.Add(amount);
                    }
                    else
                    {
                        var amountToUpdate = recipeToUpdate.Amounts.Find(_ => _.IngredientId == ingredient.Id);
                        amountToUpdate.AmountOf = ingredient.AmountOf;
                        amountToUpdate.Unit = ingredient.Unit;
                    }
                }
            }

            _unitOfWork.Commit();

            return GetRecipeAsViewModel(recipeToUpdate.Id, recipeToUpdate.UserId);
        }
예제 #5
0
 public Recipe(Ingredient ingredient, double amountInGrams)
 {
     Ingredient = ingredient;
     AmountInGrams = amountInGrams;
 }