forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding cart/basket support and applicationuser (without EF requirement)
- Loading branch information
Showing
11 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Security.Claims; | ||
|
||
namespace ApplicationCore.Entities | ||
{ | ||
public class ApplicationUser : ClaimsIdentity | ||
{ | ||
public string UserId { get; set; } | ||
public string UserName { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
namespace Microsoft.eShopWeb.ApplicationCore.Entities | ||
{ | ||
public class BaseEntity | ||
public class BaseEntity<T> | ||
{ | ||
public int Id { get; set; } | ||
public T Id { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.eShopWeb.ApplicationCore.Entities | ||
{ | ||
public class Basket : BaseEntity<string> | ||
{ | ||
public string BuyerId { get; set; } | ||
public List<BasketItem> Items { get; set; } = new List<BasketItem>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Microsoft.eShopWeb.ApplicationCore.Entities | ||
{ | ||
public class BasketItem : BaseEntity<string> | ||
{ | ||
public int ProductId { get; set; } | ||
public decimal UnitPrice { get; set; } | ||
public int Quantity { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using ApplicationCore.Entities; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities; | ||
using System.Security.Principal; | ||
using System.Threading.Tasks; | ||
|
||
namespace ApplicationCore.Interfaces | ||
{ | ||
public interface IBasketService | ||
{ | ||
Task<Basket> GetBasket(ApplicationUser user); | ||
} | ||
|
||
public interface IIdentityParser<T> | ||
{ | ||
T Parse(IPrincipal principal); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Microsoft.eShopWeb.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading.Tasks; | ||
using ApplicationCore.Interfaces; | ||
using ApplicationCore.Entities; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities; | ||
using System; | ||
|
||
namespace Microsoft.eShopWeb.Controllers | ||
{ | ||
public class CartController : Controller | ||
{ | ||
private readonly ICatalogService _catalogSvc; | ||
private readonly IBasketService _basketSvc; | ||
private readonly IIdentityParser<ApplicationUser> _appUserParser; | ||
|
||
public CartController(IBasketService basketSvc, | ||
IIdentityParser<ApplicationUser> appUserParser) | ||
{ | ||
//_catalogSvc = catalogSvc; | ||
_basketSvc = basketSvc; | ||
_appUserParser = appUserParser; | ||
} | ||
|
||
|
||
// GET: /<controller>/ | ||
public async Task<IActionResult> Index() | ||
{ | ||
var user = _appUserParser.Parse(HttpContext.User); | ||
var viewmodel = await _basketSvc.GetBasket(user); | ||
|
||
return View(viewmodel); | ||
} | ||
|
||
public async Task<IActionResult> AddToCart(CatalogItem productDetails) | ||
{ | ||
if (productDetails.Id != null) | ||
{ | ||
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); | ||
} | ||
return RedirectToAction("Index", "Catalog"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters