Skip to content

Commit

Permalink
Section 12 - View Component Code
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 27, 2021
1 parent 0e55e66 commit 234b72e
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions BulkyBookWeb/ViewComponents/ShoppingCartViewComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using BulkyBook.DataAccess.Repository.IRepository;
using BulkyBook.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace BulkyBookWeb.ViewComponents
{
public class ShoppingCartViewComponent : ViewComponent
{
private readonly IUnitOfWork _unitOfWork;
public ShoppingCartViewComponent(IUnitOfWork unitOfWork)
{
_unitOfWork= unitOfWork;
}

public async Task<IViewComponentResult> InvokeAsync()
{
var claimsIdentity = (ClaimsIdentity)User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
if (claim != null)
{
if (HttpContext.Session.GetInt32(SD.SessionCart) != null)
{
return View(HttpContext.Session.GetInt32(SD.SessionCart));
}
else
{
HttpContext.Session.SetInt32(SD.SessionCart,
_unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId == claim.Value).ToList().Count);
return View(HttpContext.Session.GetInt32(SD.SessionCart));
}
}
else
{
HttpContext.Session.Clear();
return View(0);
}
}
}
}

0 comments on commit 234b72e

Please sign in to comment.