public void Run(RestaurantContext db) { Console.WriteLine(this.description); Console.WriteLine("-------------------------------------\n"); Restaurant restaurant = new Restaurant(); Console.WriteLine("Restaurant address: "); string input = Console.ReadLine(); if (db.Restaurants.Find(input) != null) { Console.WriteLine("A restaurant already exists with that address... Going back to menu"); return; } restaurant.Address = input; Console.WriteLine("Restaurant name: "); restaurant.Name = Console.ReadLine(); input = "0"; List <string> types = new List <string>(); while (input != "x") { Console.WriteLine("Enter restaurant type - Seperate types with enter, enter \"x\" to finish: "); input = Console.ReadLine(); if (input != "x" && !types.Contains(input)) { types.Add(input); } } db.Add(restaurant); db.SaveChanges(); foreach (string type in types) { RestaurantType restaurantType = db.RestaurantTypes.Find(type); if (restaurantType == null) { restaurantType = new RestaurantType() { Type = type }; db.RestaurantTypes.Add(restaurantType); db.SaveChanges(); } RestaurantRestaurantType restaurantRestaurantType = new RestaurantRestaurantType { RestaurantAddress = restaurant.Address, TypeId = restaurantType.Type }; db.Add(restaurantRestaurantType); } db.SaveChanges(); }
public static Restaurant AddRestaurant(RestaurantContext db, string address, string name, List <string> types) { Restaurant restaurant = db.Restaurants.Find(address); if (restaurant != null) { return(null); } restaurant = new Restaurant(); restaurant.Address = address; restaurant.Name = name; db.Add(restaurant); db.SaveChanges(); foreach (var type in types) { RestaurantType restaurantType = db.RestaurantTypes.Find(type); if (restaurantType == null) { restaurantType = new RestaurantType() { Type = type }; db.RestaurantTypes.Add(restaurantType); db.SaveChanges(); } RestaurantRestaurantType restaurantRestaurantType = new RestaurantRestaurantType { RestaurantAddress = restaurant.Address, TypeId = restaurantType.Type }; db.Add(restaurantRestaurantType); } db.SaveChanges(); return(restaurant); }