Example #1
0
        public static RecipeSource GetRecipeSource(RecipesContext db, string sourceName)
        {
            if (_recipeSources == null)
            {
                lock (_lockRecipeSource)
                    _recipeSources = db.RecipeSources.ToList();
            }

            RecipeSource rs = _recipeSources.FirstOrDefault(x => x.RecipeSourceName == sourceName);

            if (rs == null)
            {
                rs = new RecipeSource();
                rs.RecipeSourceName = sourceName;
                db.RecipeSources.Add(rs);
                db.SaveChanges();
                _recipeSources.Add(rs);
            }
            return(rs);
        }
        public Recipe GetRecipe(long id)
        {
            Logger.Info($"Processing recipe: {id}");
            var source = GetRecipeSource(id);

            if (source == null)
            {
                Logger.Error($"Recipe not found: {id}");
                return(null);
            }

            var recipeSource = new RecipeSource(source);

            if (!recipeSource.IsValid || recipeSource.Name.Contains("Italian Style Chicken Sausage Skillet Pizza"))
            {
                Logger.Error($"Recipe not found: {id}");
                return(null);
            }

            var recipe = new Recipe
            {
                Name            = recipeSource.Name,
                Description     = recipeSource.Description,
                Category        = recipeSource.Categories.FirstOrDefault(),
                Author          = recipeSource.Author,
                Rating          = recipeSource.Rating,
                PreparationTime = recipeSource.PreparationTime,
                CookTime        = recipeSource.CookTime,
                Servings        = recipeSource.Servings,
                Calories        = recipeSource.Calories,
                Ingredients     = recipeSource.Ingredients.Select(x => new RecipeIngredient {
                    Name = x
                }).ToList(),
                Directions = recipeSource.Directions,
                Image      = GetImage(recipeSource.ImageUrl)
            };

            Logger.Info($"Recipe processed: {recipe}");
            return(recipe);
        }