Skip to content

Commit

Permalink
Creating a new Home controller action Details(int id), updating servi…
Browse files Browse the repository at this point in the history
…ces 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.
  • Loading branch information
gorohoroh committed Jul 4, 2018
1 parent fde46ac commit ddc5a0e
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 0 deletions.
13 changes: 13 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.AspNetCore.Mvc;
using OdeToFoodRider.Models;
using OdeToFoodRider.Services;
Expand Down Expand Up @@ -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);
}
}
}
1 change: 1 addition & 0 deletions OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface IRestaurantData
{
// VSRD: Complete Statement at GetAll{caret} generates both the parentheses and the semicolon
IEnumerable<Restaurant> GetAll();
Restaurant Get(int id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ public IEnumerable<Restaurant> GetAll()
{
return _restaurants.OrderBy(r => r.Name);
}

public Restaurant Get(int id)
{
return _restaurants.FirstOrDefault(r => r.Id == id);
}
}
}
15 changes: 15 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Views/Home/Details.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@using OdeToFoodRider.Models
@model Restaurant

<!DOCTYPE html>

<html>
<head>
<title>Restaurant details</title>
</head>
<body>
<h1>@Model.Name</h1>
<div>... details ...</div>
<a asp-action="Index" asp-controller="Home">Home</a>
</body>
</html>
6 changes: 6 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<tr>
<td>@restaurant.Id</td>
<td>@restaurant.Name</td>
<td>
@* VSRD: Rider's path mapping functionality acts weird with the path below *@
<a href="/home/details/@restaurant.Id">Details</a>
@Html.ActionLink("Go", "Details", new {id = restaurant.Id})
<a asp-action="Details" asp-route-id="@restaurant.Id">More</a>
</td>
</tr>
}
</table>
Expand Down
2 changes: 2 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Views/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -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 *@
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

namespace OdeToFoodVisualStudio.Services
{
// VSRD: "Go to Implementation" is available in Visual Studio
public interface IRestaurantData
{
IEnumerable<Restaurant> GetAll();
Restaurant Get(int id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ public IEnumerable<Restaurant> GetAll()
{
return _restaurants.OrderBy(r => r.Name);
}

public Restaurant Get(int id)
{
return _restaurants.FirstOrDefault(r => r.Id == id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@model OdeToFoodVisualStudio.Models.Restaurant

<h1>@Model.Name</h1>

<div>... details ...</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>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<tr>
<td>@restaurant.Id</td>
<td>@restaurant.Name</td>
<td>
<a href="/home/details/@restaurant.Id">Details</a>
@Html.ActionLink("Go", "Details", new { id = restaurant.Id })
<a asp-action="Details" asp-route-id="@restaurant.Id">More</a>
</td>
</tr>
}
</table>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@* VSRD: Visual Studio provides no completion for the assembly name above *@

0 comments on commit ddc5a0e

Please sign in to comment.