Initial Upgrade to .NET Core 2.0 (#50)
* Ardalis/upgrade1 (#44) * Upgrading to netcore 2.0 Updating repository to support async options and refactoring to use it. * Starting work on tracking customer orders feature. * Cleaning up some bugs Working on basket view component implementation * Fixing up styles, especially for basket in header. * Adding Order Features (#47) * Working on order model binding from checkout page - WIP * Small layout tweaks (#43) * Updating quantities implemented. * Fixed basket widget count * Order History (#49) * working on creating and viewing orders. * Working on wiring up listing of orders * List orders page works as expected. Needed to support ThenInclude scenarios. Currently using strings.
This commit is contained in:
@@ -3,12 +3,11 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Infrastructure.Identity;
|
||||
using System;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using ApplicationCore.Interfaces;
|
||||
using Web;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
|
||||
namespace Microsoft.eShopWeb.Controllers
|
||||
{
|
||||
@@ -17,18 +16,15 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly string _externalCookieScheme;
|
||||
private readonly IBasketService _basketService;
|
||||
|
||||
public AccountController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
IOptions<IdentityCookieOptions> identityCookieOptions,
|
||||
IBasketService basketService)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
_externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
|
||||
_basketService = basketService;
|
||||
}
|
||||
|
||||
@@ -37,9 +33,15 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> SignIn(string returnUrl = null)
|
||||
{
|
||||
await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);
|
||||
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
|
||||
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
if (!String.IsNullOrEmpty(returnUrl) &&
|
||||
returnUrl.ToLower().Contains("checkout"))
|
||||
{
|
||||
ViewData["ReturnUrl"] = "/Basket/Index";
|
||||
}
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
@@ -61,7 +63,7 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
string anonymousBasketId = Request.Cookies[Constants.BASKET_COOKIENAME];
|
||||
if (!String.IsNullOrEmpty(anonymousBasketId))
|
||||
{
|
||||
_basketService.TransferBasket(anonymousBasketId, model.Email);
|
||||
await _basketService.TransferBasketAsync(anonymousBasketId, model.Email);
|
||||
Response.Cookies.Delete(Constants.BASKET_COOKIENAME);
|
||||
}
|
||||
return RedirectToLocal(returnUrl);
|
||||
@@ -74,7 +76,6 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<ActionResult> SignOut()
|
||||
{
|
||||
HttpContext.Session.Clear();
|
||||
await _signInManager.SignOutAsync();
|
||||
|
||||
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
|
||||
|
||||
@@ -7,6 +7,9 @@ using Microsoft.AspNetCore.Identity;
|
||||
using Infrastructure.Identity;
|
||||
using System;
|
||||
using Web;
|
||||
using System.Collections.Generic;
|
||||
using ApplicationCore.Entities.OrderAggregate;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace Microsoft.eShopWeb.Controllers
|
||||
{
|
||||
@@ -17,14 +20,20 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
private const string _basketSessionKey = "basketId";
|
||||
private readonly IUriComposer _uriComposer;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly IAppLogger<BasketController> _logger;
|
||||
private readonly IOrderService _orderService;
|
||||
|
||||
public BasketController(IBasketService basketService,
|
||||
IOrderService orderService,
|
||||
IUriComposer uriComposer,
|
||||
SignInManager<ApplicationUser> signInManager)
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
IAppLogger<BasketController> logger)
|
||||
{
|
||||
_basketService = basketService;
|
||||
_uriComposer = uriComposer;
|
||||
_signInManager = signInManager;
|
||||
_logger = logger;
|
||||
_orderService = orderService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -35,6 +44,16 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
return View(basketModel);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Index(Dictionary<string, int> items)
|
||||
{
|
||||
var basketViewModel = await GetBasketViewModelAsync();
|
||||
await _basketService.SetQuantities(basketViewModel.Id, items);
|
||||
|
||||
return View(await GetBasketViewModelAsync());
|
||||
}
|
||||
|
||||
|
||||
// POST: /Basket/AddToBasket
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> AddToBasket(CatalogItemViewModel productDetails)
|
||||
@@ -51,11 +70,15 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Checkout()
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Checkout(Dictionary<string, int> items)
|
||||
{
|
||||
var basket = await GetBasketViewModelAsync();
|
||||
var basketViewModel = await GetBasketViewModelAsync();
|
||||
await _basketService.SetQuantities(basketViewModel.Id, items);
|
||||
|
||||
await _basketService.Checkout(basket.Id);
|
||||
await _orderService.CreateOrderAsync(basketViewModel.Id, new Address("123 Main St.", "Kent", "OH", "United States", "44240"));
|
||||
|
||||
await _basketService.DeleteBasketAsync(basketViewModel.Id);
|
||||
|
||||
return View("Checkout");
|
||||
}
|
||||
|
||||
96
src/Web/Controllers/OrderController.cs
Normal file
96
src/Web/Controllers/OrderController.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.eShopWeb.ViewModels;
|
||||
using System;
|
||||
using ApplicationCore.Entities.OrderAggregate;
|
||||
using ApplicationCore.Interfaces;
|
||||
using System.Linq;
|
||||
using ApplicationCore.Specifications;
|
||||
|
||||
namespace Microsoft.eShopWeb.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("[controller]/[action]")]
|
||||
public class OrderController : Controller
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
|
||||
public OrderController(IOrderRepository orderRepository) {
|
||||
_orderRepository = orderRepository;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var orders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(User.Identity.Name));
|
||||
|
||||
var viewModel = orders
|
||||
.Select(o => new OrderViewModel()
|
||||
{
|
||||
OrderDate = o.OrderDate,
|
||||
OrderItems = o.OrderItems?.Select(oi => new OrderItemViewModel()
|
||||
{
|
||||
Discount = 0,
|
||||
PictureUrl = oi.ItemOrdered.PictureUri,
|
||||
ProductId = oi.ItemOrdered.CatalogItemId,
|
||||
ProductName = oi.ItemOrdered.ProductName,
|
||||
UnitPrice = oi.UnitPrice,
|
||||
Units = oi.Units
|
||||
}).ToList(),
|
||||
OrderNumber = o.Id,
|
||||
ShippingAddress = o.ShipToAddress,
|
||||
Status = "Pending",
|
||||
Total = o.Total()
|
||||
|
||||
});
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[HttpGet("{orderId}")]
|
||||
public async Task<IActionResult> Detail(int orderId)
|
||||
{
|
||||
var order = await _orderRepository.GetByIdWithItemsAsync(orderId);
|
||||
var viewModel = new OrderViewModel()
|
||||
{
|
||||
OrderDate = order.OrderDate,
|
||||
OrderItems = order.OrderItems.Select(oi => new OrderItemViewModel()
|
||||
{
|
||||
Discount = 0,
|
||||
PictureUrl = oi.ItemOrdered.PictureUri,
|
||||
ProductId = oi.ItemOrdered.CatalogItemId,
|
||||
ProductName = oi.ItemOrdered.ProductName,
|
||||
UnitPrice = oi.UnitPrice,
|
||||
Units = oi.Units
|
||||
}).ToList(),
|
||||
OrderNumber = order.Id,
|
||||
ShippingAddress = order.ShipToAddress,
|
||||
Status = "Pending",
|
||||
Total = order.Total()
|
||||
};
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
private OrderViewModel GetOrder()
|
||||
{
|
||||
var order = new OrderViewModel()
|
||||
{
|
||||
OrderDate = DateTimeOffset.Now.AddDays(-1),
|
||||
OrderNumber = 12354,
|
||||
Status = "Submitted",
|
||||
Total = 123.45m,
|
||||
ShippingAddress = new Address("123 Main St.", "Kent", "OH", "United States", "44240")
|
||||
};
|
||||
|
||||
order.OrderItems.Add(new OrderItemViewModel()
|
||||
{
|
||||
ProductId = 1,
|
||||
PictureUrl = "",
|
||||
ProductName = "Something",
|
||||
UnitPrice = 5.05m,
|
||||
Units = 2
|
||||
});
|
||||
|
||||
return order;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user