public void AddToCart(string user, string isbn) { // Check if cart order exists in the database. var cartItem = booksmodel.Cart.FirstOrDefault( c => c.Username == user && c.ISBN == isbn && c.Status == "Unpaid"); if (cartItem == null) { // Create a new cart item if no cart item exists. cartItem = new Cart() { Username = user, Title = GetBook(isbn).Title, ISBN = isbn, Price = GetBookPrice(isbn), Quantity = 1, Status = "Unpaid", }; booksmodel.Cart.Add(cartItem); } else { // If the item does exist in the cart, // then add one to the quantity. cartItem.Quantity++; } booksmodel.SaveChanges(); }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int bookID = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text); Book books = new Book() { BookID = bookID }; bs.Book.Attach(books); bs.Book.Remove(books); bs.SaveChanges(); DateBind(); }
public bool ChangeStock(Cart c) { BookshopEntities bs = new BookshopEntities(); Book temp; try { string isbn = c.ISBN; temp = bs.Book.Where(x => x.ISBN == isbn).First(); } catch { return(false); } if (temp.Stock > 0) { temp.Stock -= 1; } else { return(false); } c.Status = "Removed"; bs.SaveChanges(); return(true); }
protected void Page_Load(object sender, EventArgs e) { user = Context.User.Identity.GetUserName(); var q = from x in new BusinessLogic().GetCartItems(user) select new { x.Title, x.ISBN, x.Price, x.Quantity }; GridView1.DataSource = q.ToList(); GridView1.DataBind(); Label_TotalPrice.Text = string.Format("Total Price is S$ {0}", new BusinessLogic().TotalPrice(user).ToString()); using (BookshopEntities context = new BookshopEntities()) { var cart = context.Cart.Where(x => x.Status == "Unpaid").ToList(); cart.ForEach(s => s.Status = "Paid"); context.SaveChanges(); } }