public IHttpActionResult PutRecipe(int id, Recipe recipe)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != recipe.Id)
            {
                return BadRequest();
            }

            _db.Entry(recipe).State = EntityState.Modified;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeExists(id))
                {
                    return NotFound();
                }

                throw;
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult PutFridge(string appToken, Recipe recipe)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var storage = db.ProductStorages.SingleOrDefault(_ => _.UserGroup.AppToken == appToken);
            var fridge = db.Fridges.SingleOrDefault(_ => _.UserGroup.AppToken == appToken);

            if (fridge == null && recipe != null && storage != null)
            {
                return NotFound();
            }

            var resultproducts = new List<Product>();

            recipe.Ingredients.Select(_ => _.Product).ForEach(p =>
            {
                if (storage.Products.Contains(p))
                {
                    resultproducts.Add(p);
                };
            });

          return Ok(resultproducts);
        }
        public IHttpActionResult PostRecipe(Recipe recipe)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _db.Recipes.Add(recipe);
            _db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new {id = recipe.Id}, recipe);
        }
        public IHttpActionResult PostFridge(string appToken, Recipe recipe)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var fridge = db.Fridges.SingleOrDefault(_ => _.UserGroup.AppToken == appToken);

            if (fridge == null && recipe != null)
            {
                return NotFound();
            }

            fridge.Dishes.Add(recipe);

            return Ok();
        }
Example #5
0
        public static void SaveAllRecipesToDb(IEnumerable<string> recipeLinks)
        {
            var resipeNodes = new List<HtmlNode>();
            recipeLinks.ForEach(recipelink =>
            {
                var html = new HtmlDocument();
                html.LoadHtml(GetHtmlString(recipelink));
                var root = html.DocumentNode;

                var currentPageRecipe =
                    root.SelectSingleNode("/html/body/div/div/div/div/div/article/div/div/div");
                resipeNodes.Add(currentPageRecipe);
            });

            foreach (var step in resipeNodes)
            {
                var recipeStepNodes = step.SelectNodes("//*[@class=\"instruction\"]").Select(_ => _.InnerText
                                        .Replace("\n", string.Empty)
                                        .Replace("\r", string.Empty)
                                        .Replace("\t", string.Empty)
                                        .Replace("&nbsp;", " ")).ToList();

                var recipeSteps = new List<RecipeStep>();

                recipeStepNodes.ForEach(_ => recipeSteps.Add(Context.RecipeSteps.First(s => s.Description == _)));

                var ingredientNodes = step.SelectNodes("//*[@class=\"ingredient\"]").Select(_ => new
                {
                    Names = _.SelectNodes("/html/body/div/div/div/div/div/article/div/div/div/div/table/tbody/tr//*[@class=\"name\"]").Select(n => n.InnerText).ToList(),
                    Quentities = _.SelectNodes("//*[@class=\"amount\"]").Select(q => q.InnerText).ToList()
                }).ToList();

                var ingredients = new List<Ingredient>();

                var i = 0;
                foreach (var node in ingredientNodes)
                {
                    var currentName = node.Names[i].Replace("&#171;", "\"").Replace("&#187;", "\"");
                    var currentQuantity = node.Quentities[i].Replace("&frac12;", "0,5").Replace("&frac14;", "0,25").Replace("&frac34;", "0,75");
                    var ing = Context.Ingredients.First(g => g.Product.Name == currentName & g.Quantity == currentQuantity);
                    ingredients.Add(ing);
                    if (i < node.Names.Count)
                    {
                        i++;
                    }
                }

                var recipe = new Recipe()
                {
                    Name = step.SelectSingleNode("//*[@class=\"fn s-recipe-name\"]").InnerText.Replace("&nbsp;", " "),
                    RecipeSteps = recipeSteps,
                    Ingredients = ingredients
                };

                Context.Recipes.Add(recipe);
            }

            Context.SaveChanges();
        }