committed by
GitHub
13 changed files with 179 additions and 49 deletions
@ -1,10 +0,0 @@ |
|||||
using System.Security.Claims; |
|
||||
|
|
||||
namespace ApplicationCore.Entities |
|
||||
{ |
|
||||
public class ApplicationUser : ClaimsIdentity |
|
||||
{ |
|
||||
public string UserId { get; set; } |
|
||||
public string UserName { get; set; } |
|
||||
} |
|
||||
} |
|
||||
@ -1,17 +1,13 @@ |
|||||
using ApplicationCore.Entities; |
using Microsoft.eShopWeb.ApplicationCore.Entities; |
||||
using Microsoft.eShopWeb.ApplicationCore.Entities; |
|
||||
using System.Security.Principal; |
|
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
|
|
||||
namespace ApplicationCore.Interfaces |
namespace ApplicationCore.Interfaces |
||||
{ |
{ |
||||
public interface IBasketService |
public interface IBasketService |
||||
{ |
{ |
||||
Task<Basket> GetBasket(ApplicationUser user); |
Task<Basket> GetBasket(string basketId); |
||||
} |
Task<Basket> CreateBasket(); |
||||
|
Task<Basket> CreateBasketForUser(string userId); |
||||
public interface IIdentityParser<T> |
Task UpdateBasket(Basket basket); |
||||
{ |
|
||||
T Parse(IPrincipal principal); |
|
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Security.Principal; |
||||
|
|
||||
|
namespace ApplicationCore.Interfaces |
||||
|
{ |
||||
|
public interface IIdentityParser<T> |
||||
|
{ |
||||
|
T Parse(IPrincipal principal); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; |
||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
|
||||
|
|
||||
|
namespace Infrastructure.Identity |
||||
|
{ |
||||
|
public class ApplicationUser : IdentityUser |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -1,52 +1,65 @@ |
|||||
using Microsoft.eShopWeb.Services; |
using Microsoft.AspNetCore.Mvc; |
||||
using Microsoft.AspNetCore.Mvc; |
|
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using ApplicationCore.Interfaces; |
using ApplicationCore.Interfaces; |
||||
using ApplicationCore.Entities; |
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities; |
using Microsoft.eShopWeb.ApplicationCore.Entities; |
||||
using System; |
using Microsoft.AspNetCore.Http; |
||||
|
|
||||
namespace Microsoft.eShopWeb.Controllers |
namespace Microsoft.eShopWeb.Controllers |
||||
{ |
{ |
||||
public class CartController : Controller |
public class CartController : Controller |
||||
{ |
{ |
||||
private readonly ICatalogService _catalogSvc; |
private readonly IBasketService _basketService; |
||||
private readonly IBasketService _basketSvc; |
//private readonly IIdentityParser<ApplicationUser> _appUserParser;
|
||||
private readonly IIdentityParser<ApplicationUser> _appUserParser; |
private const string _basketSessionKey = "basketId"; |
||||
|
|
||||
public CartController(IBasketService basketSvc, |
public CartController(IBasketService basketService) |
||||
IIdentityParser<ApplicationUser> appUserParser) |
// IIdentityParser<ApplicationUser> appUserParser)
|
||||
{ |
{ |
||||
//_catalogSvc = catalogSvc;
|
_basketService = basketService; |
||||
_basketSvc = basketSvc; |
// _appUserParser = appUserParser;
|
||||
_appUserParser = appUserParser; |
|
||||
} |
} |
||||
|
|
||||
|
|
||||
// GET: /<controller>/
|
// GET: /<controller>/
|
||||
public async Task<IActionResult> Index() |
public async Task<IActionResult> Index() |
||||
{ |
{ |
||||
var user = _appUserParser.Parse(HttpContext.User); |
//var user = _appUserParser.Parse(HttpContext.User);
|
||||
var viewmodel = await _basketSvc.GetBasket(user); |
var basket = await GetBasketFromSessionAsync(); |
||||
|
|
||||
return View(viewmodel); |
return View(basket); |
||||
} |
} |
||||
|
|
||||
// GET: /Cart/AddToCart
|
// GET: /Cart/AddToCart
|
||||
// TODO: This should be a POST.
|
// TODO: This should be a POST.
|
||||
public async Task<IActionResult> AddToCart(CatalogItem productDetails) |
public async Task<IActionResult> AddToCart(CatalogItem productDetails) |
||||
{ |
{ |
||||
var user = _appUserParser.Parse(HttpContext.User); |
if (productDetails?.Id == null) |
||||
var product = new BasketItem() |
|
||||
{ |
{ |
||||
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"); |
return RedirectToAction("Index", "Catalog"); |
||||
} |
} |
||||
|
var basket = await GetBasketFromSessionAsync(); |
||||
|
|
||||
|
basket.AddItem(productDetails.Id, productDetails.Price, 1); |
||||
|
|
||||
|
await _basketService.UpdateBasket(basket); |
||||
|
|
||||
|
return RedirectToAction("Index"); |
||||
|
} |
||||
|
|
||||
|
private async Task<Basket> GetBasketFromSessionAsync() |
||||
|
{ |
||||
|
string basketId = HttpContext.Session.GetString(_basketSessionKey); |
||||
|
Basket basket = null; |
||||
|
if (basketId == null) |
||||
|
{ |
||||
|
basket = await _basketService.CreateBasketForUser(User.Identity.Name); |
||||
|
HttpContext.Session.SetString(_basketSessionKey, basket.Id); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
basket = await _basketService.GetBasket(basketId); |
||||
|
} |
||||
|
return basket; |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,53 @@ |
|||||
|
using ApplicationCore.Interfaces; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities; |
||||
|
using Microsoft.eShopWeb.Infrastructure; |
||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
|
||||
|
namespace Web.Services |
||||
|
{ |
||||
|
public class BasketService : IBasketService |
||||
|
{ |
||||
|
private readonly CatalogContext _context; |
||||
|
|
||||
|
public BasketService(CatalogContext context) |
||||
|
{ |
||||
|
_context = context; |
||||
|
} |
||||
|
public async Task<Basket> GetBasket(string basketId) |
||||
|
{ |
||||
|
var basket = await _context.Baskets |
||||
|
.Include(b => b.Items) |
||||
|
.FirstOrDefaultAsync(b => b.Id == basketId); |
||||
|
if (basket == null) |
||||
|
{ |
||||
|
basket = new Basket(); |
||||
|
_context.Baskets.Add(basket); |
||||
|
await _context.SaveChangesAsync(); |
||||
|
} |
||||
|
return basket; |
||||
|
} |
||||
|
|
||||
|
public Task<Basket> CreateBasket() |
||||
|
{ |
||||
|
return CreateBasketForUser(null); |
||||
|
} |
||||
|
|
||||
|
public async Task<Basket> CreateBasketForUser(string userId) |
||||
|
{ |
||||
|
var basket = new Basket(); |
||||
|
_context.Baskets.Add(basket); |
||||
|
await _context.SaveChangesAsync(); |
||||
|
|
||||
|
return basket; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public async Task UpdateBasket(Basket basket) |
||||
|
{ |
||||
|
// only need to save changes here
|
||||
|
await _context.SaveChangesAsync(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
@using Microsoft.eShopWeb.ApplicationCore.Entities; |
||||
|
@{ |
||||
|
ViewData["Title"] = "Catalog"; |
||||
|
@model Basket |
||||
|
} |
||||
|
<section class="esh-catalog-hero"> |
||||
|
<div class="container"> |
||||
|
<img class="esh-catalog-title" src="../images/main_banner_text.png" /> |
||||
|
</div> |
||||
|
</section> |
||||
|
|
||||
|
<div class="container"> |
||||
|
|
||||
|
@if (Model.Items.Any()) |
||||
|
{ |
||||
|
<div class="esh-catalog-items row"> |
||||
|
@foreach (var item in Model.Items) |
||||
|
{ |
||||
|
<div class="esh-catalog-item col-md-4"> |
||||
|
@item.ProductId |
||||
|
</div> |
||||
|
} |
||||
|
</div> |
||||
|
|
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
<div class="esh-catalog-items row"> |
||||
|
Cart is empty. |
||||
|
</div> |
||||
|
} |
||||
|
</div> |
||||
Loading…
Reference in new issue