public bool SubmitOrder(string userName) { using (MoviesSiteDBEntities db = new MoviesSiteDBEntities()) { try { // Add new order record Order newOrder = new Order(); newOrder.CustomerName = userName; newOrder.OrderDate = DateTime.Now; db.Orders.Add(newOrder); db.SaveChanges(); // Create a new OrderDetails for each record string cartId = GetShoppingCartSession(); var myCart = (from c in db.ViewCarts where c.CartID == cartId select c); //.FirstOrDefault(); foreach (ViewCart vc in myCart.ToList()) { int i = 0; if (i < 1) { OrderDetail od = new OrderDetail(); od.OrderID = newOrder.OrderID; od.MovieID = vc.MovieID; od.Quantity = vc.Quantity; od.UnitCost = vc.MovieCost; od.DateTime = DateTime.Now; db.OrderDetails.Add(od); i++; } var myItem = (from c in db.ShoppingCarts where c.CartID == vc.CartID && c.MovieID == vc.MovieID select c).FirstOrDefault(); if (myItem != null) { db.ShoppingCarts.Remove(myItem); } db.SaveChanges(); } } catch (Exception ex) { throw new Exception ("Error: Unable to Submit order" + ex.Message.ToString(), ex); } } return(true); }
// Take old session(anonymous user) and migrate the info // into new session public void MigrateCart(string oldCartId, string newCartId) { using (MoviesSiteDBEntities db = new MoviesSiteDBEntities()) { try { var myShopCart = from cart in db.ShoppingCarts where cart.CartID == oldCartId select cart; foreach (ShoppingCart item in myShopCart) { item.CartID = newCartId; } db.SaveChanges(); Session[CartId] = newCartId; } catch (Exception ex) { throw new Exception ("Error: Unable to Migrate Shopping Cart" + ex.Message.ToString(), ex); } } }
public void UpdateItem (string cartId, int movieId, int quantity) { using (MoviesSiteDBEntities db = new MoviesSiteDBEntities()) { try { var myItem = (from c in db.ShoppingCarts where c.CartID == cartId && c.MovieID == movieId select c).FirstOrDefault(); if (myItem != null) { myItem.Quantity = quantity; db.SaveChanges(); } } catch (Exception ex) { throw new Exception ("Error: Unable to Update item" + ex.Message.ToString(), ex); } } }
public void AddItem (string cartId, int movieId, int quantity) { // Using Entity Framework using (MoviesSiteDBEntities db = new MoviesSiteDBEntities()) { try { // This query will check to add // movie id based on cartid // in any case we will used FirstOrDefault() // method that will return the first element // of a squence or the default value if the // sequence contains no elements var myItem = (from c in db.ShoppingCarts where c.CartID == cartId && c.MovieID == movieId select c).FirstOrDefault(); if (myItem == null) { ShoppingCart cartAdd = new ShoppingCart(); cartAdd.CartID = cartId; cartAdd.Quantity = quantity; cartAdd.MovieID = movieId; cartAdd.DateCreated = DateTime.Now; cartAdd.DateTime = DateTime.Now; db.ShoppingCarts.Add(cartAdd); } else { myItem.Quantity += quantity; } db.SaveChanges(); } catch (Exception ex) { throw new Exception("Error: Unable to " + " add movie to cart" + ex.Message.ToString()); } } }