/// <summary>
        /// Deletes recipe
        /// </summary>
        /// <param name="recipeID">the recipe that is being deleted</param>
        public void DeleteRecipe(int recipeID)
        {
            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    for (int i = 0; i < GetAllRecipes().Count; i++)
                    {
                        if (GetAllRecipes().ToList()[i].RecipeID == recipeID)
                        {
                            // Remove all recipe ingredients before the recipe
                            int selectedRecipeIngrediantAmountCount = GetAllSelectedRecipeIngrediantAmount(recipeID).Count;

                            for (int j = 0; j < selectedRecipeIngrediantAmountCount; j++)
                            {
                                tblIngredientAmount ingAmountToDelete = (from ss in context.tblIngredientAmounts where ss.RecipeID == recipeID select ss).First();
                                context.tblIngredientAmounts.Remove(ingAmountToDelete);
                                context.SaveChanges();
                            }

                            tblRecipe recipeToDelete = (from r in context.tblRecipes where r.RecipeID == recipeID select r).First();
                            context.tblRecipes.Remove(recipeToDelete);
                            context.SaveChanges();

                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
            }
        }
        /// <summary>
        /// Adds ingredients to shopping list to the database
        /// </summary>
        /// <param name="shoppingBasket">The shopping basket we are adding or editing</param>
        /// <returns>The new or edited shopping basket</returns>
        public tblShoppingBasket AddShoppingList(tblShoppingBasket shoppingBasket)
        {
            // Delete ingredient if it was added earlier
            if (IsIngredientAdded(shoppingBasket) != 0)
            {
                DeleteShoppingBasket(IsIngredientAdded(shoppingBasket));
            }

            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    if (shoppingBasket.ShoppingBasketID == 0)
                    {
                        tblShoppingBasket newShoppingBasket = new tblShoppingBasket
                        {
                            UserID       = LoggedGuest.ID,
                            IngredientID = shoppingBasket.IngredientID,
                            Amount       = shoppingBasket.Amount
                        };

                        if (LoggedGuest.ID == 0)
                        {
                            newShoppingBasket.UserID = null;
                        }

                        context.tblShoppingBaskets.Add(newShoppingBasket);
                        context.SaveChanges();
                        shoppingBasket.ShoppingBasketID = newShoppingBasket.ShoppingBasketID;

                        return(shoppingBasket);
                    }
                    else
                    {
                        tblShoppingBasket shoppingBasketEdit = (from ss in context.tblShoppingBaskets where ss.ShoppingBasketID == shoppingBasket.ShoppingBasketID select ss).First();

                        if (LoggedGuest.ID == 0)
                        {
                            shoppingBasketEdit.UserID = shoppingBasket.UserID;
                        }
                        else
                        {
                            shoppingBasketEdit.UserID = LoggedGuest.ID;
                        }

                        shoppingBasketEdit.IngredientID = shoppingBasket.IngredientID;
                        shoppingBasketEdit.Amount       = shoppingBasket.Amount;

                        context.SaveChanges();

                        return(shoppingBasket);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
        public void AddIngredientStorage(tblShoppingBasket item)
        {
            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    if (IngredientExistInStorage(item.IngredientID, LoggedGuest.ID) == false)
                    {
                        tblIngredientStorage newStorage = new tblIngredientStorage
                        {
                            UserID       = LoggedGuest.ID,
                            IngredientID = item.IngredientID,
                            Amount       = item.Amount
                        };

                        if (LoggedGuest.ID == 0)
                        {
                            newStorage.UserID = null;
                        }

                        context.tblIngredientStorages.Add(newStorage);
                        context.SaveChanges();
                    }
                    else
                    {
                        tblIngredientStorage storageEdit = (from ss in context.tblIngredientStorages where ss.IngredientID == item.IngredientID where ss.UserID == item.UserID select ss).First();

                        if (LoggedGuest.ID == 0)
                        {
                            storageEdit.UserID = item.UserID;
                        }
                        else
                        {
                            storageEdit.UserID = LoggedGuest.ID;
                        }

                        storageEdit.IngredientID = item.IngredientID;
                        storageEdit.Amount       = item.Amount + storageEdit.Amount;

                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
            }
        }
 /// <summary>
 /// Get all data about all ingredients in recipes from the database
 /// </summary>
 /// <returns>The list of all ingredients from recipes</returns>
 public List <tblIngredientAmount> GetAllRecipeIngrediant()
 {
     try
     {
         using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
         {
             List <tblIngredientAmount> list = new List <tblIngredientAmount>();
             list = (from x in context.tblIngredientAmounts select x).ToList();
             return(list);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
 public List <vwRecipe> GetAllRecipesVW()
 {
     try
     {
         using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
         {
             List <vwRecipe> list = new List <vwRecipe>();
             list = (from x in context.vwRecipes select x).ToList();
             return(list);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
        /// <summary>
        /// Adds ingredients to recipes to the database
        /// </summary>
        /// <param name="ingredientAmount">The ingredient Amount ID we are adding or editing</param>
        /// <returns>The new or edited ingredient amount</returns>
        public tblIngredientAmount AddIngredientAmount(tblIngredientAmount ingredientAmount)
        {
            // Delete ingredient if it was added earlier
            if (IsIngredientAdded(ingredientAmount) != 0)
            {
                DeleteIngredientAmount(IsIngredientAdded(ingredientAmount));
            }

            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    if (ingredientAmount.IngredientAmountID == 0)
                    {
                        tblIngredientAmount newIngredientAmount = new tblIngredientAmount
                        {
                            RecipeID     = ingredientAmount.RecipeID,
                            IngredientID = ingredientAmount.IngredientID,
                            Amount       = ingredientAmount.Amount
                        };

                        context.tblIngredientAmounts.Add(newIngredientAmount);
                        context.SaveChanges();
                        ingredientAmount.IngredientAmountID = newIngredientAmount.IngredientAmountID;

                        return(ingredientAmount);
                    }
                    else
                    {
                        tblIngredientAmount ingredientAmountEdit = (from ss in context.tblIngredientAmounts where ss.IngredientAmountID == ingredientAmount.IngredientAmountID select ss).First();
                        ingredientAmountEdit.RecipeID     = ingredientAmount.RecipeID;
                        ingredientAmountEdit.IngredientID = ingredientAmount.IngredientID;
                        ingredientAmountEdit.Amount       = ingredientAmount.Amount;

                        context.SaveChanges();

                        return(ingredientAmount);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
 /// <summary>
 /// Get all data about all shopping basket ingredients from the database
 /// </summary>
 /// <returns>The list of all basket ingredients from shopping cart</returns>
 public List <tblShoppingBasket> GetAllShoppingBasketItems()
 {
     try
     {
         using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
         {
             List <tblShoppingBasket> list = new List <tblShoppingBasket>();
             list = (from x in context.tblShoppingBaskets select x).ToList();
             return(list);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(null);
     }
 }
Beispiel #8
0
        public tblUser AddUser(tblUser user)
        {
            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    if (user.UserID == 0)
                    {
                        tblUser newUser = new tblUser
                        {
                            UserID        = user.UserID,
                            Username      = user.Username,
                            UserPassword  = user.UserPassword,
                            FirstLastName = user.FirstLastName
                        };

                        context.tblUsers.Add(newUser);
                        context.SaveChanges();
                        user.UserID = newUser.UserID;
                        return(user);
                    }
                    else
                    {
                        tblUser userToEdit = (from r in context.tblUsers where r.UserID == user.UserID select r).First();

                        userToEdit.UserID        = user.UserID;
                        userToEdit.Username      = user.Username;
                        userToEdit.UserPassword  = user.UserPassword;
                        userToEdit.FirstLastName = user.FirstLastName;
                        context.SaveChanges();
                        return(user);
                    }
                }
            }
            catch (Exception ex)
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("Korisničko ime je već zauzeto, vratite se na prethodnu stranu i pokušajte sa drugim ili proverite da li ste dobro uneli korisničko ime.\nObratite pažnjuna velika i mala slova!", "Greška");
                System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message.ToString());
                return(null);
            }
        }
Beispiel #9
0
        public tblUser GetUserUsername(string username)
        {
            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    string usernameFromDB = (from e in context.tblUsers where e.Username == username select e.Username).FirstOrDefault();

                    if (username == usernameFromDB)
                    {
                        return((from e in context.tblUsers where e.Username == username select e).FirstOrDefault());
                    }

                    return(null);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
 /// <summary>
 /// Deletes ingredient amount
 /// </summary>
 /// <param name="ingredientAmountID">the ingredientAmount that is being deleted</param>
 public void DeleteIngredientAmount(int ingredientAmountID)
 {
     try
     {
         using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
         {
             for (int i = 0; i < GetAllRecipeIngrediant().Count; i++)
             {
                 if (GetAllRecipeIngrediant().ToList()[i].IngredientAmountID == ingredientAmountID)
                 {
                     tblIngredientAmount ingAmountToDelete = (from ss in context.tblIngredientAmounts where ss.IngredientAmountID == ingredientAmountID select ss).First();
                     context.tblIngredientAmounts.Remove(ingAmountToDelete);
                     context.SaveChanges();
                     break;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
     }
 }
 /// <summary>
 /// Deletes shopping basket
 /// </summary>
 /// <param name="shoppingBasketID">the ingredientAmount that is being deleted</param>
 public void DeleteShoppingBasket(int shoppingBasketID)
 {
     try
     {
         using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
         {
             for (int i = 0; i < GetAllShoppingBasketItems().Count; i++)
             {
                 if (GetAllShoppingBasketItems().ToList()[i].ShoppingBasketID == shoppingBasketID)
                 {
                     tblShoppingBasket shoppingBasketToDelete = (from ss in context.tblShoppingBaskets where ss.ShoppingBasketID == shoppingBasketID select ss).First();
                     context.tblShoppingBaskets.Remove(shoppingBasketToDelete);
                     context.SaveChanges();
                     break;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
     }
 }
Beispiel #12
0
        /// <summary>
        /// Search if ingredient with that ID exists in the ingredient table
        /// </summary>
        /// <param name="id">Takes the id that we want to search for</param>
        /// <returns>The ingredient</returns>
        public tblIngredient FindIngredient(int id)
        {
            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    tblIngredient result = (from x in context.tblIngredients where x.IngredientID == id select x).FirstOrDefault();

                    if (result != null)
                    {
                        return(result);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception " + ex.Message.ToString());
                return(null);
            }
        }
Beispiel #13
0
        /// <summary>
        /// Adds an ingredient to the database
        /// </summary>
        /// <param name="ingredient">The ingredient ID we are adding or editing</param>
        /// <returns>The new or edited ingredient</returns>
        public tblIngredient AddIngredient(tblIngredient ingredient)
        {
            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    tblIngredient newIngredient = new tblIngredient
                    {
                        IngredientName = ingredient.IngredientName
                    };

                    context.tblIngredients.Add(newIngredient);
                    context.SaveChanges();
                    ingredient.IngredientID = newIngredient.IngredientID;

                    return(ingredient);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
        /// <summary>
        /// Adds an recipes to the database
        /// </summary>
        /// <param name="recipe">The recipe ID we are adding or editing</param>
        /// <returns>The new or edited recipe</returns>
        public tblRecipe AddRecipe(tblRecipe recipe)
        {
            try
            {
                using (CakeRecipesDBEntities context = new CakeRecipesDBEntities())
                {
                    if (recipe.RecipeID == 0)
                    {
                        tblRecipe newRecipe = new tblRecipe();
                        newRecipe.RecipeName        = recipe.RecipeName;
                        newRecipe.RecipeType        = recipe.RecipeType;
                        newRecipe.NoPeople          = recipe.NoPeople;
                        newRecipe.RecipeDescription = recipe.RecipeDescription;

                        if (recipe.CreationDate != default(DateTime))
                        {
                            newRecipe.CreationDate = recipe.CreationDate;
                        }
                        else
                        {
                            newRecipe.CreationDate = DateTime.Now;
                        }

                        if (recipe.Changed != null)
                        {
                            newRecipe.Changed = recipe.Changed;
                        }
                        else
                        {
                            newRecipe.Changed = LoggedGuest.NameSurname;
                        }

                        if (recipe.UserID != null)
                        {
                            newRecipe.UserID = recipe.UserID;
                        }
                        else if (LoggedGuest.ID == 0)
                        {
                            newRecipe.UserID = null;
                        }
                        else
                        {
                            newRecipe.UserID = LoggedGuest.ID;
                        }

                        context.tblRecipes.Add(newRecipe);
                        context.SaveChanges();
                        recipe.RecipeID = newRecipe.RecipeID;

                        return(recipe);
                    }
                    else
                    {
                        tblRecipe recipeToEdit = (from ss in context.tblRecipes where ss.RecipeID == recipe.RecipeID select ss).First();
                        recipeToEdit.RecipeName        = recipe.RecipeName;
                        recipeToEdit.RecipeType        = recipe.RecipeType;
                        recipeToEdit.NoPeople          = recipe.NoPeople;
                        recipeToEdit.RecipeDescription = recipe.RecipeDescription;
                        recipeToEdit.CreationDate      = DateTime.Now;
                        recipeToEdit.Changed           = LoggedGuest.NameSurname;

                        if (recipe.UserID == null)
                        {
                            recipe.UserID = null;
                        }
                        else if (LoggedGuest.ID == 0)
                        {
                            recipeToEdit.UserID = recipe.UserID;
                        }
                        else
                        {
                            recipeToEdit.UserID = LoggedGuest.ID;
                        }

                        context.SaveChanges();

                        return(recipe);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }