public ActionResult Create([Bind(Include = "Id,Title,Price,AuthorId")] Book book) { if (ModelState.IsValid) { db.Books.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AuthorId = new SelectList(db.Authors, "Id", "Name", book.AuthorId); return(View(book)); }
public ActionResult Index(Models.Cart model) { var cart = db.Carts.Find(model.ID); for (int i = 0; i < model.Cart_Books.Count; i++) { cart.Cart_Books.ElementAt(i).Quantity = model.Cart_Books.ElementAt(i).Quantity; } db.Cart_Books.RemoveRange(cart.Cart_Books.Where(x => x.Quantity == 0)); db.SaveChanges(); return(View(cart)); }
public ActionResult Index(Book model) { //Save posted information to a database! Guid cartID; Cart cart = null; if (Request.Cookies.AllKeys.Contains("cartID")) { cartID = Guid.Parse(Request.Cookies["cartID"].Value); cart = db.Carts.Find(cartID); } if (cart == null) { cartID = Guid.NewGuid(); cart = new Cart { ID = cartID, DateCreated = DateTime.UtcNow, DateLastModified = DateTime.UtcNow }; db.Carts.Add(cart); Response.AppendCookie(new HttpCookie("cartID", cartID.ToString())); } Cart_Books book = cart.Cart_Books.FirstOrDefault(x => x.BookID == model.BookID); if (book == null) { book = new Cart_Books { DateCreated = DateTime.UtcNow, DateLastModified = DateTime.UtcNow, BookID = model.BookID, Quantity = 0 }; cart.Cart_Books.Add(book); } book.Quantity += model.Quantity ?? 1; book.DateLastModified = DateTime.UtcNow; cart.DateLastModified = DateTime.UtcNow; db.SaveChanges(); TempData.Add("NewItem", model.Title); //TODO: build up the cart controller! return(RedirectToAction("Index", "Cart")); }
protected void Page_Load(object sender, EventArgs e) { //Redirect unauthenticated user to the Default page. if (Session["studentNumber"] == null) { Response.Redirect("Default.aspx"); } using (BookStoreDBEntities entityContext = new BookStoreDBEntities()) { //Get the ordered book list of the authenticated user. String studentNumber = Session["studentNumber"].ToString(); Student students = (from student in entityContext.Students where student.StudentNum == studentNumber select student).FirstOrDefault <Student>(); String id = Request.Params["id"]; String action = Request.Params["action"]; //If the user clicked the delete link of a book, //delete the selected book from the user's order if (!String.IsNullOrEmpty(id) && action == "Delete") { Book books1 = (from book in entityContext.Books where book.BookID == id select book).FirstOrDefault <Book>(); /*if (books != null) * { * entityContext.Books.Remove(books); * entityContext.SaveChanges(); * }*/ if (students != null && books1 != null) { students.Books.Remove(books1); entityContext.SaveChanges(); } } //If the user clicked a book's title, show the description of the book (use ShowBookDescrition method) if (!String.IsNullOrEmpty(id) && action == "ShowDescription") { Book books = (from book in entityContext.Books where book.BookID == id select book).FirstOrDefault <Book>(); if (books != null) { ShowBookDescription(books); } } //Display the book list of the user's order (use ShowBooks method) if (students != null) { ShowBooks(students.Books.ToList()); } } }
public async Task <ActionResult> Index(Models.CheckoutDetails model, string addressId) { Guid cartID = Guid.Parse(Request.Cookies["CartID"].Value); model.CurrentCart = db.Carts.Find(cartID); model.Addresses = new Braintree.Address[0]; if (ModelState.IsValid) { string trackingNumber = Guid.NewGuid().ToString().Substring(0, 8); decimal tax = (model.CurrentCart.Cart_Books.Sum(x => x.Book.Price * x.Quantity) ?? 0) * .1025m; decimal subtotal = model.CurrentCart.Cart_Books.Sum(x => x.Book.Price * x.Quantity) ?? 0; decimal shipping = model.CurrentCart.Cart_Books.Sum(x => x.Quantity); decimal total = subtotal + tax + shipping; #region pay for order PAAPaymentService payments = new PAAPaymentService(); string email = User.Identity.IsAuthenticated ? User.Identity.Name : model.ContactEmail; string message = await payments.AuthorizeCard(email, total, tax, trackingNumber, addressId, model.CardholderName, model.CVV, model.CreditCardNumber, model.ExpirationMonth, model.ExpirationYear); #endregion #region save order if (string.IsNullOrEmpty(message)) { Order o = new Order { DateCreated = DateTime.UtcNow, DateLastModified = DateTime.UtcNow, TrackingNumber = trackingNumber, ShippingAndHandling = shipping, Tax = tax, SubTotal = subtotal, Email = model.ContactEmail, PurchaserName = model.ContactName, ShippingAddress1 = model.ShippingAddress, ShippingCity = model.ShippingCity, ShippingPostalCode = model.ShippingPostalCode, ShippingState = model.ShippingState }; db.Orders.Add(o); await db.SaveChangesAsync(); #endregion #region send email PAAEmailService emailService = new PAAEmailService(); await emailService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage { Subject = "Your receipt for order " + trackingNumber, Destination = model.ContactEmail, Body = "Thank you for shopping" }); #endregion #region Reset Cart Response.SetCookie(new HttpCookie("cartID") { Expires = DateTime.UtcNow }); db.Cart_Books.RemoveRange(model.CurrentCart.Cart_Books); db.Carts.Remove(model.CurrentCart); db.SaveChanges(); #endregion return(RedirectToAction("Index", "Receipt", new { id = trackingNumber })); } ModelState.AddModelError("CreditCardNumber", message); } return(View(model)); }