Browse Source

Adding slugify parameter transform (#185)

* Adding slugify parameter transform

Fixes #172

* Cleaning up code

And slugifying my orders
main
Steve Smith 7 years ago
committed by GitHub
parent
commit
bc328077b0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Web/Controllers/ManageController.cs
  2. 4
      src/Web/Controllers/OrderController.cs
  3. 17
      src/Web/SlugifyParameterTransformer.cs
  4. 20
      src/Web/Startup.cs
  5. 12
      src/Web/Views/Manage/MyAccount.cshtml
  6. 0
      src/Web/Views/Order/MyOrders.cshtml
  7. 105
      src/Web/Views/Shared/_LoginPartial.cshtml
  8. 8
      src/Web/Web.csproj

2
src/Web/Controllers/ManageController.cs

@ -45,7 +45,7 @@ namespace Microsoft.eShopWeb.Web.Controllers
public string StatusMessage { get; set; } public string StatusMessage { get; set; }
[HttpGet] [HttpGet]
public async Task<IActionResult> Index() public async Task<IActionResult> MyAccount()
{ {
var user = await _userManager.GetUserAsync(User); var user = await _userManager.GetUserAsync(User);
if (user == null) if (user == null)

4
src/Web/Controllers/OrderController.cs

@ -20,8 +20,8 @@ namespace Microsoft.eShopWeb.Web.Controllers
_orderRepository = orderRepository; _orderRepository = orderRepository;
} }
[HttpGet] [HttpGet()]
public async Task<IActionResult> Index() public async Task<IActionResult> MyOrders()
{ {
var orders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(User.Identity.Name)); var orders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(User.Identity.Name));

17
src/Web/SlugifyParameterTransformer.cs

@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Routing;
using System.Text.RegularExpressions;
namespace Microsoft.eShopWeb.Web
{
public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null) { return null; }
// Slugify value
return Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2").ToLower();
}
}
}

20
src/Web/Startup.cs

@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI; using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services; using Microsoft.eShopWeb.ApplicationCore.Services;
@ -98,7 +99,22 @@ namespace Microsoft.eShopWeb.Web
// Add memory cache services // Add memory cache services
services.AddMemoryCache(); services.AddMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddRouting(options =>
{
// Replace the type and the name used to refer to it with your own
// IOutboundParameterTransformer implementation
options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
});
services.AddMvc(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(
new SlugifyParameterTransformer()));
}
).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c => services.AddSwaggerGen(c =>
{ {
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
@ -163,7 +179,7 @@ namespace Microsoft.eShopWeb.Web
{ {
routes.MapRoute( routes.MapRoute(
name: "default", name: "default",
template: "{controller=Home}/{action=Index}/{id?}"); template: "{controller:slugify=Home}/{action:slugify=Index}/{id?}");
}); });
} }

12
src/Web/Views/Manage/Index.cshtml → src/Web/Views/Manage/MyAccount.cshtml

@ -18,15 +18,15 @@
<label asp-for="Email"></label> <label asp-for="Email"></label>
@if (Model.IsEmailConfirmed) @if (Model.IsEmailConfirmed)
{ {
<div class="input-group"> <div class="input-group">
<input asp-for="Email" class="form-control" /> <input asp-for="Email" class="form-control" />
<span class="input-group-addon" aria-hidden="true"><span class="glyphicon glyphicon-ok text-success"></span></span> <span class="input-group-addon" aria-hidden="true"><span class="glyphicon glyphicon-ok text-success"></span></span>
</div> </div>
} }
else else
{ {
<input asp-for="Email" class="form-control" /> <input asp-for="Email" class="form-control" />
<button asp-action="SendVerificationEmail" class="btn btn-link">Send verification email</button> <button asp-action="SendVerificationEmail" class="btn btn-link">Send verification email</button>
} }
<span asp-validation-for="Email" class="text-danger"></span> <span asp-validation-for="Email" class="text-danger"></span>
</div> </div>

0
src/Web/Views/Order/Index.cshtml → src/Web/Views/Order/MyOrders.cshtml

105
src/Web/Views/Shared/_LoginPartial.cshtml

