cleaning up controllers (#19)

This commit is contained in:
Steve Smith
2017-07-26 17:43:32 -04:00
committed by GitHub
parent b645cb35c1
commit 11ace4ea35
3 changed files with 21 additions and 66 deletions

View File

@@ -1,6 +1,4 @@
using Microsoft.eShopWeb.Services; using Microsoft.eShopWeb.ViewModels;
using Microsoft.eShopWeb.ViewModels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
@@ -16,7 +14,6 @@ namespace Microsoft.eShopWeb.Controllers
private readonly SignInManager<ApplicationUser> _signInManager; private readonly SignInManager<ApplicationUser> _signInManager;
private readonly string _externalCookieScheme; private readonly string _externalCookieScheme;
public AccountController( public AccountController(
UserManager<ApplicationUser> userManager, UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager, SignInManager<ApplicationUser> signInManager,
@@ -27,57 +24,37 @@ namespace Microsoft.eShopWeb.Controllers
_userManager = userManager; _userManager = userManager;
_signInManager = signInManager; _signInManager = signInManager;
_externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme; _externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
} }
//
// GET: /Account/SignIn // GET: /Account/SignIn
[HttpGet] [HttpGet]
[AllowAnonymous] [AllowAnonymous]
public async Task<IActionResult> SignIn(string returnUrl = null) public async Task<IActionResult> SignIn(string returnUrl = null)
{ {
// Clear the existing external cookie to ensure a clean login process
await HttpContext.Authentication.SignOutAsync(_externalCookieScheme); await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);
ViewData["ReturnUrl"] = returnUrl; ViewData["ReturnUrl"] = returnUrl;
return View(); return View();
} }
//
// POST: /Account/SignIn // POST: /Account/SignIn
[HttpPost] [HttpPost]
[AllowAnonymous] [AllowAnonymous]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> SignIn(LoginViewModel model, string returnUrl = null) public async Task<IActionResult> SignIn(LoginViewModel model, string returnUrl = null)
{ {
ViewData["ReturnUrl"] = returnUrl; if (!ModelState.IsValid)
if (ModelState.IsValid)
{ {
// This doesn't count login failures towards account lockout return View(model);
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
//_logger.LogInformation(1, "User logged in.");
return RedirectToLocal(returnUrl);
}
//if (result.RequiresTwoFactor)
//{
// return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
//}
if (result.IsLockedOut)
{
//_logger.LogWarning(2, "User account locked out.");
return View("Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
} }
ViewData["ReturnUrl"] = returnUrl;
// If we got this far, something failed, redisplay form var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
return RedirectToLocal(returnUrl);
}
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model); return View(model);
} }
@@ -92,7 +69,5 @@ namespace Microsoft.eShopWeb.Controllers
return RedirectToAction(nameof(CatalogController.Index), "Catalog"); return RedirectToAction(nameof(CatalogController.Index), "Catalog");
} }
} }
} }
} }

View File

@@ -32,22 +32,21 @@ namespace Microsoft.eShopWeb.Controllers
return View(viewmodel); return View(viewmodel);
} }
// GET: /Cart/AddToCart
// TODO: This should be a POST.
public async Task<IActionResult> AddToCart(CatalogItem productDetails) public async Task<IActionResult> AddToCart(CatalogItem productDetails)
{ {
if (productDetails.Id != null) var user = _appUserParser.Parse(HttpContext.User);
var product = new BasketItem()
{ {
var user = _appUserParser.Parse(HttpContext.User); Id = Guid.NewGuid().ToString(),
var product = new BasketItem() Quantity = 1,
{ UnitPrice = productDetails.Price,
Id = Guid.NewGuid().ToString(), ProductId = productDetails.Id
Quantity = 1, };
UnitPrice = productDetails.Price, // TODO: Save the item
ProductId = productDetails.Id //await _basketSvc.AddItemToBasket(user, product);
};
//await _basketSvc.AddItemToBasket(user, product);
}
return RedirectToAction("Index", "Catalog"); return RedirectToAction("Index", "Catalog");
} }
} }
} }

View File

@@ -5,8 +5,6 @@ using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using ApplicationCore.Interfaces; using ApplicationCore.Interfaces;
using ApplicationCore.Exceptions;
using Microsoft.Extensions.Logging;
namespace Microsoft.eShopWeb.Controllers namespace Microsoft.eShopWeb.Controllers
{ {
@@ -56,23 +54,6 @@ namespace Microsoft.eShopWeb.Controllers
return View(vm); return View(vm);
} }
//[HttpGet("[controller]/pic/{id}")]
//public IActionResult GetImage(int id)
//{
// byte[] imageBytes;
// try
// {
// imageBytes = _imageService.GetImageBytesById(id);
// }
// catch (CatalogImageMissingException ex)
// {
// _logger.LogWarning($"No image found for id: {id}");
// return NotFound();
// }
// return File(imageBytes, "image/png");
//}
public IActionResult Error() public IActionResult Error()
{ {
return View(); return View();