Пример #1
0
        /// <summary>
        /// Method to add or edit ingredient into db
        /// </summary>
        /// <param name="ingredient"></param>
        /// <returns></returns>
        public tblIngredient AddIngredient(tblIngredient ingredient)
        {
            try
            {
                using (RecipeKeeperEntities context = new RecipeKeeperEntities())
                {
                    if (ingredient.ingridientId == 0)
                    {
                        //add
                        tblIngredient newIng = new tblIngredient();
                        newIng.name     = ingredient.name;
                        newIng.quantity = ingredient.quantity;
                        newIng.recipeId = ingredient.recipeId;
                        context.tblIngredients.Add(newIng);
                        context.SaveChanges();
                        ingredient.ingridientId = newIng.ingridientId;
                        return(ingredient);
                    }
                    else
                    {
                        tblIngredient ingToEdit = (from x in context.tblIngredients where x.ingridientId == ingredient.ingridientId select x).FirstOrDefault();

                        ingToEdit.name     = ingredient.name;
                        ingToEdit.quantity = ingredient.quantity;
                        context.SaveChanges();
                        return(ingredient);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message.ToString());
                return(null);
            }
        }
 /// <summary>
 /// Method to write admin credentials into Db on starting application if the list of all users are empty
 /// </summary>
 /// <returns></returns>
 private tblUser WriteAdminInDb()
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             if (UserList.Count() == 0)
             {
                 tblUser admin = new tblUser
                 {
                     username = "******",
                     password = PasswordHasher.Hash("Admin123"),
                     fullname = "Administrator",
                     role     = "admin"
                 };
                 context.tblUsers.Add(admin);
                 context.SaveChanges();
                 return(admin);
             }
             return(null);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Пример #3
0
 /// <summary>
 /// Method to show selected Recipe based on id
 /// </summary>
 /// <param name="recipeId"></param>
 /// <returns></returns>
 public tblRecipe GetSelectedRecipe(int recipeId)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             return(context.tblRecipes.Where(x => x.recipeId == recipeId).FirstOrDefault());
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Пример #4
0
 /// <summary>
 /// Gets user by forwarded username.
 /// </summary>
 /// <param name="userName">User's username</param>
 /// <param name="pass">User's password</param>
 /// <returns>User.</returns>
 public tblUser GetUserByUsernameAndPass(string userName, string pass)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             return(context.tblUsers.Where(x => x.username == userName && x.password == pass).FirstOrDefault());
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Пример #5
0
 /// <summary>
 /// Check if user is registered by forwarded username
 /// </summary>
 /// <param name="username"></param>
 /// <returns>true if registered, false if not</returns>
 public bool IsRegisteredUser(string username)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             bool result = (from x in context.tblUsers where x.username == username select x).Any();
             return(result);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception " + ex.Message.ToString());
         return(false);
     }
 }
Пример #6
0
 /// <summary>
 /// Method to get all ingredient from selected recipe
 /// </summary>
 /// <param name="recipeId"></param>
 /// <returns></returns>
 public List <tblIngredient> AllIngredientForRecipe(int recipeId)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             List <tblIngredient> r = (from x in context.tblIngredients where x.recipeId == recipeId select x).ToList();
             return(r);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Пример #7
0
 /// <summary>
 /// Check if ingredint is in recipe
 /// </summary>
 /// <param name="recipeId"></param>
 /// <param name="ingredient"></param>
 /// <returns></returns>
 public bool IsRecipeIngredient(int recipeId, string ingredient)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             bool r = (from x in context.tblIngredients where x.recipeId == recipeId && x.name == ingredient select x).Any();
             return(r);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(false);
     }
 }
Пример #8
0
 /// <summary>
 /// Method to get all recipes from db
 /// </summary>
 /// <returns>list of Recipes</returns>
 public List <tblRecipe> GetAllRecipes()
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             List <tblRecipe> list = new List <tblRecipe>();
             list = (from x in context.tblRecipes select x).ToList();
             return(list);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Пример #9
0
 /// <summary>
 /// This method deletes ingredient.
 /// </summary>
 /// <param name="ingredient">Ingredient to be deleted.</param>
 public bool DeleteIngredient(tblIngredient ingredient)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             tblIngredient ingredientToDelete = context.tblIngredients.Where(x => x.ingridientId == ingredient.ingridientId).FirstOrDefault();
             context.tblIngredients.Remove(ingredientToDelete);
             context.SaveChanges();
             return(true);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(false);
     }
 }
Пример #10
0
 /// <param name="recipe"></param>
 /// <returns></returns>
 public tblRecipe AddRecipe(tblRecipe recipe)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             //add
             if (recipe.recipeId == 0)
             {
                 tblRecipe newRecipe = new tblRecipe();
                 newRecipe.authorId        = recipe.authorId;
                 newRecipe.creationDate    = DateTime.Now;
                 newRecipe.description     = recipe.description;
                 newRecipe.numberOfPersons = recipe.numberOfPersons;
                 newRecipe.title           = recipe.title;
                 newRecipe.type            = recipe.type;
                 context.tblRecipes.Add(newRecipe);
                 context.SaveChanges();
                 recipe.recipeId = newRecipe.recipeId;
                 return(recipe);
             }
             //edit
             else
             {
                 tblRecipe recipeToEdit = (from x in context.tblRecipes where x.recipeId == recipe.recipeId select x).FirstOrDefault();
                 recipeToEdit.authorId        = recipe.authorId;
                 recipeToEdit.creationDate    = DateTime.Now;
                 recipeToEdit.description     = recipe.description;
                 recipeToEdit.numberOfPersons = recipe.numberOfPersons;
                 recipeToEdit.title           = recipe.title;
                 recipeToEdit.type            = recipe.type;
                 context.SaveChanges();
                 return(recipe);
             }
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception: " + ex.Message.ToString());
         return(null);
     }
 }
Пример #11
0
 /// <summary>
 /// Method to edit the user
 /// </summary>
 /// <param name="userName"></param>
 /// <param name="pass"></param>
 /// <returns>user</returns>
 public tblUser EditUser(tblUser user)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             tblUser userToEdit = (from x in context.tblUsers where x.userId == user.userId select x).First();
             userToEdit.fullname = user.fullname;
             userToEdit.username = user.username;
             userToEdit.password = PasswordHasher.Hash(user.password);
             userToEdit.role     = "user";
             userToEdit.userId   = user.userId;
             context.SaveChanges();
             return(user);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Пример #12
0
 /// <summary>
 /// Adds the user
 /// </summary>
 /// <param name="userName"></param>
 /// <param name="pass"></param>
 /// <returns>user</returns>
 public tblUser AddUser(string userName, string pass)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             tblUser user = new tblUser();
             user.fullname = userName;
             user.username = userName;
             user.password = PasswordHasher.Hash(pass);
             user.role     = "user";
             context.tblUsers.Add(user);
             context.SaveChanges();
             return(user);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Пример #13
0
 /// <summary>
 /// Method to delete recipe
 /// </summary>
 /// <param name="recipe"></param>
 public void DeleteRecipe(tblRecipe recipe)
 {
     try
     {
         using (RecipeKeeperEntities context = new RecipeKeeperEntities())
         {
             //first, we must delete ingredients with this recipeId
             List <tblIngredient> ing = AllIngredientForRecipe(recipe.recipeId);
             for (int i = 0; i < ing.Count; i++)
             {
                 DeleteIngredient(ing[i]);
             }
             //now we can remove our Recipe
             tblRecipe toDelete = (from u in context.tblRecipes where u.recipeId == recipe.recipeId select u).First();
             context.tblRecipes.Remove(toDelete);
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString());
     }
 }