@ -1,64 +1,55 @@
@if (Context.User.Identity.IsAuthenticated) @if (Context.User.Identity.IsAuthenticated)
{ {
<section class="col-lg-4 col-md-5 col-xs-12"> <section class="col-lg-4 col-md-5 col-xs-12">
<div class="esh-identity"> <div class="esh-identity">
<form asp-area="" asp-controller="Account" asp-action="SignOut" method="post" id="logoutForm" class="navbar-right"> <form asp-area="" asp-controller="Account" asp-action="SignOut" method="post" id="logoutForm" class="navbar-right">
<section class="esh-identity-section"> <section class="esh-identity-section">
@*<div class="esh-identity-name">@User.FindFirst(x => x.Type == "preferred_username").Value</div>*@
@*<div class="esh-identity-name">@User.FindFirst(x => x.Type == "preferred_username").Value</div>*@ <img class="esh-identity-image" src="~/images/arrow-down.png">
<img class="esh-identity-image" src="~/images/arrow-down.png"> </section>
</section> <section class="esh-identity-drop">
<a class="esh-identity-item"
<section class="esh-identity-drop"> asp-controller="Order"
asp-action="MyOrders">
<a class="esh-identity-item" <div class="esh-identity-name esh-identity-name--upper">My orders</div>
asp-controller="Order" <img class="esh-identity-image" src="~/images/my_orders.png">
asp-action="Index"> </a>
<a class="esh-identity-item"
<div class="esh-identity-name esh-identity-name--upper">My orders</div> asp-controller="Manage"
<img class="esh-identity-image" src="~/images/my_orders.png"> asp-action="MyAccount">
</a> <div class="esh-identity-name esh-identity-name--upper">My account</div>
<img class="esh-identity-image" src="~/images/my_orders.png">
<a class="esh-identity-item" </a>
asp-controller="Manage" <a class="esh-identity-item"
asp-action="Index"> href="javascript:document.getElementById('logoutForm').submit()">
<div class="esh-identity-name esh-identity-name--upper">Log Out</div>
<div class="esh-identity-name esh-identity-name--upper">My account</div> <img class="esh-identity-image" src="~/images/logout.png">
<img class="esh-identity-image" src="~/images/my_orders.png"> </a>
</a> </section>
</form>
<a class="esh-identity-item" </div>
href="javascript:document.getElementById('logoutForm').submit()"> </section>
<div class="esh-identity-name esh-identity-name--upper">Log Out</div>
<img class="esh-identity-image" src="~/images/logout.png">
</a>
</section>
</form>
</div>
</section>
<section class="col-lg-1 col-xs-12"> <section class="col-lg-1 col-xs-12">
@await Component.InvokeAsync("Basket", User.Identity.Name) @await Component.InvokeAsync("Basket", User.Identity.Name)
</section> </section>
} }
else else
{ {
<section class="col-lg-1 col-lg-offset-3 col-md-3 col-xs-6"> <section class="col-lg-1 col-lg-offset-3 col-md-3 col-xs-6">
<div class="esh-identity"> <div class="esh-identity">
<section class="esh-identity-section"> <section class="esh-identity-section">
<div class="esh-identity-item"> <div class="esh-identity-item">
<a asp-area="" asp-controller="Account" asp-action="SignIn" class="esh-identity-name esh-identity-name--upper">
<a asp-area="" asp-controller="Account" asp-action="SignIn" class="esh-identity-name esh-identity-name--upper"> Login
Login </a>
</a> </div>
</div> </section>
</section> </div>
</div> </section>
</section>
<section class="col-lg-1 col-xs-12">
<section class="col-lg-1 col-xs-12"> @await Component.InvokeAsync("Basket")
@await Component.InvokeAsync("Basket") </section>
</section>
} }

8
src/Web/Web.csproj

@ -76,7 +76,6 @@
<_ContentIncludedByDefault Remove="Views\Manage\EnableAuthenticator.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\EnableAuthenticator.cshtml" />
<_ContentIncludedByDefault Remove="Views\Manage\ExternalLogins.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\ExternalLogins.cshtml" />
<_ContentIncludedByDefault Remove="Views\Manage\GenerateRecoveryCodes.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\GenerateRecoveryCodes.cshtml" />
<_ContentIncludedByDefault Remove="Views\Manage\Index.cshtml" />
<_ContentIncludedByDefault Remove="Views\Manage\ResetAuthenticator.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\ResetAuthenticator.cshtml" />
<_ContentIncludedByDefault Remove="Views\Manage\SetPassword.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\SetPassword.cshtml" />
<_ContentIncludedByDefault Remove="Views\Manage\TwoFactorAuthentication.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\TwoFactorAuthentication.cshtml" />
@ -85,7 +84,6 @@
<_ContentIncludedByDefault Remove="Views\Manage\_StatusMessage.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\_StatusMessage.cshtml" />
<_ContentIncludedByDefault Remove="Views\Manage\_ViewImports.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\_ViewImports.cshtml" />
<_ContentIncludedByDefault Remove="Views\Order\Detail.cshtml" /> <_ContentIncludedByDefault Remove="Views\Order\Detail.cshtml" />
<_ContentIncludedByDefault Remove="Views\Order\Index.cshtml" />
<_ContentIncludedByDefault Remove="Views\Shared\Components\Basket\Default.cshtml" /> <_ContentIncludedByDefault Remove="Views\Shared\Components\Basket\Default.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -139,9 +137,6 @@
<ItemGroup> <ItemGroup>
<UpToDateCheckInput Remove="Views\Manage\GenerateRecoveryCodes.cshtml" /> <UpToDateCheckInput Remove="Views\Manage\GenerateRecoveryCodes.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<UpToDateCheckInput Remove="Views\Manage\Index.cshtml" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<UpToDateCheckInput Remove="Views\Manage\ResetAuthenticator.cshtml" /> <UpToDateCheckInput Remove="Views\Manage\ResetAuthenticator.cshtml" />
</ItemGroup> </ItemGroup>
@ -154,9 +149,6 @@
<ItemGroup> <ItemGroup>
<UpToDateCheckInput Remove="Views\Order\Detail.cshtml" /> <UpToDateCheckInput Remove="Views\Order\Detail.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<UpToDateCheckInput Remove="Views\Order\Index.cshtml" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<UpToDateCheckInput Remove="Views\Shared\Components\Basket\Default.cshtml" /> <UpToDateCheckInput Remove="Views\Shared\Components\Basket\Default.cshtml" />
</ItemGroup> </ItemGroup>

Loading…
Cancel
Save