diff --git a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs index d4152e5..0aeaa02 100644 --- a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs +++ b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs @@ -41,9 +41,22 @@ public IActionResult Details(int id) return View(model); } + [HttpGet] public IActionResult Create() { return View(); } + + [HttpPost] + public IActionResult Create(RestaurantEditModel model) + { + var newRestaurant = new Restaurant(); + newRestaurant.Name = model.Name; + newRestaurant.Cuisine = model.Cuisine; + + newRestaurant = _restaurantData.Add(newRestaurant); + + return View("Details", newRestaurant); + } } } \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs b/OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs index 4d16e29..618078a 100644 --- a/OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs +++ b/OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs @@ -8,5 +8,6 @@ public interface IRestaurantData // VSRD: Complete Statement at GetAll{caret} generates both the parentheses and the semicolon IEnumerable GetAll(); Restaurant Get(int id); + Restaurant Add(Restaurant restaurant); } } \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs b/OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs index d4948c1..d5bd6e7 100644 --- a/OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs +++ b/OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs @@ -33,5 +33,12 @@ public Restaurant Get(int id) { return _restaurants.FirstOrDefault(r => r.Id == id); } + + public Restaurant Add(Restaurant restaurant) + { + restaurant.Id = _restaurants.Max(r => r.Id) + 1; + _restaurants.Add(restaurant); + return restaurant; + } } } \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Startup.cs b/OdeToFoodRider/OdeToFoodRider/Startup.cs index e3419db..cb9d541 100644 --- a/OdeToFoodRider/OdeToFoodRider/Startup.cs +++ b/OdeToFoodRider/OdeToFoodRider/Startup.cs @@ -20,7 +20,7 @@ public class Startup public void ConfigureServices(IServiceCollection services) { services.AddSingleton(); - services.AddScoped(); + services.AddSingleton(); services.AddMvc(); } diff --git a/OdeToFoodRider/OdeToFoodRider/ViewModels/RestaurantEditModel.cs b/OdeToFoodRider/OdeToFoodRider/ViewModels/RestaurantEditModel.cs new file mode 100644 index 0000000..b59c412 --- /dev/null +++ b/OdeToFoodRider/OdeToFoodRider/ViewModels/RestaurantEditModel.cs @@ -0,0 +1,10 @@ +using OdeToFoodRider.Models; + +namespace OdeToFoodRider.ViewModels +{ + public class RestaurantEditModel + { + public string Name { get; set; } + public CuisineType Cuisine { get; set; } + } +} \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml b/OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml index 4086f46..b33d9a2 100644 --- a/OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml +++ b/OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml @@ -9,7 +9,8 @@

@Model.Name

-
... details ...
+
Id: @Model.Id
+
Cuisine: @Model.Cuisine
Home \ No newline at end of file diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs index 5f08014..3444943 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs @@ -45,9 +45,23 @@ public IActionResult Details(int id) return View(model); } + [HttpGet] public IActionResult Create() { return View(); } + + [HttpPost] + public IActionResult Create(RestaurantEditModel model) + { + var newRestaurant = new Restaurant(); + newRestaurant.Name = model.Name; + newRestaurant.Cuisine = model.Cuisine; + + newRestaurant = _restaurantData.Add(newRestaurant); + + return View("Details", newRestaurant); + + } } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs index 209f703..18ffb31 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs @@ -11,5 +11,6 @@ public interface IRestaurantData { IEnumerable GetAll(); Restaurant Get(int id); + Restaurant Add(Restaurant restaurant); } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs index 5b7019b..9a944c2 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs @@ -33,5 +33,12 @@ public Restaurant Get(int id) { return _restaurants.FirstOrDefault(r => r.Id == id); } + + public Restaurant Add(Restaurant restaurant) + { + restaurant.Id = _restaurants.Max(r => r.Id) + 1; + _restaurants.Add(restaurant); + return restaurant; + } } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs index e3e5120..6f46403 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs @@ -19,7 +19,7 @@ public void ConfigureServices(IServiceCollection services) // RDVS: Rider's completion works better with this generic method as it additionally adds the parentheses; additionally, VS overlaps the completion list inside the generic brackets // with with parameter info services.AddSingleton(); // singleton scope - services.AddScoped(); // per-HTTP-request lifeime + services.AddSingleton(); // per-HTTP-request lifeime services.AddMvc(); } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/RestaurantEditModel.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/RestaurantEditModel.cs new file mode 100644 index 0000000..c93ba4a --- /dev/null +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/RestaurantEditModel.cs @@ -0,0 +1,14 @@ +using OdeToFoodVisualStudio.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace OdeToFoodVisualStudio.ViewModels +{ + public class RestaurantEditModel + { + public string Name { get; set; } + public CuisineType Cuisine { get; set; } + } +} diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Details.cshtml b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Details.cshtml index e748476..688e035 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Details.cshtml +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Details.cshtml @@ -2,6 +2,7 @@

@Model.Name

-
... details ...
+
Id: @Model.Id
+
Cuisine: @Model.Cuisine
@* VSRD: no completion or resolve for controller and action names in tag helpers in Visual Studio *@ Home \ No newline at end of file