Skip to content

Commit

Permalink
Section 10 - Summary POST Action Method
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 26, 2021
1 parent d5ce8d0 commit 8d9242c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions BulkyBook.Utility/SD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,17 @@ public static class SD
public const string Role_User_Comp = "Company";
public const string Role_Admin = "Admin";
public const string Role_Employee = "Employee";

public const string StatusPending = "Pending";
public const string StatusApproved = "Approved";
public const string StatusInProcess = "Processing";
public const string StatusShipped = "Shipped";
public const string StatusCancelled = "Cancelled";
public const string StatusRefunded = "Refunded";

public const string PaymentStatusPending = "Pending";
public const string PaymentStatusApproved = "Approved";
public const string PaymentStatusDelayedPayment = "ApprovedForDelayedPayment";
public const string PaymentStatusRejected = "Rejected";
}
}
47 changes: 47 additions & 0 deletions BulkyBookWeb/Areas/Customer/Controllers/CartController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using BulkyBook.DataAccess.Repository.IRepository;
using BulkyBook.Models;
using BulkyBook.Models.ViewModels;
using BulkyBook.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
Expand All @@ -11,6 +13,7 @@ namespace BulkyBookWeb.Areas.Customer.Controllers
public class CartController : Controller
{
private readonly IUnitOfWork _unitOfWork;
[BindProperty]
public ShoppingCartVM ShoppingCartVM { get; set; }
public CartController(IUnitOfWork unitOfWork)
{
Expand Down Expand Up @@ -68,6 +71,50 @@ public IActionResult Summary()
return View(ShoppingCartVM);
}

[HttpPost]
[ActionName("Summary")]
[ValidateAntiForgeryToken]
public IActionResult SummaryPOST()
{
var claimsIdentity = (ClaimsIdentity)User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

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

ShoppingCartVM.OrderHeader.PaymentStatus = SD.PaymentStatusPending;
ShoppingCartVM.OrderHeader.OrderStatus = SD.StatusPending;
ShoppingCartVM.OrderHeader.OrderDate = System.DateTime.Now;
ShoppingCartVM.OrderHeader.ApplicationUserId = claim.Value;


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);
}


_unitOfWork.OrderHeader.Add(ShoppingCartVM.OrderHeader);
_unitOfWork.Save();
foreach (var cart in ShoppingCartVM.ListCart)
{
OrderDetail orderDetail = new()
{
ProductId = cart.ProductId,
OrderId = ShoppingCartVM.OrderHeader.Id,
Price = cart.Price,
Count = cart.Count
};
_unitOfWork.OrderDetail.Add(orderDetail);
_unitOfWork.Save();
}

_unitOfWork.ShoppingCart.RemoveRange(ShoppingCartVM.ListCart);
_unitOfWork.Save();
return RedirectToAction("Index","Home");
}

public IActionResult Plus(int cartId)
{
Expand Down

0 comments on commit 8d9242c

Please sign in to comment.