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()); } } }
// 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()); }
// GET: api/Recipe/5 /// <summary> /// Get recipe by id. /// </summary> /// <param name="recipeId"></param> /// <returns></returns> public IHttpActionResult Get(int id) { int recipeId = id; using (var context = new MarcDbEntities()) { //context.Configuration.ProxyCreationEnabled = false; if (context.Recipes.Any(r => r.Id == recipeId)) { var recipe = (context.Recipes.Where(r => r.Id == recipeId) .Select(r => new SimpleRecipe { Name = r.Name, Id = r.Id, Ingredients = r.RecipeIngredients .Select(i => new SimpleIngredient { Id = i.Ingredient.Id, Name = i.Ingredient.Name }) })).FirstOrDefault(); return(Ok(recipe)); } else { return(NotFound()); } } }
// GET: api/Recipe /// <summary> /// Get all recipes /// </summary> /// <returns></returns> public IHttpActionResult Get() { using (var context = new MarcDbEntities()) { //context.Configuration.ProxyCreationEnabled = false; if (context.Recipes.Any()) { var recipes = (context.Recipes .Select(r => new SimpleRecipe { Name = r.Name, Id = r.Id, Ingredients = r.RecipeIngredients .Select(i => new SimpleIngredient { Id = i.Ingredient.Id, Name = i.Ingredient.Name }) })).ToList(); return(Ok(recipes)); } else { return(NotFound()); } } }
public void GetRecipe() { using (var context = new MarcDbEntities()) { var coo = context.Recipes.Count(); var adfhgdsg = context.Recipes.FirstOrDefault(r => r.Id == 1); } }
public void InsertGroceryList() { using (var context = new MarcDbEntities()) { var list = context.GroceryLists.Add(new GroceryList() { Name = "Main" }); context.SaveChanges(); } }
// PUT: api/Recipe/5 //public void Put(int id, [FromBody]string value) //{ //} // DELETE: api/Recipe/5 /// <summary> /// Delete recipe by id. /// </summary> /// <param name="id"></param> /// <returns></returns> public IHttpActionResult Delete(int id) { using (var context = new MarcDbEntities()) { if (!context.Recipes.Any(r => r.Id == id)) { return(NotFound()); } context.Recipes.Remove(context.Recipes.FirstOrDefault(r => r.Id == id)); return(Ok()); } }
// 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()); } }
public void InsertIngredient() { using (var context = new MarcDbEntities()) { context.Ingredients.Add(new Ingredient() { Name = "Rosemary" }); context.Ingredients.Add(new Ingredient() { Name = "Butter" }); context.SaveChanges(); } }
public IHttpActionResult Get(string filter) { using (var context = new MarcDbEntities()) { var data = (context.Ingredients .Where(i => i.Name.Contains(filter)) .Select(i => new SimpleIngredient { Id = i.Id, Name = i.Name, Category = i.IngredientCategory.Category, Reoccurring = i.Reoccurring.HasValue ? i.Reoccurring.Value : false }) ).ToList(); return(Ok(data)); } }
public IHttpActionResult GetRelatedRecipes(int id) { using (var context = new MarcDbEntities()) { var recipes = context.GroceryRecipeLists.Where(g => g.GroceryId == id); if (recipes.Any()) { var recipeList = recipes.Select(r => r.Recipe.Name).ToList(); return(Ok(recipeList)); } else { return(NotFound()); } } }
public IHttpActionResult GetGroceryIngredient(int id) { using (var context = new MarcDbEntities()) { var data = (context.GroceryIngredients .Where(i => i.GroceryId == 1 && i.IngredientId == id) .Select(i => new SimpleIngredient { Id = i.IngredientId, Name = i.Ingredient.Name, Category = i.Ingredient.IngredientCategory == null ? "Other" : i.Ingredient.IngredientCategory.Category, Reoccurring = i.Ingredient.Reoccurring.HasValue ? i.Ingredient.Reoccurring.Value : false, Quantity = i.Quantity.HasValue ? i.Quantity.Value : 1, CategoryId = i.Ingredient.IngredientCategory == null ? 0 : i.Ingredient.IngredientCategory.Id }) ).FirstOrDefault(); return(Ok(data)); } }
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()); } } }
// GET: api/Grocery public IHttpActionResult Get() { using (var context = new MarcDbEntities()) { var data = (context.GroceryLists .Select(g => new SimpleGrocery() { Id = g.Id, Name = g.Name, Ingredients = g.GroceryIngredients .Select(i => new SimpleIngredient { Id = i.Ingredient.Id, Name = i.Ingredient.Name, Done = i.Done.HasValue ? i.Done.Value : false, Category = i.Ingredient.IngredientCategory.Category, Quantity = i.Quantity.HasValue ? i.Quantity.Value : 1 }) })).FirstOrDefault(); return(Ok(data)); } }
// 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()); } } }
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(); } }
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(); } }
// 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()); } }
/// <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()); } } } }