public static Dish AddDishToRestaurant(RestaurantContext db, string name, int price, List <string> types, Restaurant restaurant) { var dish = new Dish(); dish.RestaurantAddress = restaurant.Address; dish.Name = name; dish.Price = price; db.Add(dish); db.SaveChanges(); foreach (var type in types) { DishType dishType = db.DishTypes.Find(type); if (dishType == null) { dishType = new DishType() { Type = type }; db.DishTypes.Add(dishType); db.SaveChanges(); } DishDishType restaurantRestaurantType = new DishDishType() { DishId = dish.DishId, TypeId = dishType.Type }; db.Add(restaurantRestaurantType); db.SaveChanges(); } db.SaveChanges(); return(dish); }
public void Run(RestaurantContext db) { Console.WriteLine(this.description); Console.WriteLine("-------------------------------------\n"); Dish dish = new Dish(); List <Restaurant> restaurantsToPrint = db.Restaurants.ToList(); Console.WriteLine("Listing all available restaurants:"); foreach (var r in restaurantsToPrint) { Console.WriteLine("Name: {0}, \tAddress: {1}", r.Name, r.Address); } Console.WriteLine("Enter address of restaurant to add dish to:"); string input = Console.ReadLine(); Restaurant restaurant = db.Restaurants.Find(input); if (restaurant == null) { Console.WriteLine("Restaurant does not exist, returning to menu..."); return; } dish.RestaurantAddress = restaurant.Address; Console.WriteLine("Enter name of dish:"); dish.Name = Console.ReadLine(); Console.WriteLine("Enter price of dish (must be an whole number):"); input = Console.ReadLine(); int price = 0; while (!int.TryParse(input, out price)) { Console.WriteLine("You failed to enter a WHOLE number, try again..."); input = Console.ReadLine(); } dish.Price = price; db.Add(dish); db.SaveChanges(); input = "0"; List <string> types = new List <string>(); while (input != "x") { Console.WriteLine("Add dish types - Seperate with enter, enter \"x\" to finish: "); input = Console.ReadLine(); if (input != "x" && !types.Contains(input)) { types.Add(input); } } foreach (string type in types) { DishType dishType = db.DishTypes.Find(type); if (dishType == null) { dishType = new DishType() { Type = type }; db.DishTypes.Add(dishType); db.SaveChanges(); } DishDishType dishDishType = new DishDishType() { DishId = dish.DishId, TypeId = dishType.Type }; db.Add(dishDishType); } db.SaveChanges(); }