Skip to content

Commit

Permalink
Consolidate Web and WebRazorPages Projects (dotnet-architecture#192)
Browse files Browse the repository at this point in the history
* Moved Privacy, Home page to Razor Pages

* Migrating Basket from RazorPages to Web.

* Removed BasketController; refactored viewmodels

* Moved BasketComponent into Pages/Shared
Added auth rules to Startup for Pages
Added notes to controllers about Pages usage.

* Fixed broken my orders test
Consolidated Functional Tests

* Fixed logo link to home page
Fixed Order Detail Total $ format
  • Loading branch information
ardalis authored Jan 18, 2019
1 parent 483340f commit 99c4161
Show file tree
Hide file tree
Showing 42 changed files with 429 additions and 430 deletions.
7 changes: 0 additions & 7 deletions eShopOnWeb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Web", "src\Web\Web.csproj", "{227CF035-29B0-448D-97E4-944F9EA850E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebRazorPages", "src\WebRazorPages\WebRazorPages.csproj", "{E2D75FD9-84A8-46B1-97E4-9AC67F919D11}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,10 +54,6 @@ Global
{227CF035-29B0-448D-97E4-944F9EA850E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{227CF035-29B0-448D-97E4-944F9EA850E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{227CF035-29B0-448D-97E4-944F9EA850E5}.Release|Any CPU.Build.0 = Release|Any CPU
{E2D75FD9-84A8-46B1-97E4-9AC67F919D11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E2D75FD9-84A8-46B1-97E4-9AC67F919D11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2D75FD9-84A8-46B1-97E4-9AC67F919D11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2D75FD9-84A8-46B1-97E4-9AC67F919D11}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -71,7 +65,6 @@ Global
{0F576306-7E2D-49B7-87B1-EB5D94CFD5FC} = {15EA4737-125B-4E6E-A806-E13B7EBCDCCF}
{7EFB5482-F942-4C3D-94B0-9B70596E6D0A} = {15EA4737-125B-4E6E-A806-E13B7EBCDCCF}
{227CF035-29B0-448D-97E4-944F9EA850E5} = {419A6ACE-0419-4315-A6FB-B0E63D39432E}
{E2D75FD9-84A8-46B1-97E4-9AC67F919D11} = {419A6ACE-0419-4315-A6FB-B0E63D39432E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {49813262-5DA3-4D61-ABD3-493C74CE8C2B}
Expand Down
2 changes: 2 additions & 0 deletions src/Web/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
public static class Constants
{
public const string BASKET_COOKIENAME = "eShop";
public const int ITEMS_PER_PAGE = 10;
public const string DEFAULT_USERNAME = "Guest";
}
}
8 changes: 4 additions & 4 deletions src/Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.eShopWeb.Web.Controllers
{
[ApiExplorerSettings(IgnoreApi = true)]
[Route("[controller]/[action]")]
[Authorize]
[Authorize] // Controllers that mainly require Authorization still use Controller/View; other pages use Pages
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
Expand Down Expand Up @@ -150,7 +150,7 @@ public async Task<ActionResult> SignOut()
{
await _signInManager.SignOutAsync();

return RedirectToAction(nameof(CatalogController.Index), "Catalog");
return RedirectToPage("/Index");
}

[AllowAnonymous]
Expand Down Expand Up @@ -186,7 +186,7 @@ public async Task<IActionResult> ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
{
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
return RedirectToPage("/Index");
}
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
Expand Down Expand Up @@ -217,7 +217,7 @@ private IActionResult RedirectToLocal(string returnUrl)
}
else
{
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
return RedirectToPage("/Index");
}
}

Expand Down
112 changes: 0 additions & 112 deletions src/Web/Controllers/BasketController.cs

This file was deleted.

30 changes: 0 additions & 30 deletions src/Web/Controllers/CatalogController.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Web/Controllers/ManageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace Microsoft.eShopWeb.Web.Controllers
{
[ApiExplorerSettings(IgnoreApi = true)]
[Authorize]
[Authorize] // Controllers that mainly require Authorization still use Controller/View; other pages use Pages
[Route("[controller]/[action]")]
public class ManageController : Controller
{
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Microsoft.eShopWeb.Web.Controllers
{
[ApiExplorerSettings(IgnoreApi = true)]
[Authorize]
[Authorize] // Controllers that mainly require Authorization still use Controller/View; other pages use Pages
[Route("[controller]/[action]")]
public class OrderController : Controller
{
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Interfaces/IBasketViewModelService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.eShopWeb.Web.ViewModels;
using Microsoft.eShopWeb.Web.Pages.Basket;
using System.Threading.Tasks;

namespace Microsoft.eShopWeb.Web.Interfaces
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.eShopWeb.Web.ViewModels
namespace Microsoft.eShopWeb.Web.Pages.Basket
{
public class BasketItemViewModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.eShopWeb.Web.ViewModels
namespace Microsoft.eShopWeb.Web.Pages.Basket
{
public class BasketViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@{
@page
@model CheckoutModel
@{
ViewData["Title"] = "Checkout Complete";
@model BasketViewModel
}
<section class="esh-catalog-hero">
<div class="container">
Expand All @@ -11,5 +12,5 @@
<div class="container">
<h1>Thanks for your Order!</h1>

<a asp-controller="Catalog" asp-action="Index">Continue Shopping...</a>
<a asp-page="/Index">Continue Shopping...</a>
</div>
83 changes: 83 additions & 0 deletions src/Web/Pages/Basket/Checkout.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Web.Interfaces;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Microsoft.eShopWeb.Web.Pages.Basket
{
public class CheckoutModel : PageModel
{
private readonly IBasketService _basketService;
private readonly IUriComposer _uriComposer;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IOrderService _orderService;
private string _username = null;
private readonly IBasketViewModelService _basketViewModelService;

public CheckoutModel(IBasketService basketService,
IBasketViewModelService basketViewModelService,
IUriComposer uriComposer,
SignInManager<ApplicationUser> signInManager,
IOrderService orderService)
{
_basketService = basketService;
_uriComposer = uriComposer;
_signInManager = signInManager;
_orderService = orderService;
_basketViewModelService = basketViewModelService;
}

public BasketViewModel BasketModel { get; set; } = new BasketViewModel();

public void OnGet()
{
}

public async Task<IActionResult> OnPost(Dictionary<string, int> items)
{
await SetBasketModelAsync();

await _basketService.SetQuantities(BasketModel.Id, items);

await _orderService.CreateOrderAsync(BasketModel.Id, new Address("123 Main St.", "Kent", "OH", "United States", "44240"));

await _basketService.DeleteBasketAsync(BasketModel.Id);

return RedirectToPage();
}

private async Task SetBasketModelAsync()
{
if (_signInManager.IsSignedIn(HttpContext.User))
{
BasketModel = await _basketViewModelService.GetOrCreateBasketForUser(User.Identity.Name);
}
else
{
GetOrSetBasketCookieAndUserName();
BasketModel = await _basketViewModelService.GetOrCreateBasketForUser(_username);
}
}

private void GetOrSetBasketCookieAndUserName()
{
if (Request.Cookies.ContainsKey(Constants.BASKET_COOKIENAME))
{
_username = Request.Cookies[Constants.BASKET_COOKIENAME];
}
if (_username != null) return;

_username = Guid.NewGuid().ToString();
var cookieOptions = new CookieOptions();
cookieOptions.Expires = DateTime.Today.AddYears(10);
Response.Cookies.Append(Constants.BASKET_COOKIENAME, _username, cookieOptions);
}
}
}
Loading

0 comments on commit 99c4161

Please sign in to comment.