diff --git a/src/Web/Controllers/AccountController.cs b/src/Web/Controllers/AccountController.cs index 2658e98..ccaffa8 100644 --- a/src/Web/Controllers/AccountController.cs +++ b/src/Web/Controllers/AccountController.cs @@ -8,6 +8,7 @@ using Infrastructure.Identity; namespace Microsoft.eShopWeb.Controllers { + [Route("[controller]/[action]")] public class AccountController : Controller { private readonly UserManager _userManager; @@ -17,7 +18,7 @@ namespace Microsoft.eShopWeb.Controllers public AccountController( UserManager userManager, SignInManager signInManager, - IOptions identityCookieOptions + IOptions identityCookieOptions ) { @@ -58,6 +59,16 @@ namespace Microsoft.eShopWeb.Controllers return View(model); } + [HttpPost] + [ValidateAntiForgeryToken] + public async Task SignOut() + { + HttpContext.Session.Clear(); + await _signInManager.SignOutAsync(); + + return RedirectToAction(nameof(CatalogController.Index), "Catalog"); + } + private IActionResult RedirectToLocal(string returnUrl) { if (Url.IsLocalUrl(returnUrl)) diff --git a/src/Web/Controllers/CartController.cs b/src/Web/Controllers/CartController.cs index e23b3f3..64d2a5e 100644 --- a/src/Web/Controllers/CartController.cs +++ b/src/Web/Controllers/CartController.cs @@ -1,43 +1,36 @@ using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; using ApplicationCore.Interfaces; -using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.AspNetCore.Http; using Microsoft.eShopWeb.ViewModels; -using System.Linq; namespace Microsoft.eShopWeb.Controllers { + [Route("[controller]/[action]")] public class CartController : Controller { private readonly IBasketService _basketService; - //private readonly IIdentityParser _appUserParser; private const string _basketSessionKey = "basketId"; private readonly IUriComposer _uriComposer; public CartController(IBasketService basketService, IUriComposer uriComposer) -// IIdentityParser appUserParser) { _basketService = basketService; _uriComposer = uriComposer; - // _appUserParser = appUserParser; } - - // GET: // + [HttpGet] public async Task Index() { - //var user = _appUserParser.Parse(HttpContext.User); var basketModel = await GetBasketFromSessionAsync(); - return View(basketModel); } - // GET: /Cart/AddToCart - // TODO: This should be a POST. - public async Task AddToCart(CatalogItem productDetails) + // POST: /Cart/AddToCart + [HttpPost] + public async Task AddToCart(CatalogItemViewModel productDetails) { if (productDetails?.Id == null) { @@ -50,6 +43,16 @@ namespace Microsoft.eShopWeb.Controllers return RedirectToAction("Index"); } + [HttpPost] + public async Task Checkout() + { + var basket = await GetBasketFromSessionAsync(); + + await _basketService.Checkout(basket.Id); + + return View("Checkout"); + } + private async Task GetBasketFromSessionAsync() { string basketId = HttpContext.Session.GetString(_basketSessionKey); diff --git a/src/Web/Controllers/CatalogController.cs b/src/Web/Controllers/CatalogController.cs index 4b57f03..de7475b 100644 --- a/src/Web/Controllers/CatalogController.cs +++ b/src/Web/Controllers/CatalogController.cs @@ -4,13 +4,15 @@ using System.Threading.Tasks; namespace Microsoft.eShopWeb.Controllers { + [Route("")] public class CatalogController : Controller { private readonly ICatalogService _catalogService; public CatalogController(ICatalogService catalogService) => _catalogService = catalogService; - // GET: // + [HttpGet] + [HttpPost] public async Task Index(int? brandFilterApplied, int? typesFilterApplied, int? page) { var itemsPage = 10; @@ -18,6 +20,7 @@ namespace Microsoft.eShopWeb.Controllers return View(catalogModel); } + [HttpGet("Error")] public IActionResult Error() { return View(); diff --git a/src/Web/Interfaces/IBasketService.cs b/src/Web/Interfaces/IBasketService.cs index c72826d..51e982a 100644 --- a/src/Web/Interfaces/IBasketService.cs +++ b/src/Web/Interfaces/IBasketService.cs @@ -10,5 +10,7 @@ namespace ApplicationCore.Interfaces Task CreateBasketForUser(string userId); Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity); + + Task Checkout(int basketId); } } diff --git a/src/Web/Services/BasketService.cs b/src/Web/Services/BasketService.cs index 8f0cedf..1b9b494 100644 --- a/src/Web/Services/BasketService.cs +++ b/src/Web/Services/BasketService.cs @@ -1,9 +1,7 @@ using ApplicationCore.Interfaces; using System.Threading.Tasks; using Microsoft.eShopWeb.ApplicationCore.Entities; -using Microsoft.EntityFrameworkCore; using System; -using Infrastructure.Data; using System.Linq; using Microsoft.eShopWeb.ViewModels; using System.Collections.Generic; @@ -82,5 +80,14 @@ namespace Web.Services _basketRepository.Update(basket); } + + public async Task Checkout(int basketId) + { + var basket = _basketRepository.GetById(basketId); + + // TODO: Actually Process the order + + _basketRepository.Delete(basket); + } } } diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs index a6a863e..53476c6 100644 --- a/src/Web/Startup.cs +++ b/src/Web/Startup.cs @@ -132,13 +132,7 @@ namespace Microsoft.eShopWeb app.UseIdentity(); - app.UseMvc(routes => - { - routes.MapRoute( - name: "default", - template: "{controller=Catalog}/{action=Index}/{id?}"); - }); - + app.UseMvc(); } public void ConfigureDevelopment(IApplicationBuilder app, diff --git a/src/Web/Views/Cart/Checkout.cshtml b/src/Web/Views/Cart/Checkout.cshtml new file mode 100644 index 0000000..b0e061f --- /dev/null +++ b/src/Web/Views/Cart/Checkout.cshtml @@ -0,0 +1,16 @@ +@using Microsoft.eShopWeb.ViewModels +@{ + ViewData["Title"] = "Checkout Complete"; + @model BasketViewModel +} +
+
+ +
+
+ +
+

Thanks for your Order!

+ + Continue Shopping... +
diff --git a/src/Web/Views/Cart/Index.cshtml b/src/Web/Views/Cart/Index.cshtml index 9257e48..0ca7f6f 100644 --- a/src/Web/Views/Cart/Index.cshtml +++ b/src/Web/Views/Cart/Index.cshtml @@ -13,64 +13,66 @@ @if (Model.Items.Any()) { -
-
-
Product
-
-
Price
-
Quantity
-
Cost
-
-
- @foreach (var item in Model.Items) - { -
-
-
- -
-
@item.ProductName
-
$ @item.UnitPrice.ToString("N2")
-
- - -
-
$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")
+
+
+
+
Product
+
+
Price
+
Quantity
+
Cost
+
+
+ @foreach (var item in Model.Items) + { +
+
+
+ +
+
@item.ProductName
+
$ @item.UnitPrice.ToString("N2")
+
+ + +
+
$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")
+
+
+ +
+
+ @*
+ @item.ProductId +
*@ + +
+
+
+
Total
+
+ +
+
+
$ @Model.Total()
+
+ +
+
+
+ @**@ +
+
-
- -
-
- @*
- @item.ProductId -
*@ - -
-
-
-
Total
-
- -
-
-
$ @Model.Total()
-
- -
-
-
- -
-
- -
-
-
- } -
+ } +
+ +
+ + } else { diff --git a/src/Web/Views/Shared/_Layout.cshtml b/src/Web/Views/Shared/_Layout.cshtml index 2e77475..165e2f2 100644 --- a/src/Web/Views/Shared/_Layout.cshtml +++ b/src/Web/Views/Shared/_Layout.cshtml @@ -30,6 +30,7 @@ @await Html.PartialAsync("_LoginPartial") +
Cart
@@ -47,7 +48,7 @@
- +
diff --git a/src/Web/Views/Shared/_LoginPartial.cshtml b/src/Web/Views/Shared/_LoginPartial.cshtml index eae827a..23160ce 100644 --- a/src/Web/Views/Shared/_LoginPartial.cshtml +++ b/src/Web/Views/Shared/_LoginPartial.cshtml @@ -14,13 +14,13 @@
-
My orders
-
+ *@ @@ -33,9 +33,9 @@
-
- @*@await Component.InvokeAsync("Cart", new { user = UserManager.Parse(User) })*@ -
+ @*
+ @await Component.InvokeAsync("Cart", new { user = UserManager.Parse(User) }) +
*@ } else @@ -53,5 +53,5 @@ else -
+ @*
*@ }