Exemple #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();
        }
        public Recipe[] ReadRecipes(AuthIdentity identity, Guid[] recipeIds, ReadRecipeOptions options)
        {
            using (var session = this.GetSession())
            {
                var recipes = session.QueryOver<Recipes>()
                   .Fetch(prop => prop.RecipeMetadata).Eager
                   .Fetch(prop => prop.Ingredients).Eager
                   .Fetch(prop => prop.Ingredients[0].Ingredient).Eager
                   .Fetch(prop => prop.Ingredients[0].IngredientForm).Eager
                   .AndRestrictionOn(p => p.RecipeId).IsInG(recipeIds)
                   .TransformUsing(Transformers.DistinctRootEntity)
                   .List();

                if (!recipes.Any())
                {
                    throw new RecipeNotFoundException();
                }

                var ret = new List<Recipe>();
                foreach (var recipie in recipes)
                {
                    var recipe = new Recipe
                    {
                        Id = recipie.RecipeId,
                        Title = recipie.Title,
                        Description = recipie.Description,
                        DateEntered = recipie.DateEntered,
                        ImageUrl = recipie.ImageUrl,
                        ServingSize = recipie.ServingSize,
                        PreparationTime = recipie.PrepTime,
                        CookTime = recipie.CookTime,
                        Credit = recipie.Credit,
                        CreditUrl = recipie.CreditUrl,
                        AvgRating = recipie.Rating
                    };

                    if (options.ReturnMethod)
                    {
                        recipe.Method = recipie.Steps;
                    }

                    if (options.ReturnUserRating)
                    {
                        var id = recipie.RecipeId;
                        var rating = session.QueryOver<RecipeRatings>()
                           .Where(p => p.Recipe.RecipeId == id)
                           .Where(p => p.UserId == identity.UserId)
                           .SingleOrDefault();

                        recipe.UserRating = rating == null ? Rating.None : (Rating)rating.Rating;
                    }

                    recipe.Ingredients = recipie.Ingredients.Select(i => new IngredientUsage
                    {
                        Amount = i.Qty.HasValue ? new Amount(i.Qty.Value, i.Unit) : null,
                        PreparationNote = i.PrepNote,
                        Section = i.Section,
                        Form = i.IngredientForm != null ? i.IngredientForm.AsIngredientForm() : null, // Note: Form will be null when usage has no amount
                        Ingredient = i.Ingredient.AsIngredient()
                    }).ToArray();

                    recipe.Tags = recipie.RecipeMetadata.Tags;
                    ret.Add(recipe);
                }

                return ret.ToArray();
            }
        }
        public RecipeResult CreateRecipe(AuthIdentity identity, Recipe recipe)
        {
            using (var session = this.GetSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    // Create Recipe
                    var recipes = new Recipes
                    {
                        Title = recipe.Title,
                        Description = recipe.Description,
                        CookTime = recipe.CookTime,
                        PrepTime = recipe.PreparationTime,
                        Credit = recipe.Credit,
                        CreditUrl = recipe.CreditUrl,
                        DateEntered = recipe.DateEntered,
                        ImageUrl = recipe.ImageUrl,
                        Rating = recipe.AvgRating,
                        ServingSize = recipe.ServingSize,
                        Steps = recipe.Method
                    };

                    session.Save(recipes);

                    // Create Ingredients
                    short displayOrder = 0;
                    recipe.Ingredients.ForEach(i =>
                    {
                        var dbIngredient = new RecipeIngredients
                        {
                            Recipe = recipes,
                            Ingredient = Ingredients.FromId(i.Ingredient.Id),
                            IngredientForm = i.Form != null ? IngredientForms.FromId(i.Form.FormId) : null,
                            Qty = i.Amount != null ? (float?)i.Amount.SizeHigh : null,
                            QtyLow = i.Amount != null ? (float?)i.Amount.SizeLow : null,
                            Unit = i.Amount != null ? i.Amount.Unit : Units.Unit,
                            Section = i.Section,
                            DisplayOrder = ++displayOrder
                        };

                        session.Save(dbIngredient);
                    });

                    // Create RecipeMetadata
                    var recipeMetadata = new RecipeMetadata
                    {
                        Recipe = recipes,
                        DietGlutenFree = recipe.Tags.HasTag(RecipeTag.GlutenFree),
                        DietNoAnimals = recipe.Tags.HasTag(RecipeTag.NoAnimals),
                        DietNomeat = recipe.Tags.HasTag(RecipeTag.NoMeat),
                        DietNoPork = recipe.Tags.HasTag(RecipeTag.NoPork),
                        DietNoRedMeat = recipe.Tags.HasTag(RecipeTag.NoRedMeat),
                        MealBreakfast = recipe.Tags.HasTag(RecipeTag.Breakfast),
                        MealDessert = recipe.Tags.HasTag(RecipeTag.Dessert),
                        MealDinner = recipe.Tags.HasTag(RecipeTag.Dinner),
                        MealLunch = recipe.Tags.HasTag(RecipeTag.Lunch),
                        NutritionLowCalorie = recipe.Tags.HasTag(RecipeTag.LowCalorie),
                        NutritionLowCarb = recipe.Tags.HasTag(RecipeTag.LowCarb),
                        NutritionLowFat = recipe.Tags.HasTag(RecipeTag.LowFat),
                        NutritionLowSodium = recipe.Tags.HasTag(RecipeTag.LowSodium),
                        NutritionLowSugar = recipe.Tags.HasTag(RecipeTag.LowSugar),
                        SkillCommon = recipe.Tags.HasTag(RecipeTag.CommonIngredients),
                        SkillEasy = recipe.Tags.HasTag(RecipeTag.EasyToMake),
                        SkillQuick = recipe.Tags.HasTag(RecipeTag.Quick)
                    };

                    session.Save(recipeMetadata);
                    transaction.Commit();

                    return new RecipeResult
                    {
                        RecipeCreated = true,
                        NewRecipeId = recipes.RecipeId
                    };
                }
            }
        }