Example #1
0
        public RecipeResult CreateRecipe(AuthIdentity identity, Recipe recipe)
        {
            using (var session = GetSession())
             {
            using (var transaction = session.BeginTransaction())
            {
               // Create Recipe
               var dbRecipe = new Models.Recipes
               {
                  Title = recipe.Title,
                  Description = recipe.Description,
                  CookTime = recipe.CookTime,
                  PrepTime = recipe.PrepTime,
                  Credit = recipe.Credit,
                  CreditUrl = recipe.CreditUrl,
                  DateEntered = recipe.DateEntered,
                  ImageUrl = recipe.ImageUrl,
                  Rating = recipe.AvgRating,
                  ServingSize = recipe.ServingSize,
                  Steps = recipe.Method
               };

               session.Save(dbRecipe);

               // Create Ingredients
               short displayOrder = 0;
               recipe.Ingredients.ForEach(i =>
               {
                  var dbIngredient = new RecipeIngredients
                  {
                     Recipe = dbRecipe,
                     Ingredient = Models.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 dbMetadata = new RecipeMetadata
               {
                  Recipe = dbRecipe,
                  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.Common),
                  SkillEasy = recipe.Tags.HasTag(RecipeTag.Easy),
                  SkillQuick = recipe.Tags.HasTag(RecipeTag.Quick)
               };

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

               return new RecipeResult
               {
                  RecipeCreated = true,
                  NewRecipeId = dbRecipe.RecipeId
               };
            }
             }
        }
Example #2
0
        public SearchResults Search(AuthIdentity identity, RecipeQuery query)
        {
            using (var session = this.adapter.GetSession())
            {
                Recipes recipe = null;

                var q = session.QueryOver <Recipes>(() => recipe)
                        .Where(p => !p.Hidden);

                // Add keyword search
                if (!string.IsNullOrWhiteSpace(query.Keywords))
                {
                    q = q.Where(
                        Restrictions.Or(
                            Restrictions.InsensitiveLike("Title", string.Format("%{0}%", query.Keywords.Trim())),
                            Restrictions.InsensitiveLike("Description", string.Format("%{0}%", query.Keywords.Trim()))));
                }

                if (query.Time.MaxPrep.HasValue)
                {
                    q = q.Where(p => p.PrepTime <= query.Time.MaxPrep.Value);
                }

                if (query.Time.MaxCook.HasValue)
                {
                    q = q.Where(p => p.CookTime <= query.Time.MaxCook.Value);
                }

                if (query.Rating > 0)
                {
                    q = q.Where(p => p.Rating >= (int)query.Rating);
                }

                // Add ingredients to include
                if (query.Include != null && query.Include.Length > 0)
                {
                    // Create a sub-query for ingredients to include
                    q = q.WithSubquery
                        .WhereExists(QueryOver.Of <RecipeIngredients>()
                                     .Where(item => item.Recipe.RecipeId == recipe.RecipeId)
                                     .Where(Restrictions.InG("Ingredient", query.Include.Select(Models.Ingredients.FromId).ToArray()))
                                     .Select(i => i.RecipeIngredientId).Take(1));
                }

                // Add ingredients to exclude
                if (query.Exclude != null && query.Exclude.Length > 0)
                {
                    // Create a sub-query for ingredients to exclude
                    q = q.WithSubquery
                        .WhereNotExists(QueryOver.Of <RecipeIngredients>()
                                        .Where(item => item.Recipe.RecipeId == recipe.RecipeId)
                                        .Where(Restrictions.InG("Ingredient", query.Exclude.Select(Models.Ingredients.FromId).ToArray()))
                                        .Select(i => i.RecipeIngredientId).Take(1));
                }

                if (query.Photos == PhotoFilter.Photo || query.Photos == PhotoFilter.HighRes)
                {
                    q = q.Where(Restrictions.IsNotNull("ImageUrl"));
                }

                // Need to search in metadata
                if (query.Diet || query.Nutrition || query.Skill || query.Taste || (query.Meal != MealFilter.All) || (query.Photos == PhotoFilter.HighRes))
                {
                    RecipeMetadata metadata = null;
                    q = q.JoinAlias(r => r.RecipeMetadata, () => metadata);

                    // Meal
                    if (query.Meal != MealFilter.All)
                    {
                        if (query.Meal == MealFilter.Breakfast)
                        {
                            q = q.Where(() => metadata.MealBreakfast);
                        }

                        if (query.Meal == MealFilter.Dessert)
                        {
                            q = q.Where(() => metadata.MealDessert);
                        }

                        if (query.Meal == MealFilter.Dinner)
                        {
                            q = q.Where(() => metadata.MealDinner);
                        }

                        if (query.Meal == MealFilter.Lunch)
                        {
                            q = q.Where(() => metadata.MealLunch);
                        }
                    }

                    // High-res photos
                    if (query.Photos == PhotoFilter.HighRes)
                    {
                        q = q.Where(() => metadata.PhotoRes >= 1024 * 768);
                    }

                    // Diet
                    if (query.Diet.GlutenFree)
                    {
                        q = q.Where(() => metadata.DietGlutenFree);
                    }

                    if (query.Diet.NoAnimals)
                    {
                        q = q.Where(() => metadata.DietNoAnimals);
                    }

                    if (query.Diet.NoMeat)
                    {
                        q = q.Where(() => metadata.DietNomeat);
                    }

                    if (query.Diet.NoPork)
                    {
                        q = q.Where(() => metadata.DietNoPork);
                    }

                    if (query.Diet.NoRedMeat)
                    {
                        q = q.Where(() => metadata.DietNoRedMeat);
                    }

                    // Nutrition
                    if (query.Nutrition.LowCalorie)
                    {
                        q = q.Where(() => metadata.NutritionLowCalorie);
                    }

                    if (query.Nutrition.LowCarb)
                    {
                        q = q.Where(() => metadata.NutritionLowCarb);
                    }

                    if (query.Nutrition.LowFat)
                    {
                        q = q.Where(() => metadata.NutritionLowFat);
                    }

                    if (query.Nutrition.LowSodium)
                    {
                        q = q.Where(() => metadata.NutritionLowSodium);
                    }

                    if (query.Nutrition.LowSugar)
                    {
                        q = q.Where(() => metadata.NutritionLowSugar);
                    }

                    // Skill
                    if (query.Skill.Common)
                    {
                        q = q.Where(() => metadata.SkillCommon).OrderBy(() => metadata.Commonality).Desc();
                    }

                    if (query.Skill.Easy)
                    {
                        q = q.Where(() => metadata.SkillEasy);
                    }

                    if (query.Skill.Quick)
                    {
                        q = q.Where(() => metadata.SkillQuick);
                    }

                    // Taste
                    if (query.Taste.MildToSpicy != SpicinessLevel.Medium)
                    {
                        q = query.Taste.MildToSpicy < SpicinessLevel.Medium
                           ? q.Where(() => metadata.TasteMildToSpicy <= query.Taste.Spiciness).OrderBy(() => metadata.TasteMildToSpicy).Asc()
                           : q.Where(() => metadata.TasteMildToSpicy >= query.Taste.Spiciness).OrderBy(() => metadata.TasteMildToSpicy).Desc();
                    }

                    if (query.Taste.SavoryToSweet != SweetnessLevel.Medium)
                    {
                        q = query.Taste.SavoryToSweet < SweetnessLevel.Medium
                           ? q.Where(() => metadata.TasteSavoryToSweet <= query.Taste.Sweetness).OrderBy(() => metadata.TasteSavoryToSweet).Asc()
                           : q.Where(() => metadata.TasteSavoryToSweet >= query.Taste.Sweetness).OrderBy(() => metadata.TasteSavoryToSweet).Desc();
                    }
                }

                IQueryOverOrderBuilder <Recipes, Recipes> orderBy;
                switch (query.Sort)
                {
                case SortOrder.Title:
                    orderBy = q.OrderBy(p => p.Title);
                    break;

                case SortOrder.PrepTime:
                    orderBy = q.OrderBy(p => p.PrepTime);
                    break;

                case SortOrder.CookTime:
                    orderBy = q.OrderBy(p => p.CookTime);
                    break;

                case SortOrder.Image:
                    orderBy = q.OrderBy(p => p.ImageUrl);
                    break;

                default:
                    orderBy = q.OrderBy(p => p.Rating);
                    break;
                }

                var results = (query.Direction == SortDirection.Descending ? orderBy.Desc() : orderBy.Asc())
                              .Skip(query.Offset)
                              .List();

                return(new SearchResults
                {
                    Briefs = results.Select(r => r.AsRecipeBrief()).ToArray(),
                    TotalCount = results.Count
                });
            }
        }
        public RecipeResult CreateRecipe(AuthorIdentity identity, Recipe recipe)
        {
            using (var session = this.GetSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    // Create Recipe
                    var databaseRecipe = new Recipes
                                       {
                                           Title = recipe.Title,
                                           Description = recipe.Description,
                                           CookTime = recipe.CookingTime,
                                           PrepTime = recipe.PreparationTime,
                                           Credit = recipe.Credit,
                                           CreditUrl = recipe.CreditUrl,
                                           DateEntered = recipe.DateEntered,
                                           ImageUrl = recipe.ImageUrl,
                                           Rating = recipe.AverageRating,
                                           ServingSize = recipe.ServingSize,
                                           Steps = recipe.Method
                                       };

                    session.Save(databaseRecipe);

                    CreateIngredients(recipe, databaseRecipe, session);

                    var databaseMetadata = CreateRecipeMetadata(recipe, databaseRecipe);

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

                    return new RecipeResult { RecipeCreated = true, NewRecipeId = databaseRecipe.RecipeId };
                }
            }
        }
 private static RecipeMetadata CreateRecipeMetadata(Recipe recipe, Recipes databaseRecipe)
 {
     var databaseMetadata = new RecipeMetadata
                          {
                              Recipe = databaseRecipe,
                              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.Common),
                              SkillEasy = recipe.Tags.HasTag(RecipeTag.Easy),
                              SkillQuick = recipe.Tags.HasTag(RecipeTag.Quick)
                          };
     return databaseMetadata;
 }
        private static void CreateIngredients(Recipe recipe, Recipes databaseRecipe, ISession session)
        {
            short displayOrder = 0;
            recipe.Ingredients.ForEach(
                i =>
                    {
                        var databaseIngredient = new RecipeIngredients
                                               {
                                                   Recipe = databaseRecipe,
                                                   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 ? i.Amount.SizeLow : null,
                                                   Unit =
                                                       i.Amount != null ? i.Amount.Unit : Units.Unit,
                                                   Section = i.Section,
                                                   DisplayOrder = ++displayOrder
                                               };

                        session.Save(databaseIngredient);
                    });
        }
        private static IQueryOver<Recipes, Recipes> AddIngredientsToInclude(
            RecipeQuery query,
            IQueryOver<Recipes, Recipes> subQuery,
            Recipes recipe)
        {
            if (query.Include != null && query.Include.Length > 0)
            {
                // Add ingredients to include
                // Create a sub-query for ingredients to include
                subQuery =
                    subQuery.WithSubquery.WhereExists(
                        QueryOver.Of<RecipeIngredients>()
                            .Where(item => item.Recipe.RecipeId == recipe.RecipeId)
                            .Where(Restrictions.InG("Ingredient", query.Include.Select(Ingredients.FromId).ToArray()))
                            .Select(i => i.RecipeIngredientId)
                            .Take(1));
            }

            return subQuery;
        }
