public static MealsInMenuDal getInstance() { if (_instance == null) { _instance = new MealsInMenuDal(); } return(_instance); }
public Menu InsertMenu(Menu menu) { MealTypeDal mt_dal = MealTypeDal.getInstance(); MealsInMenuDal mim_dal = MealsInMenuDal.getInstance(); int mt_id; //Create the SQL Query for inserting an menu string createQuery = String.Format("Insert into Menues (PickRate) Values({0});" + "Select @@Identity", 1); //Create and open a connection to SQL Server SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["sports_db"].ConnectionString); connection.Open(); //Create a Command object SqlCommand command = null; // new SqlCommand(createQuery, connection); command = new SqlCommand(createQuery, connection); try { //Execute the command to SQL Server and return the newly created ID var commandResult = command.ExecuteScalar(); if (commandResult != null) { menu.MenuID = Convert.ToInt32(commandResult); foreach (var item in menu.Breakfast) { mt_id = mt_dal.GetMealType((int)MealTypeENUM.Breakfast, item.FoodID).MealTypeID; mim_dal.InsertOrUpdateMealsInMenu(new MealsInMenu() { MealTypeID = mt_id, MenuID = menu.MenuID }); } foreach (var item in menu.Lunch) { mt_id = mt_dal.GetMealType((int)MealTypeENUM.Lunch, item.FoodID).MealTypeID; mim_dal.InsertOrUpdateMealsInMenu(new MealsInMenu() { MealTypeID = mt_id, MenuID = menu.MenuID }); } foreach (var item in menu.Dinner) { mt_id = mt_dal.GetMealType((int)MealTypeENUM.Dinner, item.FoodID).MealTypeID; //InsertOrUpdateMealType(new MealType() //{ // FoodID = item.FoodID, // Type = (int)MealTypeENUM.Dinner //}); mim_dal.InsertOrUpdateMealsInMenu(new MealsInMenu() { MealTypeID = mt_id, MenuID = menu.MenuID }); } } else { return(null); } } catch (Exception Ex) { return(null); } //Close and dispose CloseAndDispose(command, connection); // Set return value return(menu); }