From ddc5a0ebabab97464050d15219bfe3c4ea143615 Mon Sep 17 00:00:00 2001 From: gorohoroh Date: Wed, 4 Jul 2018 22:02:41 +0300 Subject: [PATCH] Creating a new Home controller action Details(int id), updating services with a new Get(int id) method to return details for a particular restaurant, creating a simple Details view, updating the Index view to use tag helpers and creating a required _ViewImports.cshtml along the way. --- .../OdeToFoodRider/Controllers/HomeController.cs | 13 +++++++++++++ .../OdeToFoodRider/Services/IRestaurantData.cs | 1 + .../Services/InMemoryRestaurantData.cs | 5 +++++ .../OdeToFoodRider/Views/Home/Details.cshtml | 15 +++++++++++++++ .../OdeToFoodRider/Views/Home/Index.cshtml | 6 ++++++ .../OdeToFoodRider/Views/_ViewImports.cshtml | 2 ++ .../Controllers/HomeController.cs | 14 ++++++++++++++ .../Services/IRestaurantData.cs | 2 ++ .../Services/InMemoryRestaurantData.cs | 5 +++++ .../Views/Home/Details.cshtml | 7 +++++++ .../OdeToFoodVisualStudio/Views/Home/Index.cshtml | 5 +++++ .../Views/_ViewImports.cshtml | 2 ++ 12 files changed, 77 insertions(+) create mode 100644 OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml create mode 100644 OdeToFoodRider/OdeToFoodRider/Views/_ViewImports.cshtml create mode 100644 OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Details.cshtml create mode 100644 OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/_ViewImports.cshtml diff --git a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs index bb45738..0466366 100644 --- a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs +++ b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.AspNetCore.Mvc; using OdeToFoodRider.Models; using OdeToFoodRider.Services; @@ -27,5 +28,17 @@ public IActionResult Index() return View(model); } + + public IActionResult Details(int id) + { + // VSRD: Rider's completion misfires here, too: the opening parentheses completes the existing GetAll() method, too + var model = _restaurantData.Get(id); // VSRD: In Rider, a "Check variable for null" CA is available, along with a quick path to null check pattern settings + if (model == null) + { + return RedirectToAction(nameof(Index)); + } + + return View(model); + } } } \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs b/OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs index e202e59..4d16e29 100644 --- a/OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs +++ b/OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs @@ -7,5 +7,6 @@ public interface IRestaurantData { // VSRD: Complete Statement at GetAll{caret} generates both the parentheses and the semicolon IEnumerable GetAll(); + Restaurant Get(int id); } } \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs b/OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs index cb6f23c..d4948c1 100644 --- a/OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs +++ b/OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs @@ -28,5 +28,10 @@ public IEnumerable GetAll() { return _restaurants.OrderBy(r => r.Name); } + + public Restaurant Get(int id) + { + return _restaurants.FirstOrDefault(r => r.Id == id); + } } } \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml b/OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml new file mode 100644 index 0000000..4086f46 --- /dev/null +++ b/OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml @@ -0,0 +1,15 @@ +@using OdeToFoodRider.Models +@model Restaurant + + + + + + Restaurant details + + +

@Model.Name

+
... details ...
+Home + + \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml b/OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml index e42ce5d..423c17c 100644 --- a/OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml +++ b/OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml @@ -17,6 +17,12 @@ @restaurant.Id @restaurant.Name + + @* VSRD: Rider's path mapping functionality acts weird with the path below *@ + Details + @Html.ActionLink("Go", "Details", new {id = restaurant.Id}) + More + } diff --git a/OdeToFoodRider/OdeToFoodRider/Views/_ViewImports.cshtml b/OdeToFoodRider/OdeToFoodRider/Views/_ViewImports.cshtml new file mode 100644 index 0000000..9532982 --- /dev/null +++ b/OdeToFoodRider/OdeToFoodRider/Views/_ViewImports.cshtml @@ -0,0 +1,2 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@* VSRD: No completion suggestions for assembly name in Rider, too :( - completion for @addTagHelper is available though *@ \ No newline at end of file diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs index 169f0e3..a65fd9c 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs @@ -30,5 +30,19 @@ public IActionResult Index() return View(model); } + + public IActionResult Details(int id) + { + // VSRD: at _restaurantData.Get{caret}, I want to use a method called Get() before declaring it but entering an opening parentheses completes the existing GetAll() method. Not good. + // VSRD: However, creating the method from usage here does in fact create "object Get()" in the IRestaurantData interface + var model = _restaurantData.Get(id); + if (model == null) + { + return RedirectToAction(nameof(Index)); // VSRD: if we type RedirectToAction("Index"), Visual Studio doesn't provide view completion in the string literal, which is why Scott prefers the nameof syntax + } + + // VSRD: Visual Studio doesn't see that the view isn't resolved and doesn't suggest to create one. + return View(model); + } } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs index eaf00f2..209f703 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs @@ -6,8 +6,10 @@ namespace OdeToFoodVisualStudio.Services { + // VSRD: "Go to Implementation" is available in Visual Studio public interface IRestaurantData { IEnumerable GetAll(); + Restaurant Get(int id); } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs index c44ed91..5b7019b 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs @@ -28,5 +28,10 @@ public IEnumerable GetAll() { return _restaurants.OrderBy(r => r.Name); } + + public Restaurant Get(int id) + { + return _restaurants.FirstOrDefault(r => r.Id == id); + } } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Details.cshtml b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Details.cshtml new file mode 100644 index 0000000..e748476 --- /dev/null +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Details.cshtml @@ -0,0 +1,7 @@ +@model OdeToFoodVisualStudio.Models.Restaurant + +

@Model.Name

+ +
... details ...
+@* VSRD: no completion or resolve for controller and action names in tag helpers in Visual Studio *@ +Home \ No newline at end of file diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml index 312912e..4f268c6 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml @@ -16,6 +16,11 @@ @restaurant.Id @restaurant.Name + + Details + @Html.ActionLink("Go", "Details", new { id = restaurant.Id }) + More + } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/_ViewImports.cshtml b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/_ViewImports.cshtml new file mode 100644 index 0000000..e471631 --- /dev/null +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/_ViewImports.cshtml @@ -0,0 +1,2 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@* VSRD: Visual Studio provides no completion for the assembly name above *@ \ No newline at end of file