Пример #1
0
        // DELETE: api/Grocery/5
        public IHttpActionResult Delete(int id)
        {
            if (id == 9999)
            {
                using (var context = new MarcDbEntities())
                {
                    context.GroceryIngredients.RemoveRange(context.GroceryIngredients.Where(i => i.GroceryId == 1));
                    context.GroceryRecipeLists.RemoveRange(context.GroceryRecipeLists.Where(i => i.GroceryId == 1));
                    //db.People.RemoveRange(db.People.Where(x => x.State == "CA"));


                    //var ids = context.GroceryIngredients.Where(g => g.GroceryId == 1).Select(g => g.Id).ToList();

                    //foreach (var tempId in ids)
                    //{
                    //    context.GroceryIngredients.Remove(context.GroceryIngredients.FirstOrDefault(r => r.Id == tempId));
                    //}

                    context.SaveChanges();
                }

                return(Ok());
            }

            return(NotFound());
        }
Пример #2
0
        public IHttpActionResult ModifyGroceryIngredientCategory(int id, int categoryId)
        {
            using (var context = new MarcDbEntities())
            {
                var groceryIngredients = context.GroceryIngredients.Where(g => g.GroceryId == 1);

                if (groceryIngredients.Any())
                {
                    var ingredient = groceryIngredients.FirstOrDefault(i => i.IngredientId == id).Ingredient;
                    var category   = context.IngredientCategories.FirstOrDefault(i => i.Id == categoryId);

                    if (category != null)
                    {
                        ingredient.IngredientCategory = category;
                        context.SaveChanges();
                        return(Ok());
                    }
                    else
                    {
                        return(NotFound());
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
        }
Пример #3
0
        public void InsertGroceryList()
        {
            using (var context = new MarcDbEntities())
            {
                var list = context.GroceryLists.Add(new GroceryList()
                {
                    Name = "Main"
                });

                context.SaveChanges();
            }
        }
Пример #4
0
        // GET: api/Grocery/5
        //public string Get(int id)
        //{
        //    return "value";
        //}

        /// <summary>
        /// Take a recipe id and add all ingredients to grocery list
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IHttpActionResult Post(int id)
        {
            var recipeId  = id;
            var groceryId = 1;

            using (var context = new MarcDbEntities())
            {
                var recipe = context.Recipes.FirstOrDefault(r => r.Id == recipeId);

                var ingredients = recipe.RecipeIngredients.Select(r => r.Ingredient);

                var groceryList = context.GroceryLists.FirstOrDefault();

                // Add recipe to db for tracking
                if (!context.GroceryRecipeLists.Any(gr => gr.GroceryId == groceryList.Id && gr.RecipeId == id))
                {
                    var groceryRecipe = new GroceryRecipeList()
                    {
                        GroceryId = groceryList.Id,
                        RecipeId  = recipe.Id
                    };

                    context.GroceryRecipeLists.Add(groceryRecipe);
                }

                // Loop through all ingredients and if it doesn't exist add it
                foreach (var ingredient in ingredients)
                {
                    if (!groceryList.GroceryIngredients.Any(i => i.Ingredient.Id == ingredient.Id && i.GroceryId == groceryId))
                    {
                        groceryList.GroceryIngredients.Add(new GroceryIngredient()
                        {
                            Ingredient = ingredient
                        });
                    }
                    else
                    {
                        // It's in the grocery list change the done status no matter what
                        var groceryIngredient = groceryList.GroceryIngredients.FirstOrDefault(i => i.Ingredient.Id == ingredient.Id && i.GroceryId == groceryId);
                        groceryIngredient.Done = false;
                    }
                }

                context.SaveChanges();

                return(Ok());
            }
        }
Пример #5
0
        public void InsertIngredient()
        {
            using (var context = new MarcDbEntities())
            {
                context.Ingredients.Add(new Ingredient()
                {
                    Name = "Rosemary"
                });
                context.Ingredients.Add(new Ingredient()
                {
                    Name = "Butter"
                });

                context.SaveChanges();
            }
        }
Пример #6
0
        public IHttpActionResult ModifyGroceryIngredientQuantity(int id, int quantity)
        {
            using (var context = new MarcDbEntities())
            {
                var groceryIngredients = context.GroceryIngredients.Where(g => g.GroceryId == 1);

                if (groceryIngredients.Any())
                {
                    var ingredient = groceryIngredients.FirstOrDefault(i => i.IngredientId == id);
                    ingredient.Quantity = quantity;

                    context.SaveChanges();

                    return(Ok());
                }
                else
                {
                    return(NotFound());
                }
            }
        }
Пример #7
0
        // post is to create
        // put is to create or update

        /// <summary>
        /// Mark ingredient as done or not.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        public IHttpActionResult Put(int id, [FromBody] bool value)
        {
            var recipeId = id;

            using (var context = new MarcDbEntities())
            {
                var groceryIngredients = context.GroceryIngredients.Where(g => g.GroceryId == 1);

                if (groceryIngredients.Any())
                {
                    var ingredient = groceryIngredients.FirstOrDefault(i => i.IngredientId == id);
                    ingredient.Done = value;

                    context.SaveChanges();

                    return(Ok());
                }
                else
                {
                    return(NotFound());
                }
            }
        }
Пример #8
0
        public void InsertRecipeToGroceryList()
        {
            using (var context = new MarcDbEntities())
            {
                var recipe = context.Recipes.FirstOrDefault(r => r.Id == 1);

                var ingredients = recipe.RecipeIngredients.Select(r => r.Ingredient);

                var groceryList = context.GroceryLists.FirstOrDefault();

                foreach (var ingredient in ingredients)
                {
                    if (!groceryList.GroceryIngredients.Any(i => i.Ingredient.Id == ingredient.Id))
                    {
                        groceryList.GroceryIngredients.Add(new GroceryIngredient()
                        {
                            Ingredient = ingredient
                        });
                    }
                }

                context.SaveChanges();
            }
        }
Пример #9
0
        public void InsertRecipe()
        {
            using (var context = new MarcDbEntities())
            {
                var rosemary = context.Ingredients.FirstOrDefault(r => r.Name == "Butter");
                var butter   = context.Ingredients.FirstOrDefault(r => r.Name == "Meatballs");

                var newRecipe = context.Recipes.Add(new Recipe()
                {
                    Name = "Butter Balls"
                });

                context.RecipeIngredients.Add(new RecipeIngredient()
                {
                    Recipe = newRecipe, Ingredient = rosemary
                });
                context.RecipeIngredients.Add(new RecipeIngredient()
                {
                    Recipe = newRecipe, Ingredient = butter
                });

                context.SaveChanges();
            }
        }
Пример #10
0
        /// <summary>
        /// Take an ingredient and add it to grocery list
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IHttpActionResult Post([FromBody] SimpleIngredient value)
        {
            // Add by ID
            // Add by name if no ID present
            using (var context = new MarcDbEntities())
            {
                // Let's try by id first then name
                if (value.Id != -1)
                {
                    var groceryIngredients = context.GroceryIngredients.Where(g => g.IngredientId == value.Id);

                    if (!groceryIngredients.Any())
                    {
                        var ingredient = context.Ingredients.FirstOrDefault(i => i.Id == value.Id);
                        context.GroceryIngredients.Add(new GroceryIngredient()
                        {
                            Ingredient = ingredient, GroceryId = 1
                        });

                        context.SaveChanges();

                        return(Ok());
                    }
                    else
                    {
                        // Already exists
                        return(Ok());
                    }
                }
                else
                {
                    var groceryIngredients = context.GroceryIngredients.Where(g => g.Ingredient.Name == value.Name);

                    // If this ingredient in the grocery list?
                    if (!groceryIngredients.Any())
                    {
                        // No it's not see if the ingredient actually exists
                        Ingredient ingredient = context.Ingredients.FirstOrDefault(i => i.Name == value.Name);

                        if (ingredient == null)
                        {
                            // Created ingredient
                            ingredient = context.Ingredients.Add(new Ingredient {
                                Name = value.Name
                            });
                            context.SaveChanges();
                        }

                        context.GroceryIngredients.Add(new GroceryIngredient()
                        {
                            Ingredient = ingredient, GroceryId = 1
                        });

                        context.SaveChanges();

                        return(Ok());
                    }
                    else
                    {
                        // Already exists
                        return(Ok());
                    }
                }
            }
        }
Пример #11
0
        // POST: api/Recipe I think this is the way to go
        /// <summary>
        /// Add ingredients to a recipe.
        /// </summary>
        /// <param name="recipe"></param>
        /// <returns></returns>
        public IHttpActionResult Post([FromBody] SimpleRecipe recipe)
        {
            using (var context = new MarcDbEntities())
            {
                if (!context.Recipes.Any(r => r.Id == recipe.Id))
                {
                    var temp = context.Recipes.Add(new Recipe()
                    {
                        Name = recipe.Name
                    });
                    context.SaveChanges();
                    recipe.Id = temp.Id;
                }

                var recipeInDB = context.Recipes.FirstOrDefault(r => r.Id == recipe.Id);

                List <RecipeIngredient> listOfRecipeIngredients = new List <RecipeIngredient>();

                // Wipe out all ingredients for the recipe
                foreach (var recipeIngredient in recipeInDB.RecipeIngredients)
                {
                    listOfRecipeIngredients.Add(recipeIngredient);
                }

                foreach (var recipeIngredient in listOfRecipeIngredients)
                {
                    context.RecipeIngredients.Remove(recipeIngredient);
                }

                context.SaveChanges();
                var category = context.IngredientCategories.FirstOrDefault(i => i.Id == 5);
                foreach (var ingredient in recipe.Ingredients)
                {
                    Ingredient ingredientInDB;

                    // If no id supplied, try to find by name or create a new entry in db
                    if (ingredient.Id == -1)
                    {
                        ingredientInDB = context.Ingredients.FirstOrDefault(r => r.Name.ToLower() == ingredient.Name.ToLower());

                        if (ingredientInDB == null)
                        {
                            ingredientInDB = context.Ingredients.Add(new Ingredient()
                            {
                                Name = ingredient.Name, IngredientCategory = category
                            });
                        }
                    }
                    else
                    {
                        ingredientInDB = context.Ingredients.FirstOrDefault(r => r.Id == ingredient.Id);
                    }

                    if (ingredientInDB == null)
                    {
                        ingredientInDB = context.Ingredients.Add(new Ingredient()
                        {
                            Name = ingredientInDB.Name, IngredientCategory = category
                        });
                    }

                    //if (!recipeInDB.RecipeIngredients.Any(i => i.IngredientId == ingredientInDB.Id))
                    //{
                    recipeInDB.RecipeIngredients.Add(new RecipeIngredient()
                    {
                        Ingredient = ingredientInDB
                    });
                    //}
                }

                context.SaveChanges();

                return(Ok());
            }
        }