コード例 #1
0
        /// <summary>
        /// Used to delete the list of bevarages found in the SearchByAndPossiblyDelete method
        /// </summary>
        /// <param name="queryBeverages"></param>
        /// <returns>bool</returns>
        public bool DeleteItem(List <Beverage> queryBeverages)
        {
            try
            {
                //Bool to hold if the item(s) was successfully deleted of not
                bool itemDeletedBool = false;
                //Count used to find out how many times that the database did not correctly delete the beverage
                int count = 0;
                //Open the database
                BeverageJMartinEntities beverageEntities = new BeverageJMartinEntities();
                //Beverage to temporarily hold a beverage
                Beverage tempBeverage = new Beverage();

                foreach (Beverage beverage in queryBeverages)
                {
                    //hold the current beverage in the list than delete if from the list
                    tempBeverage = beverageEntities.Beverages.Find(beverage.id);
                    beverageEntities.Beverages.Remove(tempBeverage);

                    //Check if the beverage just deleted is in the database
                    tempBeverage = beverageEntities.Beverages.Find(beverage.id);
                    //If the bevarage is still in the database add one to the count
                    if (tempBeverage == null)
                    {
                        count++;
                    }
                }
                //If the count is not less than one, than at least one of the items was not succesffuly deleted
                if (count < 1)
                {
                    //All items successfully added so save the changes
                    beverageEntities.SaveChanges();
                    itemDeletedBool = true;
                }
                else
                {
                    //An item was not added correctly so do not save the changes and print out an error message.
                    UserInterface ui = new UserInterface();
                    ui.ErrorInDeleting();
                }

                return(itemDeletedBool);
            }
            catch (Exception e)
            {
                UserInterface ui = new UserInterface();
                ui.OutputAString(e.ToString() + " " + e.StackTrace);
                return(false);
            }
        }