Skip to content

Commit

Permalink
Section 9 - Load Shopping Cart List in Get Action
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 25, 2021
1 parent a671342 commit 6899663
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion BulkyBook.DataAccess/Repository/IRepository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IRepository<T> where T : class
{
//T - Category
T GetFirstOrDefault(Expression<Func<T, bool>> filter, string? includeProperties = null);
IEnumerable<T> GetAll(string? includeProperties = null);
IEnumerable<T> GetAll(Expression<Func<T, bool>> filter, string? includeProperties = null);
void Add(T entity);
void Remove(T entity);
void RemoveRange(IEnumerable<T> entity);
Expand Down
5 changes: 3 additions & 2 deletions BulkyBook.DataAccess/Repository/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ public class Repository<T> : IRepository<T> where T : class
public Repository(ApplicationDbContext db)
{
_db= db;
//_db.Products.Include(u => u.Category).Include(u=>u.CoverType);
//_db.ShoppingCarts.Include(u => u.Product).Include(u=>u.CoverType);
this.dbSet= _db.Set<T>();
}
public void Add(T entity)
{
dbSet.Add(entity);
}
//includeProp - "Category,CoverType"
public IEnumerable<T> GetAll(string? includeProperties = null)
public IEnumerable<T> GetAll(Expression<Func<T, bool>> filter, string? includeProperties = null)
{
IQueryable<T> query = dbSet;
query = query.Where(filter);
if (includeProperties != null)
{
foreach(var includeProp in includeProperties.Split(new char[] { ','}, StringSplitOptions.RemoveEmptyEntries))
Expand Down
13 changes: 13 additions & 0 deletions BulkyBook.Models/ViewModels/ShoppingCartVM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BulkyBook.Models.ViewModels
{
public class ShoppingCartVM
{
public IEnumerable<ShoppingCart> ListCart { get; set; }
}
}
24 changes: 22 additions & 2 deletions BulkyBookWeb/Areas/Customer/Controllers/CartController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
using Microsoft.AspNetCore.Mvc;
using BulkyBook.DataAccess.Repository.IRepository;
using BulkyBook.Models.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace BulkyBookWeb.Areas.Customer.Controllers
{
[Area("Customer")]
[Authorize]
public class CartController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public ShoppingCartVM ShoppingCartVM { get; set; }
public CartController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
return View();
var claimsIdentity = (ClaimsIdentity)User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

ShoppingCartVM = new ShoppingCartVM()
{
ListCart = _unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId == claim.Value,
includeProperties: "Product")
};

return View(ShoppingCartVM);
}
}
}

0 comments on commit 6899663

Please sign in to comment.