Skip to content

Commit

Permalink
Accepting form input: adding a restaurant edit model, modifying servi…
Browse files Browse the repository at this point in the history
…ces, the Details view, and creating a new Home controller action
  • Loading branch information
gorohoroh committed Jul 5, 2018
1 parent e1eef7d commit e005da1
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 4 deletions.
13 changes: 13 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
1 change: 1 addition & 0 deletions OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IRestaurantData
// VSRD: Complete Statement at GetAll{caret} generates both the parentheses and the semicolon
IEnumerable<Restaurant> GetAll();
Restaurant Get(int id);
Restaurant Add(Restaurant restaurant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion OdeToFoodRider/OdeToFoodRider/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IGreeter, Greeter>();
services.AddScoped<IRestaurantData, InMemoryRestaurantData>();
services.AddSingleton<IRestaurantData, InMemoryRestaurantData>();
services.AddMvc();

}
Expand Down
10 changes: 10 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/ViewModels/RestaurantEditModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using OdeToFoodRider.Models;

namespace OdeToFoodRider.ViewModels
{
public class RestaurantEditModel
{
public string Name { get; set; }
public CuisineType Cuisine { get; set; }
}
}
3 changes: 2 additions & 1 deletion OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
</head>
<body>
<h1>@Model.Name</h1>
<div>... details ...</div>
<div>Id: @Model.Id</div>
<div>Cuisine: @Model.Cuisine</div>
<a asp-action="Index" asp-controller="Home">Home</a>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface IRestaurantData
{
IEnumerable<Restaurant> GetAll();
Restaurant Get(int id);
Restaurant Add(Restaurant restaurant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IGreeter, Greeter>(); // singleton scope
services.AddScoped<IRestaurantData, InMemoryRestaurantData>(); // per-HTTP-request lifeime
services.AddSingleton<IRestaurantData, InMemoryRestaurantData>(); // per-HTTP-request lifeime
services.AddMvc();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<h1>@Model.Name</h1>

<div>... details ...</div>
<div>Id: @Model.Id</div>
<div>Cuisine: @Model.Cuisine</div>
@* VSRD: no completion or resolve for controller and action names in tag helpers in Visual Studio *@
<a asp-action="Index" asp-controller="Home">Home</a>

0 comments on commit e005da1

Please sign in to comment.