Пример #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 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);
        }
Пример #3
0
        public HttpResponseMessage Post(RecipeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                _recipeService.CreateRecipe(viewModel);

                return new HttpResponseMessage(HttpStatusCode.OK);
            }

            return new HttpResponseMessage(HttpStatusCode.BadRequest);
        }
Пример #4
0
        public RecipeViewModel GetRecipeAsViewModel(Guid id, Guid userId)
        {
            var recipe = _unitOfWork.Recipies.Get(_ => _.Id == id);
            var user = _unitOfWork.Users.Get(_ => _.Id == userId);
            var favorite = user.Favorites.FirstOrDefault(_=>_.Recipe.Id == id);
            var gradeing = recipe.Ratings.FirstOrDefault(_ => _.UserId == userId);

            var viewModel = new RecipeViewModel
            {
                Id = recipe.Id,
                Name = recipe.Name,
                ImageUrl = recipe.ImageUrl,
                Description = recipe.Description,
                Instructions = recipe.Instructions,
                Severity = recipe.Severity,
                Time = recipe.Time,
                Ingredients = new List<IngredientViewModel>(),
                Tags = new List<TagViewModel>(),
                Rate = recipe.Rate,
                Created = recipe.Created,
                CreatedBy = recipe.User.Id,
                UserRating = gradeing != null ? gradeing.Grade : (int?)null,
                NbOfRatings = recipe.Ratings.Count,
                User = new UserViewModel{FirstName = recipe.User.FirstName, LastName = recipe.User.LastName},
                FavoriteId = favorite == null ? (Guid?) null : favorite.Id
            };

            foreach (var ingredient in recipe.Ingredients)
            {
                var amount = recipe.Amounts.Find(_ => _.IngredientId == ingredient.Id);
                viewModel.Ingredients.Add(new IngredientViewModel { Id = ingredient.Id, Name = ingredient.Name, Unit = amount.Unit, RecipeId = recipe.Id, AmountOf = amount.AmountOf, AmountId = amount.Id });
            }

            foreach (var tag in recipe.Tags)
            {
                viewModel.Tags.Add(new TagViewModel{Id = tag.Id, IsCategory = tag.IsCategory, Text = tag.Name});
            }

            return viewModel;
        }
Пример #5
0
        public RecipeViewModel Put(Guid id, RecipeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                return _recipeService.UpdateRecipe(viewModel);
            }

            return null;
        }