/MvcMusicStore/Controllers/CheckoutController.cs
# · C# · 83 lines · 60 code · 13 blank · 10 comment · 5 complexity · dfc295a35fae1c258a1766b6e001160a MD5 · raw file
- using System;
- using System.Linq;
- using System.Web.Mvc;
- using MvcMusicStore.Models;
-
- namespace MvcMusicStore.Controllers
- {
- [Authorize]
- public class CheckoutController : Controller
- {
- MusicStoreEntities storeDB = new MusicStoreEntities();
- const string PromoCode = "FREE";
-
- //
- // GET: /Checkout/AddressAndPayment
-
- public ActionResult AddressAndPayment()
- {
- return View();
- }
-
- //
- // POST: /Checkout/AddressAndPayment
-
- [HttpPost]
- public ActionResult AddressAndPayment(FormCollection values)
- {
- var order = new Order();
- TryUpdateModel(order);
-
- try
- {
- if (string.Equals(values["PromoCode"], PromoCode,
- StringComparison.OrdinalIgnoreCase) == false)
- {
- return View(order);
- }
- else
- {
- order.Username = User.Identity.Name;
- order.OrderDate = DateTime.Now;
-
- //Save Order
- storeDB.Orders.Add(order);
- storeDB.SaveChanges();
-
- //Process the order
- var cart = ShoppingCart.GetCart(this.HttpContext);
- cart.CreateOrder(order);
-
- return RedirectToAction("Complete",
- new { id = order.OrderId });
- }
-
- }
- catch
- {
- //Invalid - redisplay with errors
- return View(order);
- }
- }
-
- //
- // GET: /Checkout/Complete
-
- public ActionResult Complete(int id)
- {
- // Validate customer owns this order
- bool isValid = storeDB.Orders.Any(
- o => o.OrderId == id &&
- o.Username == User.Identity.Name);
-
- if (isValid)
- {
- return View(id);
- }
- else
- {
- return View("Error");
- }
- }
- }
- }