/// <summary> /// Method to search any of the Bevarage properties for the data specified by the user /// and then delete those items if the user specifed deletion. /// </summary> /// <param name="searchFor">string</param> /// <param name="propertyName">string</param> /// <param name="delete">bool</param> /// <returns>string</returns> public string SearchByAndPossiblyDelete(string searchFor, string propertyName, bool delete) { try { //Open the database BeverageJMartinEntities beveageEntities = new BeverageJMartinEntities(); //Create an empty list to hold the Beverages List <Beverage> queryBeverages = null; //Bool of if anything is found in the search starts out as false bool found = false; //Create the string to hold the found list string listString = "*************************************************************************************************" + Environment.NewLine; //From the property name create a search to be done switch (propertyName) { case nameof(Beverage.id): queryBeverages = beveageEntities.Beverages.Where( bev => bev.id.ToLower() == searchFor.ToLower() ).ToList(); break; case nameof(Beverage.name): queryBeverages = beveageEntities.Beverages.Where( bev => bev.name.ToLower().Contains(searchFor.ToLower()) ).ToList(); break; case nameof(Beverage.pack): queryBeverages = beveageEntities.Beverages.Where( bev => bev.pack.ToLower().Contains(searchFor.ToLower()) ).ToList(); break; case nameof(Beverage.price): decimal searchForDec = 0; //create a decimal value from the string value for the search if (Decimal.TryParse(searchFor, out searchForDec)) { queryBeverages = beveageEntities.Beverages.Where( bev => bev.price == searchForDec ).ToList(); } break; case nameof(Beverage.active): bool tempBool; //Create a bool value from the string value to be used for the search if (searchFor == "True") { tempBool = true; } else { tempBool = false; } queryBeverages = beveageEntities.Beverages.Where( bev => bev.active == tempBool ).ToList(); break; } //Find out if anything was found in the search if (queryBeverages != null) { //As something was found, return the formated beverage information for each beverage found //and make the found bool true. foreach (Beverage beverage in queryBeverages) { listString += FormatBeverageSting(beverage) + Environment.NewLine; found = true; } } if (!found) { //As nothing was found return searched for term was not found. listString += searchFor + " was not found." + Environment.NewLine; } else { //if somthing was found and you want to delete the items ask to confirm than //delete the items. if (delete) { //Make an instance of UserInterface UserInterface ui = new UserInterface(); //Output to the user the list of items found ui.OutputAString(listString); //Find out if the user is sure to delete the list of items found. if (ui.AreYouSure()) { //Call the method to delete the list an inform the user if it was deleted or not. if (DeleteItem(queryBeverages)) { listString += Environment.NewLine + "The list was deleted" + Environment.NewLine; } else { listString += Environment.NewLine + "The list was NOT deleted do to an error with the database!" + Environment.NewLine; } } else { //As the user does not want to delete the items exit out and return a blank list. listString = "Not deleted"; } } listString += "*************************************************************************************************" + Environment.NewLine; } return(listString); } catch (Exception e) { UserInterface ui = new UserInterface(); ui.OutputAString(e.ToString() + " " + e.StackTrace); return(null); } }