Skip to content

Commit

Permalink
Adding cart/basket support and applicationuser (without EF requirement)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Apr 18, 2017
1 parent c1d19a0 commit e019654
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/ApplicationCore/ApplicationCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Interfaces\" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions src/ApplicationCore/Entities/ApplicationUser.cs
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; }
}
}
4 changes: 2 additions & 2 deletions src/ApplicationCore/Entities/BaseEntity.cs
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; }
}
}
10 changes: 10 additions & 0 deletions src/ApplicationCore/Entities/Basket.cs
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>();
}
}
9 changes: 9 additions & 0 deletions src/ApplicationCore/Entities/BasketItem.cs
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; }
}
}
2 changes: 1 addition & 1 deletion src/ApplicationCore/Entities/CatalogBrand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogBrand : BaseEntity
public class CatalogBrand : BaseEntity<int>
{
public string Brand { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationCore/Entities/CatalogItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogItem : BaseEntity
public class CatalogItem : BaseEntity<int>
{
public string Name { get; set; }
public string Description { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationCore/Entities/CatalogType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogType : BaseEntity
public class CatalogType : BaseEntity<int>
{
public string Type { get; set; }
}
Expand Down
18 changes: 18 additions & 0 deletions src/ApplicationCore/Interfaces/IBasketService.cs
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);
}

}
53 changes: 53 additions & 0 deletions src/Web/Controllers/CartController.cs
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");
}

}
}
6 changes: 1 addition & 5 deletions src/Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace Microsoft.eShopWeb
Expand Down

0 comments on commit e019654

Please sign in to comment.