Example #7
0
        public RecipeResult CreateRecipe(AuthIdentity identity, Recipe recipe)
        {
            using (var session = GetSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    // Create Recipe
                    var dbRecipe = new Models.Recipes
                    {
                        Title       = recipe.Title,
                        Description = recipe.Description,
                        CookTime    = recipe.CookTime,
                        PrepTime    = recipe.PrepTime,
                        Credit      = recipe.Credit,
                        CreditUrl   = recipe.CreditUrl,
                        DateEntered = recipe.DateEntered,
                        ImageUrl    = recipe.ImageUrl,
                        Rating      = recipe.AvgRating,
                        ServingSize = recipe.ServingSize,
                        Steps       = recipe.Method
                    };

                    session.Save(dbRecipe);

                    // Create Ingredients
                    short displayOrder = 0;
                    recipe.Ingredients.ForEach(i =>
                    {
                        var dbIngredient = new RecipeIngredients
                        {
                            Recipe         = dbRecipe,
                            Ingredient     = Models.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 dbMetadata = new RecipeMetadata
                    {
                        Recipe              = dbRecipe,
                        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.Common),
                        SkillEasy           = recipe.Tags.HasTag(RecipeTag.Easy),
                        SkillQuick          = recipe.Tags.HasTag(RecipeTag.Quick)
                    };

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

                    return(new RecipeResult
                    {
                        RecipeCreated = true,
                        NewRecipeId = dbRecipe.RecipeId
                    });
                }
            }
        }