/// <summary> /// This method deletes recipe and every ingredient of that recipe from database. /// </summary> /// <param name="recipeToDelete">Recipe to be deleted.</param> /// <returns>True if deleted, false if not.</returns> public bool DeleteRecipe(vwRecipe recipeToDelete) { try { using (RecipesDBEntities context = new RecipesDBEntities()) { //finding recipe with forwarded id tblRecipe recipe = context.tblRecipes.Where(x => x.RecipeId == recipeToDelete.RecipeId).FirstOrDefault(); //finding ingredients with forwarded id List <tblIngredient> ingredients = context.tblIngredients.Where(x => x.RecipeId == recipeToDelete.RecipeId).ToList(); //removing ingredients foreach (var item in ingredients) { context.tblIngredients.Remove(item); context.SaveChanges(); } context.tblRecipes.Remove(recipe); context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
public ActionResult Create([Bind(Exclude = "Id")] Recipe recipe) { if (ModelState.IsValid) { db.Recipes.Add(recipe); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(recipe)); }
/// <summary> /// This method adds new recipe to DbSet and then saves changes to database. /// </summary> /// <param name="recipeToAdd">Recipe to be added.</param> /// <returns>True if created, false if not.</returns> public bool CreateRecipe(vwRecipe recipeToAdd, out int recipeId) { try { using (RecipesDBEntities context = new RecipesDBEntities()) { tblRecipe recipe = new tblRecipe { Author = recipeToAdd.Author, DateOfCreation = DateTime.Now, Description = recipeToAdd.Description, NumberOfPersons = recipeToAdd.NumberOfPersons, RecipeName = recipeToAdd.RecipeName, Type = recipeToAdd.Type, UserId = recipeToAdd.UserId }; context.tblRecipes.Add(recipe); context.SaveChanges(); recipeId = recipe.RecipeId; return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); recipeId = 0; return(false); } }
/// <summary> /// This method add ingredient of recipe. /// </summary> /// <param name="ingredient">Ingredient to be added.</param> /// <returns>True if created, false if not.</returns> public bool AddIngredient(vwIngredient ingredientToAdd, out int ingredientId) { try { using (RecipesDBEntities context = new RecipesDBEntities()) { tblIngredient ingredient = new tblIngredient { IngredientName = ingredientToAdd.IngredientName, Quantity = ingredientToAdd.Quantity, RecipeId = ingredientToAdd.RecipeId }; context.tblIngredients.Add(ingredient); context.SaveChanges(); ingredientId = ingredient.IngredientId; return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); ingredientId = 0; return(false); } }
public void SaveRecipe(Recipe recipe) { if (recipe.Id == 0) { context.Recipe.Add(recipe); } else { Recipe dbEntry = context.Recipe.Find(recipe.Id); if (dbEntry != null) { dbEntry.Name = recipe.Name; dbEntry.ShortDescryption = recipe.ShortDescryption; dbEntry.Components = recipe.Components; dbEntry.Implementations = recipe.Implementations; dbEntry.Category = recipe.Category; dbEntry.ImageData = recipe.ImageData; dbEntry.ImageMimeType = recipe.ImageMimeType; } } context.SaveChanges(); }
/// <summary> /// This method changes time of recept creation and saves changes to database. /// </summary> /// <param name="recipe">Recipe.</param> /// <returns>True if recipe is edited, false if not.</returns> public bool ConfirmRecipe(vwRecipe recipeToConfirm) { try { using (RecipesDBEntities context = new RecipesDBEntities()) { tblRecipe recipe = context.tblRecipes.Where(x => x.RecipeId == recipeToConfirm.RecipeId).FirstOrDefault(); recipe.DateOfCreation = DateTime.Now; context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method deletes ingredient. /// </summary> /// <param name="ingredient">Ingredient to be deleted.</param> public bool DeleteIngredient(vwIngredient ingredient) { try { using (RecipesDBEntities context = new RecipesDBEntities()) { tblIngredient ingredientToDelete = context.tblIngredients.Where(x => x.IngredientId == ingredient.IngredientId).FirstOrDefault(); context.tblIngredients.Remove(ingredientToDelete); context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method edits data of ingredient. /// </summary> /// <param name="ingredientToEdit">Ingredient to be edited.</param> /// <returns>True if edited, false if not.</returns> public bool EditIngredient(vwIngredient ingredientToEdit) { try { using (RecipesDBEntities context = new RecipesDBEntities()) { tblIngredient ingredient = context.tblIngredients.Where(x => x.IngredientId == ingredientToEdit.IngredientId).FirstOrDefault(); ingredient.IngredientName = ingredientToEdit.IngredientName; ingredient.Quantity = ingredientToEdit.Quantity; context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
// Method that add User to database public bool AddUser(vwUser user) { try { using (RecipesDBEntities context = new RecipesDBEntities()) { tblUser newUser = new tblUser(); newUser.NameAndSurname = user.NameAndSurname; newUser.Username = user.Username; newUser.Password = SecurePasswordHasher.Hash(user.Password); context.tblUsers.Add(newUser); context.SaveChanges(); user.UserId = newUser.UserId; return(true); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method edits data of recipe and save changes to database.. /// </summary> /// <param name="recipeToEdit">Recipe to be edited.</param> /// <returns>True if edited, false if not.</returns> public bool EditRecipe(vwRecipe recipeToEdit) { try { using (RecipesDBEntities context = new RecipesDBEntities()) { tblRecipe recipe = context.tblRecipes.Where(x => x.RecipeId == recipeToEdit.RecipeId).FirstOrDefault(); recipe.RecipeName = recipeToEdit.RecipeName; recipe.Author = recipeToEdit.Author; recipe.DateOfCreation = DateTime.Now; recipe.Description = recipeToEdit.Description; recipe.NumberOfPersons = recipeToEdit.NumberOfPersons; recipe.Type = recipeToEdit.Type; recipe.UserId = recipeToEdit.UserId; context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
public int Create(Ratings rating) { db.Ratings.Add(rating); db.SaveChanges(); return(rating.RatingID); }
public int Create(Recipe recipe) { db.Recipe.Add(recipe); db.SaveChanges(); return(recipe.RecipeID); }
public void Create(T entity) { table.Add(entity); db.SaveChanges(); }
public void Create(T item) { Context.Set <T>().Add(item); Context.SaveChanges(); }
public void Create(Instructions instruction) { db.Instructions.Add(instruction); db.SaveChanges(); }
public int Create(Comments comment) { db.Comments.Add(comment); db.SaveChanges(); return(comment.CommentID); }
public int Create(Ingredients ingredient) { db.Ingredients.Add(ingredient); db.SaveChanges(); return(ingredient.IngredientID); }