public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] MyRecipe myRecipe)
        {
            if (id != myRecipe.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(myRecipe);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!MyRecipeExists(myRecipe.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(myRecipe));
        }
        public MyRecipe Build(Recipe rec)
        {
            MyRecipe recipe = new MyRecipe();

            var person = (Person)rec.Author.Value.Value;

            recipe.Author        = person.Name.HasValue && person.Name.Value.HasValue ? (string)person.Name.Value.Value : string.Empty;
            recipe.Category      = rec.RecipeCategory.HasValue && rec.RecipeCategory.Value.HasValue ? (string)rec.RecipeCategory.Value.Value : string.Empty;
            recipe.CookingMethod = rec.CookingMethod.HasValue && rec.CookingMethod.Value.HasValue ? (string)rec.CookingMethod.Value.Value: string.Empty;
            recipe.CookTime      = rec.CookTime.HasValue && rec.CookTime.Value.HasValue ? (TimeSpan)rec.CookTime.Value.Value : TimeSpan.FromTicks(0);
            recipe.Cuisine       = rec.RecipeCuisine.HasValue && rec.RecipeCuisine.Value.HasValue ? (string)rec.RecipeCuisine.Value.Value : string.Empty;
            recipe.Desciption    = rec.Description.HasValue && rec.Description.Value.HasValue ? (string)rec.Description.Value.Value : string.Empty;
            recipe.Diets         = rec.SuitableForDiet.HasValue && rec.SuitableForDiet.Value.HasValue ? new List <string> {
                (rec.SuitableForDiet.Value.Value).ToString()
            } : null;
            recipe.Ingredients  = rec.RecipeIngredient.HasValue ? rec.RecipeIngredient.Value.List : null;
            recipe.Instructions = BuildInstructions(rec.RecipeInstructions);
            recipe.Name         = rec.Name.HasValue ? rec.Name.Value.Value.ToString() : string.Empty;
            recipe.Nutrition    = BuildNutrition(rec.Nutrition);
            recipe.PrepTime     = rec.PrepTime.HasValue && rec.PrepTime.Value.HasValue? (TimeSpan)rec.PrepTime.Value.Value : TimeSpan.FromTicks(0);
            recipe.Servings     = ExtractServings(rec.RecipeYield);
            recipe.Source       = rec.Url.HasValue && rec.Url.Value.HasValue ? rec.Url.Value.Value.ToString() : string.Empty;

            return(recipe);
        }
 public Task <int> SaveItemAsync(MyRecipe item)
 {
     if (item.Id != 0)
     {
         return(database.UpdateAsync(item));
     }
     else
     {
         return(database.InsertAsync(item));
     }
 }
Beispiel #4
0
 public void RemoveIngredient(IngredientModel Ingredient, bool similar)
 {
     if (similar == false)
     {
         MyRecipe.Remove(Ingredient);
     }
     else
     {
         SimilarRecipe.Remove(Ingredient);
     }
 }
Beispiel #5
0
 // Remove Ingredient
 public void RemoveIngredient(string IngredientName, DataModel dataModel, bool similar)
 {
     if (similar == false)
     {
         MyRecipe.Remove(new IngredientModel(IngredientName, dataModel));
     }
     else
     {
         SimilarRecipe.Remove(new IngredientModel(IngredientName, dataModel));
     }
 }
Beispiel #6
0
 public void AddIngredient(IngredientModel Ingredient, bool similar)
 {
     if (similar == false)
     {
         MyRecipe.Add(Ingredient);
     }
     else
     {
         SimilarRecipe.Add(Ingredient);
     }
 }
        public async Task <IActionResult> Create([Bind("Name")] MyRecipe myRecipe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(myRecipe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(myRecipe));
        }
        public NewItemPage()
        {
            InitializeComponent();

            Item = new MyRecipe
            {
                Title       = "Item title",
                Ingredients = "Ingredients",
                Description = "This is an item description."
            };

            BindingContext = this;
        }
Beispiel #9
0
        public ItemDetailPage()
        {
            InitializeComponent();

            var item = new MyRecipe
            {
                Title       = "Pizza",
                Ingredients = "Olive, cheese, tomatoes",
                Description = "This is an description for pizza baking. You can add your own recipes. Just click + (ADD)."
            };

            viewModel      = new ItemDetailViewModel(item);
            BindingContext = viewModel;
        }
Beispiel #10
0
        public void TestSerialize()
        {
            MyRecipe        ar     = new MyRecipe();
            Stream          stream = File.Open("favourite_recipes.osl", FileMode.Create);
            BinaryFormatter b      = new BinaryFormatter();

            try
            {
                b.Serialize(stream, ar);
            }
            catch (System.Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
Beispiel #11
0
        public void TestDeserialize()
        {
            TestDeserialize();
            Stream          stream = File.Open("favourite_recipes.osl", FileMode.Open);
            BinaryFormatter b      = new BinaryFormatter();
            MyRecipe        ar     = new MyRecipe();

            try
            {
                ar = (MyRecipe)b.Deserialize(stream);
            }
            catch (System.Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
        public void ExtractRecipeTest()
        {
            Recipe             rec          = PrepRecipe();
            RecipeInstructions instructions = PrepRecipeInstructions();
            NutritionList      nlist        = new NutritionList
            {
                Calories = 240,
                Fat      = 9
            };

            RecipeBuilder builder = new RecipeBuilder();
            MyRecipe      recipe  = builder.Build(rec);

            Assert.Equal("Sahil Makhija", recipe.Author);
            Assert.Equal("Dessert", recipe.Category);
            Assert.Equal(string.Empty, recipe.CookingMethod);
            Assert.Equal(TimeSpan.FromMinutes(20), recipe.CookTime);
            Assert.Equal("General", recipe.Cuisine);
            Assert.Equal("A delicious layered low carb dessert with the flavours of chocolate and coffee", recipe.Desciption);
            Assert.Equal(new List <string> {
                "LowFatDiet"
            }, recipe.Diets);
            Assert.Equal(new List <string>
            {
                "45 grams Almond Flour ( I use this one )",
                "30 grams Salted Butter",
                "1 Tbsp Unsweetened Coco Powder ( I recommend this one )",
                "150 grams Mascarpone cheese",
                "1 Tsp Vanilla Extract",
                "2 Tbsp Water",
                "1 Tsp Instant espresso powder",
                "100 ml Heavy Cream",
                "30 grams Dark Chocolate (85% or Higher) (I use Lindt 85%)",
                "Stevia to taste"
            }
                         , recipe.Ingredients);

            recipe.Instructions.Should().BeEquivalentTo(instructions);
            Assert.Equal("Keto Coffee & Chocolate Tart", recipe.Name);
            recipe.Nutrition.Should().BeEquivalentTo(nlist);
            Assert.Equal(TimeSpan.FromMinutes(10), recipe.PrepTime);
            Assert.Equal(3, recipe.Servings);
            Assert.Equal("http://www.janedoe.com/", recipe.Source);
        }
        public static MyRecipe ExtractRecipe(string url)
        {
            string   urlResponse;
            MyRecipe myRecipe = null;

            // 1. Get Response from url
            using (WebClient w = new WebClient())
            {
                urlResponse = w.DownloadString(url);
            }

            //2: Check and scrape if any structured JSON is present (application/ld+json)
            var configJson      = @"{                
                'data': '//script[contains(@type, \'application\/ld+json\')]'
            }";
            var config          = StructuredDataConfig.ParseJsonString(configJson);
            var openScraping    = new StructuredDataExtractor(config);
            var scrapingResults = openScraping.Extract(urlResponse);

            if (scrapingResults != null && scrapingResults["data"] != null)
            {
                var content = scrapingResults["data"].ToString();
                if (content.Contains("\"@type\":\"Recipe\""))
                {
                    try
                    {
                        var serializerSettings = new JsonSerializerSettings()
                        {
                            DateParseHandling = DateParseHandling.DateTimeOffset
                        };
                        Recipe        rec     = JsonConvert.DeserializeObject <Recipe>(content, serializerSettings);
                        RecipeBuilder builder = new RecipeBuilder();
                        myRecipe = builder.Build(rec);
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            return(myRecipe);
        }
        public async Task AddNewRecipeAsync(MyRecipe item)
        {
            int result = 0;

            try
            {
                //basic validation to ensure a title was entered
                if (string.IsNullOrEmpty(item.Title))
                {
                    throw new Exception("Title required");
                }

                if (string.IsNullOrEmpty(item.Description))
                {
                    throw new Exception("Description required");
                }


                result = await database.InsertAsync(item);

                List <MyRecipe> recipes;
                recipes = await database.Table <MyRecipe>().OrderBy(a => a.Title).ToListAsync();

                Recipes.Clear();
                foreach (var c in recipes)
                {
                    Recipes.Add(c);
                }

                //StatusMessage = string.Format("{0} record(s) added [Name: {1})", result, title);
            }
            catch (Exception ex)
            {
                //StatusMessage = string.Format("Failed to add {0}. Error: {1}", title, ex.Message);
            }
        }
 public Task <int> DeleteItemAsync(MyRecipe item)
 {
     return(database.DeleteAsync(item));
 }
Beispiel #16
0
 public ItemDetailViewModel(MyRecipe item = null)
 {
     Title    = item?.Title;
     MyRecipe = item;
 }