9.3.5 Completing the Cart Controller; Listing 9-18 Completing the Controller in the CartController.cs File

chap09
Jason Zhu 2021-09-22 15:20:39 +10:00
parent b8a2c0043d
commit 23f23641e5
1 changed files with 24 additions and 1 deletions

View File

@ -12,10 +12,12 @@ namespace SportsStore.WebUI.Controllers
public class CartController : Controller
{
private IProductRepository repository;
private IOrderProcessor orderProcessor;
public CartController(IProductRepository repo)
public CartController(IProductRepository repo, IOrderProcessor proc)
{
repository = repo;
orderProcessor = proc;
}
public ViewResult Index(Cart cart, string returnUrl)
@ -57,9 +59,30 @@ namespace SportsStore.WebUI.Controllers
return PartialView(cart);
}
// GET
public ViewResult Checkout()
{
return View(new ShippingDetails());
}
// POST
[HttpPost]
public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
{
if (cart.Lines.Count() == 0)
{
ModelState.AddModelError("", "Sorry, your cart is empty");
}
if (ModelState.IsValid)
{
orderProcessor.ProcessOrder(cart, shippingDetails);
cart.Clear();
return View("Completed");
}
else
{
return View(shippingDetails);
}
}
}
}