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.AspNetCore.Hosting;
using Microsoft.eShopWeb.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
@@ -16,7 +14,6 @@ namespace Microsoft.eShopWeb.Controllers
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly string _externalCookieScheme;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
@@ -27,57 +24,37 @@ namespace Microsoft.eShopWeb.Controllers
_userManager = userManager;
_signInManager = signInManager;
_externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
}
//
// GET: /Account/SignIn
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> SignIn(string returnUrl = null)
{
// Clear the existing external cookie to ensure a clean login process
await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);
ViewData["ReturnUrl"] = returnUrl;
return View();
}
//
// POST: /Account/SignIn
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
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
// 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);
}
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);
}
@@ -92,7 +69,5 @@ namespace Microsoft.eShopWeb.Controllers
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
}
}
}
}

View File

@@ -32,22 +32,21 @@ namespace Microsoft.eShopWeb.Controllers
return View(viewmodel);
}
// GET: /Cart/AddToCart
// TODO: This should be a POST.
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);
var product = new BasketItem()
{
Id = Guid.NewGuid().ToString(),
Quantity = 1,
UnitPrice = productDetails.Price,
ProductId = productDetails.Id
};
//await _basketSvc.AddItemToBasket(user, product);
}
Id = Guid.NewGuid().ToString(),
Quantity = 1,
UnitPrice = productDetails.Price,
ProductId = productDetails.Id
};
// TODO: Save the item
//await _basketSvc.AddItemToBasket(user, product);
return RedirectToAction("Index", "Catalog");
}
}
}

View File

@@ -5,8 +5,6 @@ using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using ApplicationCore.Interfaces;
using ApplicationCore.Exceptions;
using Microsoft.Extensions.Logging;
namespace Microsoft.eShopWeb.Controllers
{
@@ -56,23 +54,6 @@ namespace Microsoft.eShopWeb.Controllers
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()
{
return View();