9.3.5 Completing the Cart Controller; Listing 9-18 Completing the Controller in the CartController.cs File
parent
b8a2c0043d
commit
23f23641e5
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue