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
|
||||
{
|
||||
[Route("[controller]/[action]")]
|
||||
public class AccountController : Controller
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
@@ -17,7 +18,7 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
public AccountController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
IOptions<IdentityCookieOptions> identityCookieOptions
|
||||
IOptions<IdentityCookieOptions> identityCookieOptions
|
||||
|
||||
)
|
||||
{
|
||||
@@ -58,6 +59,16 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
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)
|
||||
{
|
||||
if (Url.IsLocalUrl(returnUrl))
|
||||
|
||||
@@ -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<ApplicationUser> _appUserParser;
|
||||
private const string _basketSessionKey = "basketId";
|
||||
private readonly IUriComposer _uriComposer;
|
||||
|
||||
public CartController(IBasketService basketService,
|
||||
IUriComposer uriComposer)
|
||||
// IIdentityParser<ApplicationUser> appUserParser)
|
||||
{
|
||||
_basketService = basketService;
|
||||
_uriComposer = uriComposer;
|
||||
// _appUserParser = appUserParser;
|
||||
}
|
||||
|
||||
|
||||
// GET: /<controller>/
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> 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<IActionResult> AddToCart(CatalogItem productDetails)
|
||||
// POST: /Cart/AddToCart
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> AddToCart(CatalogItemViewModel productDetails)
|
||||
{
|
||||
if (productDetails?.Id == null)
|
||||
{
|
||||
@@ -50,6 +43,16 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
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()
|
||||
{
|
||||
string basketId = HttpContext.Session.GetString(_basketSessionKey);
|
||||
|
||||
@@ -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: /<controller>/
|
||||
[HttpGet]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> 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();
|
||||
|
||||
Reference in New Issue
Block a user