Skip to content

Commit

Permalink
Section 10 - Summary GET Action Method
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 25, 2021
1 parent 72f7e9f commit 41fe1f3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
3 changes: 2 additions & 1 deletion BulkyBook.Models/ViewModels/ShoppingCartVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace BulkyBook.Models.ViewModels
public class ShoppingCartVM
{
public IEnumerable<ShoppingCart> ListCart { get; set; }
public double CartTotal { get; set; }

public OrderHeader OrderHeader { get; set; }
}
}
49 changes: 31 additions & 18 deletions BulkyBookWeb/Areas/Customer/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,48 @@ public IActionResult Index()
ShoppingCartVM = new ShoppingCartVM()
{
ListCart = _unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId == claim.Value,
includeProperties: "Product")
includeProperties: "Product"),
OrderHeader = new()
};
foreach(var cart in ShoppingCartVM.ListCart)
{
cart.Price = GetPriceBasedOnQuantity(cart.Count, cart.Product.Price,
cart.Product.Price50, cart.Product.Price100);
ShoppingCartVM.CartTotal += (cart.Price * cart.Count);
ShoppingCartVM.OrderHeader.OrderTotal += (cart.Price * cart.Count);
}
return View(ShoppingCartVM);
}

public IActionResult Summary()
{
//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")
//};
//foreach (var cart in ShoppingCartVM.ListCart)
//{
// cart.Price = GetPriceBasedOnQuantity(cart.Count, cart.Product.Price,
// cart.Product.Price50, cart.Product.Price100);
// ShoppingCartVM.CartTotal += (cart.Price * cart.Count);
//}
//return View(ShoppingCartVM);
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"),
OrderHeader= new()
};
ShoppingCartVM.OrderHeader.ApplicationUser = _unitOfWork.ApplicationUser.GetFirstOrDefault(
u => u.Id == claim.Value);

ShoppingCartVM.OrderHeader.Name = ShoppingCartVM.OrderHeader.ApplicationUser.Name;
ShoppingCartVM.OrderHeader.PhoneNumber = ShoppingCartVM.OrderHeader.ApplicationUser.PhoneNumber;
ShoppingCartVM.OrderHeader.StreetAddress = ShoppingCartVM.OrderHeader.ApplicationUser.StreetAddress;
ShoppingCartVM.OrderHeader.City = ShoppingCartVM.OrderHeader.ApplicationUser.City;
ShoppingCartVM.OrderHeader.State = ShoppingCartVM.OrderHeader.ApplicationUser.State;
ShoppingCartVM.OrderHeader.PostalCode = ShoppingCartVM.OrderHeader.ApplicationUser.PostalCode;



foreach (var cart in ShoppingCartVM.ListCart)
{
cart.Price = GetPriceBasedOnQuantity(cart.Count, cart.Product.Price,
cart.Product.Price50, cart.Product.Price100);
ShoppingCartVM.OrderHeader.OrderTotal += (cart.Price * cart.Count);
}
return View(ShoppingCartVM);
}


Expand Down
2 changes: 1 addition & 1 deletion BulkyBookWeb/Areas/Customer/Views/Cart/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between bg-light">
<span class="text-info"> Total (USD)</span>
<strong class="text-info">@Model.CartTotal.ToString("c")</strong>
<strong class="text-info">@Model.OrderHeader.OrderTotal.ToString("c")</strong>
</li>
</ul>
</div>
Expand Down

0 comments on commit 41fe1f3

Please sign in to comment.