/MvcMusicStore/Controllers/CheckoutController.cs
C# | 83 lines | 60 code | 13 blank | 10 comment | 5 complexity | dfc295a35fae1c258a1766b6e001160a MD5 | raw file
1using System; 2using System.Linq; 3using System.Web.Mvc; 4using MvcMusicStore.Models; 5 6namespace MvcMusicStore.Controllers 7{ 8 [Authorize] 9 public class CheckoutController : Controller 10 { 11 MusicStoreEntities storeDB = new MusicStoreEntities(); 12 const string PromoCode = "FREE"; 13 14 // 15 // GET: /Checkout/AddressAndPayment 16 17 public ActionResult AddressAndPayment() 18 { 19 return View(); 20 } 21 22 // 23 // POST: /Checkout/AddressAndPayment 24 25 [HttpPost] 26 public ActionResult AddressAndPayment(FormCollection values) 27 { 28 var order = new Order(); 29 TryUpdateModel(order); 30 31 try 32 { 33 if (string.Equals(values["PromoCode"], PromoCode, 34 StringComparison.OrdinalIgnoreCase) == false) 35 { 36 return View(order); 37 } 38 else 39 { 40 order.Username = User.Identity.Name; 41 order.OrderDate = DateTime.Now; 42 43 //Save Order 44 storeDB.Orders.Add(order); 45 storeDB.SaveChanges(); 46 47 //Process the order 48 var cart = ShoppingCart.GetCart(this.HttpContext); 49 cart.CreateOrder(order); 50 51 return RedirectToAction("Complete", 52 new { id = order.OrderId }); 53 } 54 55 } 56 catch 57 { 58 //Invalid - redisplay with errors 59 return View(order); 60 } 61 } 62 63 // 64 // GET: /Checkout/Complete 65 66 public ActionResult Complete(int id) 67 { 68 // Validate customer owns this order 69 bool isValid = storeDB.Orders.Any( 70 o => o.OrderId == id && 71 o.Username == User.Identity.Name); 72 73 if (isValid) 74 { 75 return View(id); 76 } 77 else 78 { 79 return View("Error"); 80 } 81 } 82 } 83}