Controller cleanup (#30)
* Cleaning up routes. * Adding signout functionality * Added simple checkout behavior
This commit is contained in:
@@ -8,6 +8,7 @@ using Infrastructure.Identity;
|
|||||||
|
|
||||||
namespace Microsoft.eShopWeb.Controllers
|
namespace Microsoft.eShopWeb.Controllers
|
||||||
{
|
{
|
||||||
|
[Route("[controller]/[action]")]
|
||||||
public class AccountController : Controller
|
public class AccountController : Controller
|
||||||
{
|
{
|
||||||
private readonly UserManager<ApplicationUser> _userManager;
|
private readonly UserManager<ApplicationUser> _userManager;
|
||||||
@@ -17,7 +18,7 @@ namespace Microsoft.eShopWeb.Controllers
|
|||||||
public AccountController(
|
public AccountController(
|
||||||
UserManager<ApplicationUser> userManager,
|
UserManager<ApplicationUser> userManager,
|
||||||
SignInManager<ApplicationUser> signInManager,
|
SignInManager<ApplicationUser> signInManager,
|
||||||
IOptions<IdentityCookieOptions> identityCookieOptions
|
IOptions<IdentityCookieOptions> identityCookieOptions
|
||||||
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -58,6 +59,16 @@ namespace Microsoft.eShopWeb.Controllers
|
|||||||
return View(model);
|
return View(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[ValidateAntiForgeryToken]
|
||||||
|
public async Task<ActionResult> SignOut()
|
||||||
|
{
|
||||||
|
HttpContext.Session.Clear();
|
||||||
|
await _signInManager.SignOutAsync();
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
|
||||||
|
}
|
||||||
|
|
||||||
private IActionResult RedirectToLocal(string returnUrl)
|
private IActionResult RedirectToLocal(string returnUrl)
|
||||||
{
|
{
|
||||||
if (Url.IsLocalUrl(returnUrl))
|
if (Url.IsLocalUrl(returnUrl))
|
||||||
|
|||||||
@@ -1,43 +1,36 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using ApplicationCore.Interfaces;
|
using ApplicationCore.Interfaces;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.eShopWeb.ViewModels;
|
using Microsoft.eShopWeb.ViewModels;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.Controllers
|
namespace Microsoft.eShopWeb.Controllers
|
||||||
{
|
{
|
||||||
|
[Route("[controller]/[action]")]
|
||||||
public class CartController : Controller
|
public class CartController : Controller
|
||||||
{
|
{
|
||||||
private readonly IBasketService _basketService;
|
private readonly IBasketService _basketService;
|
||||||
//private readonly IIdentityParser<ApplicationUser> _appUserParser;
|
|
||||||
private const string _basketSessionKey = "basketId";
|
private const string _basketSessionKey = "basketId";
|
||||||
private readonly IUriComposer _uriComposer;
|
private readonly IUriComposer _uriComposer;
|
||||||
|
|
||||||
public CartController(IBasketService basketService,
|
public CartController(IBasketService basketService,
|
||||||
IUriComposer uriComposer)
|
IUriComposer uriComposer)
|
||||||
// IIdentityParser<ApplicationUser> appUserParser)
|
|
||||||
{
|
{
|
||||||
_basketService = basketService;
|
_basketService = basketService;
|
||||||
_uriComposer = uriComposer;
|
_uriComposer = uriComposer;
|
||||||
// _appUserParser = appUserParser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
// GET: /<controller>/
|
|
||||||
public async Task<IActionResult> Index()
|
public async Task<IActionResult> Index()
|
||||||
{
|
{
|
||||||
//var user = _appUserParser.Parse(HttpContext.User);
|
|
||||||
var basketModel = await GetBasketFromSessionAsync();
|
var basketModel = await GetBasketFromSessionAsync();
|
||||||
|
|
||||||
|
|
||||||
return View(basketModel);
|
return View(basketModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: /Cart/AddToCart
|
// POST: /Cart/AddToCart
|
||||||
// TODO: This should be a POST.
|
[HttpPost]
|
||||||
public async Task<IActionResult> AddToCart(CatalogItem productDetails)
|
public async Task<IActionResult> AddToCart(CatalogItemViewModel productDetails)
|
||||||
{
|
{
|
||||||
if (productDetails?.Id == null)
|
if (productDetails?.Id == null)
|
||||||
{
|
{
|
||||||
@@ -50,6 +43,16 @@ namespace Microsoft.eShopWeb.Controllers
|
|||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> Checkout()
|
||||||
|
{
|
||||||
|
var basket = await GetBasketFromSessionAsync();
|
||||||
|
|
||||||
|
await _basketService.Checkout(basket.Id);
|
||||||
|
|
||||||
|
return View("Checkout");
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<BasketViewModel> GetBasketFromSessionAsync()
|
private async Task<BasketViewModel> GetBasketFromSessionAsync()
|
||||||
{
|
{
|
||||||
string basketId = HttpContext.Session.GetString(_basketSessionKey);
|
string basketId = HttpContext.Session.GetString(_basketSessionKey);
|
||||||
|
|||||||
@@ -4,13 +4,15 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Microsoft.eShopWeb.Controllers
|
namespace Microsoft.eShopWeb.Controllers
|
||||||
{
|
{
|
||||||
|
[Route("")]
|
||||||
public class CatalogController : Controller
|
public class CatalogController : Controller
|
||||||
{
|
{
|
||||||
private readonly ICatalogService _catalogService;
|
private readonly ICatalogService _catalogService;
|
||||||
|
|
||||||
public CatalogController(ICatalogService catalogService) => _catalogService = catalogService;
|
public CatalogController(ICatalogService catalogService) => _catalogService = catalogService;
|
||||||
|
|
||||||
// GET: /<controller>/
|
[HttpGet]
|
||||||
|
[HttpPost]
|
||||||
public async Task<IActionResult> Index(int? brandFilterApplied, int? typesFilterApplied, int? page)
|
public async Task<IActionResult> Index(int? brandFilterApplied, int? typesFilterApplied, int? page)
|
||||||
{
|
{
|
||||||
var itemsPage = 10;
|
var itemsPage = 10;
|
||||||
@@ -18,6 +20,7 @@ namespace Microsoft.eShopWeb.Controllers
|
|||||||
return View(catalogModel);
|
return View(catalogModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("Error")]
|
||||||
public IActionResult Error()
|
public IActionResult Error()
|
||||||
{
|
{
|
||||||
return View();
|
return View();
|
||||||
|
|||||||
@@ -10,5 +10,7 @@ namespace ApplicationCore.Interfaces
|
|||||||
Task<BasketViewModel> CreateBasketForUser(string userId);
|
Task<BasketViewModel> CreateBasketForUser(string userId);
|
||||||
|
|
||||||
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity);
|
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity);
|
||||||
|
|
||||||
|
Task Checkout(int basketId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
using ApplicationCore.Interfaces;
|
using ApplicationCore.Interfaces;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using System;
|
using System;
|
||||||
using Infrastructure.Data;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.eShopWeb.ViewModels;
|
using Microsoft.eShopWeb.ViewModels;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -82,5 +80,14 @@ namespace Web.Services
|
|||||||
|
|
||||||
_basketRepository.Update(basket);
|
_basketRepository.Update(basket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task Checkout(int basketId)
|
||||||
|
{
|
||||||
|
var basket = _basketRepository.GetById(basketId);
|
||||||
|
|
||||||
|
// TODO: Actually Process the order
|
||||||
|
|
||||||
|
_basketRepository.Delete(basket);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,13 +132,7 @@ namespace Microsoft.eShopWeb
|
|||||||
|
|
||||||
app.UseIdentity();
|
app.UseIdentity();
|
||||||
|
|
||||||
app.UseMvc(routes =>
|
app.UseMvc();
|
||||||
{
|
|
||||||
routes.MapRoute(
|
|
||||||
name: "default",
|
|
||||||
template: "{controller=Catalog}/{action=Index}/{id?}");
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ConfigureDevelopment(IApplicationBuilder app,
|
public void ConfigureDevelopment(IApplicationBuilder app,
|
||||||
|
|||||||
16
src/Web/Views/Cart/Checkout.cshtml
Normal file
16
src/Web/Views/Cart/Checkout.cshtml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
@using Microsoft.eShopWeb.ViewModels
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Checkout Complete";
|
||||||
|
@model BasketViewModel
|
||||||
|
}
|
||||||
|
<section class="esh-catalog-hero">
|
||||||
|
<div class="container">
|
||||||
|
<img class="esh-catalog-title" src="../images/main_banner_text.png" />
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<h1>Thanks for your Order!</h1>
|
||||||
|
|
||||||
|
<a asp-controller="Catalog" asp-action="Index">Continue Shopping...</a>
|
||||||
|
</div>
|
||||||
@@ -13,64 +13,66 @@
|
|||||||
|
|
||||||
@if (Model.Items.Any())
|
@if (Model.Items.Any())
|
||||||
{
|
{
|
||||||
<article class="esh-basket-titles row">
|
<form method="post">
|
||||||
<br />
|
<article class="esh-basket-titles row">
|
||||||
<section class="esh-basket-title col-xs-3">Product</section>
|
<br />
|
||||||
<section class="esh-basket-title col-xs-3 hidden-lg-down"></section>
|
<section class="esh-basket-title col-xs-3">Product</section>
|
||||||
<section class="esh-basket-title col-xs-2">Price</section>
|
<section class="esh-basket-title col-xs-3 hidden-lg-down"></section>
|
||||||
<section class="esh-basket-title col-xs-2">Quantity</section>
|
<section class="esh-basket-title col-xs-2">Price</section>
|
||||||
<section class="esh-basket-title col-xs-2">Cost</section>
|
<section class="esh-basket-title col-xs-2">Quantity</section>
|
||||||
</article>
|
<section class="esh-basket-title col-xs-2">Cost</section>
|
||||||
<div class="esh-catalog-items row">
|
</article>
|
||||||
@foreach (var item in Model.Items)
|
<div class="esh-catalog-items row">
|
||||||
{
|
@foreach (var item in Model.Items)
|
||||||
<article class="esh-basket-items row">
|
{
|
||||||
<div>
|
<article class="esh-basket-items row">
|
||||||
<section class="esh-basket-item esh-basket-item--middle col-lg-3 hidden-lg-down">
|
<div>
|
||||||
<img class="esh-basket-image" src="@item.PictureUrl" />
|
<section class="esh-basket-item esh-basket-item--middle col-lg-3 hidden-lg-down">
|
||||||
</section>
|
<img class="esh-basket-image" src="@item.PictureUrl" />
|
||||||
<section class="esh-basket-item esh-basket-item--middle col-xs-3">@item.ProductName</section>
|
</section>
|
||||||
<section class="esh-basket-item esh-basket-item--middle col-xs-2">$ @item.UnitPrice.ToString("N2")</section>
|
<section class="esh-basket-item esh-basket-item--middle col-xs-3">@item.ProductName</section>
|
||||||
<section class="esh-basket-item esh-basket-item--middle col-xs-2">
|
<section class="esh-basket-item esh-basket-item--middle col-xs-2">$ @item.UnitPrice.ToString("N2")</section>
|
||||||
<input type="hidden" name="@("quantities[" + item.Id +"].Key")" value="@item.Id" />
|
<section class="esh-basket-item esh-basket-item--middle col-xs-2">
|
||||||
<input type="number" class="esh-basket-input" min="1" name="@("quantities[" + item.Id +"].Value")" value="@item.Quantity" />
|
<input type="hidden" name="@("quantities[" + item.Id +"].Key")" value="@item.Id" />
|
||||||
</section>
|
<input type="number" class="esh-basket-input" min="1" name="@("quantities[" + item.Id +"].Value")" value="@item.Quantity" />
|
||||||
<section class="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2">$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")</section>
|
</section>
|
||||||
|
<section class="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2">$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")</section>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
|
@*<div class="esh-catalog-item col-md-4">
|
||||||
|
@item.ProductId
|
||||||
|
</div>*@
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<article class="esh-basket-titles esh-basket-titles--clean row">
|
||||||
|
<section class="esh-basket-title col-xs-10"></section>
|
||||||
|
<section class="esh-basket-title col-xs-2">Total</section>
|
||||||
|
</article>
|
||||||
|
|
||||||
|
<article class="esh-basket-items row">
|
||||||
|
<section class="esh-basket-item col-xs-10"></section>
|
||||||
|
<section class="esh-basket-item esh-basket-item--mark col-xs-2">$ @Model.Total()</section>
|
||||||
|
</article>
|
||||||
|
|
||||||
|
<article class="esh-basket-items row">
|
||||||
|
<section class="esh-basket-item col-xs-7"></section>
|
||||||
|
<section class="esh-basket-item col-xs-2">
|
||||||
|
@*<button class="btn esh-basket-checkout" name="name" value="" type="submit">[ Update ]</button>*@
|
||||||
|
</section>
|
||||||
|
</article>
|
||||||
</div>
|
</div>
|
||||||
<div class="row">
|
}
|
||||||
|
<section class="esh-basket-item col-xs-push-9 col-xs-3">
|
||||||
</div>
|
<input type="submit" asp-action="Checkout"
|
||||||
</article>
|
class="btn esh-basket-checkout"
|
||||||
@*<div class="esh-catalog-item col-md-4">
|
value="[ Checkout ]" name="action" />
|
||||||
@item.ProductId
|
</section>
|
||||||
</div>*@
|
|
||||||
|
|
||||||
<div class="container">
|
|
||||||
<article class="esh-basket-titles esh-basket-titles--clean row">
|
|
||||||
<section class="esh-basket-title col-xs-10"></section>
|
|
||||||
<section class="esh-basket-title col-xs-2">Total</section>
|
|
||||||
</article>
|
|
||||||
|
|
||||||
<article class="esh-basket-items row">
|
|
||||||
<section class="esh-basket-item col-xs-10"></section>
|
|
||||||
<section class="esh-basket-item esh-basket-item--mark col-xs-2">$ @Model.Total()</section>
|
|
||||||
</article>
|
|
||||||
|
|
||||||
<article class="esh-basket-items row">
|
|
||||||
<section class="esh-basket-item col-xs-7"></section>
|
|
||||||
<section class="esh-basket-item col-xs-2">
|
|
||||||
<button class="btn esh-basket-checkout" name="name" value="" type="submit">[ Update ]</button>
|
|
||||||
</section>
|
|
||||||
<section class="esh-basket-item col-xs-3">
|
|
||||||
<input type="submit"
|
|
||||||
class="btn esh-basket-checkout"
|
|
||||||
value="[ Checkout ]" name="action" />
|
|
||||||
</section>
|
|
||||||
</article>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</section>
|
</section>
|
||||||
@await Html.PartialAsync("_LoginPartial")
|
@await Html.PartialAsync("_LoginPartial")
|
||||||
|
<section class="col-lg-1 col-xs-12"><a asp-controller="Cart" asp-action="Index">Cart</a></section>
|
||||||
|
|
||||||
</article>
|
</article>
|
||||||
</div>
|
</div>
|
||||||
@@ -47,7 +48,7 @@
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section class="col-sm-6">
|
<section class="col-sm-6">
|
||||||
<div class="esh-app-footer-text hidden-xs"> e-ShoponContainers. All right reserved </div>
|
<div class="esh-app-footer-text hidden-xs"> e-ShopOnWeb. All right reserved </div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
</article>
|
</article>
|
||||||
|
|||||||
@@ -14,13 +14,13 @@
|
|||||||
|
|
||||||
<section class="esh-identity-drop">
|
<section class="esh-identity-drop">
|
||||||
|
|
||||||
<a class="esh-identity-item"
|
@*<a class="esh-identity-item"
|
||||||
asp-controller="Order"
|
asp-controller="Order"
|
||||||
asp-action="Index">
|
asp-action="Index">
|
||||||
|
|
||||||
<div class="esh-identity-name esh-identity-name--upper">My orders</div>
|
<div class="esh-identity-name esh-identity-name--upper">My orders</div>
|
||||||
<img class="esh-identity-image" src="~/images/my_orders.png">
|
<img class="esh-identity-image" src="~/images/my_orders.png">
|
||||||
</a>
|
</a>*@
|
||||||
|
|
||||||
<a class="esh-identity-item"
|
<a class="esh-identity-item"
|
||||||
href="javascript:document.getElementById('logoutForm').submit()">
|
href="javascript:document.getElementById('logoutForm').submit()">
|
||||||
@@ -33,9 +33,9 @@
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section class="col-lg-1 col-xs-12">
|
@*<section class="col-lg-1 col-xs-12">
|
||||||
@*@await Component.InvokeAsync("Cart", new { user = UserManager.Parse(User) })*@
|
@await Component.InvokeAsync("Cart", new { user = UserManager.Parse(User) })
|
||||||
</section>
|
</section>*@
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -53,5 +53,5 @@ else
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section class="col-lg-1 col-xs-12"></section>
|
@*<section class="col-lg-1 col-xs-12"></section>*@
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user