public void addListBook(string date) { string sql = "Select * from PhieuMuonSach"; int idPhieuMuon = SingletonDataProvider.Ins.DataBase.truyvan(sql).Rows.Count + 1; string sql2 = "Select * from PhieuMuon_Sach"; int id = SingletonDataProvider.Ins.DataBase.truyvan(sql2).Rows.Count + 1; var listBook = CartBook.GetList(); decimal totalMoney = 0; for (int i = 0; i < listBook.Count(); i++) { totalMoney += listBook[i].Gia; } string sql3 = "Insert Into PhieuMuonSach Values (" + idPhieuMuon + "," + User.getUserID() + ",null,'" + DateTime.UtcNow.Date.ToString().Split()[0] + "'," + "'binh thuong','" + date + "'," + totalMoney + " )"; SingletonDataProvider.Ins.DataBase.truyvan(sql3); for (int i = 0; i < listBook.Count(); i++) { string sql4 = "Insert Into PhieuMuon_Sach Values (" + id + "," + idPhieuMuon + "," + listBook[i].idSach + " )"; id++; SingletonDataProvider.Ins.DataBase.truyvan(sql4); } }
private void AddNewBookToCart(Cart cart, CartBook cartBook) { if (cart.CartBooks.FirstOrDefault(x => x.BookId == cartBook.BookId) == null) { cart.CartBooks.Add(cartBook); _cartService.UpdateCart(cart); } }
public void RemoveBookFormUserCart(int id) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { CartBook cartBooks = _context.CartBooks.Where(cb => cb.BookID == id).SingleOrDefault(); _context.CartBooks.Remove(cartBooks); _context.SaveChanges(); } }
private Cart CreateNewCartWithBook(string userId, CartBook cartBook) { var newCart = new Cart() { UserId = userId, CartBooks = new Collection <CartBook>() { cartBook } }; _cartService.InsertCart(newCart); return(newCart); }
/// <summary> /// AddToCartAsync /// </summary> /// <param name="bookId"></param> /// <param name="userId"></param> /// <returns></returns> public async Task AddToCartAsync(int bookId, string userId) { var cartBook = await _context.CartBooks.FirstOrDefaultAsync(x => x.BookId == bookId && x.UserId == userId); if (cartBook != null) { cartBook.Count++; } else { cartBook = new CartBook { Count = 1, BookId = bookId, UserId = userId }; await _context.CartBooks.AddAsync(cartBook); } var result = await _context.SaveChangesAsync(); }
private void Button_Click_1(object sender, RoutedEventArgs e) { Button button = sender as Button; var book = (DataRowView)button.DataContext; //MessageBox.Show(int.Parse(book.Row[0].ToString()) + book.Row[1].ToString() + int.Parse(book.Row[2].ToString()) + int.Parse(book.Row[3].ToString()) +decimal.Parse(book.Row[4].ToString())+ int.Parse(book.Row[5].ToString()) + book.Row[6].ToString() + book.Row[7].ToString()); BookMgr bookmgr = new BookMgr(int.Parse(book.Row[0].ToString()), book.Row[1].ToString(), int.Parse(book.Row[2].ToString()), int.Parse(book.Row[3].ToString()), decimal.Parse(book.Row[4].ToString()), int.Parse(book.Row[5].ToString()), book.Row[6].ToString(), book.Row[7].ToString()); if (CartBook.GetList().Count() == Regulations.SoLuongSachToiDa) { MessageBox.Show("Số Sách Mượn Đã Tối Đa"); return; } CartBook.AddBook(bookmgr); //DataRowView librarian = ListBook.SelectedItem as DataRowView; //DataRowView librarian = ListBook.SelectedItem as DataRowView; // MessageBox.Show(book.Row[0].ToString()); //Game game = button.DataContext as Game; //int id = game.ID; }
public async Task <Response> InsertToCart(Cart cart, Book book, int quantity) { try { var originalID = book.Id.Length < 18 ? book.Id : SecureHelper.GetOriginalInput(book.Id); var checkCartBook = await context.CartBook .Where(x => x.BookId.Equals(originalID) && x.CartId.Equals(cart.Id)) .FirstOrDefaultAsync(); if (checkCartBook is null) { var cartBook = new CartBook { BookId = book.Id.Length < 18 ? book.Id : SecureHelper.GetOriginalInput(book.Id), CartId = cart.Id, PickedDate = DateTime.Now, Quantity = quantity, SubTotal = book.CurrentPrice }; context.CartBook.Add(cartBook); await context.SaveChangesAsync(); return(new Response("Success", true, 1, cartBook)); } else { checkCartBook.Quantity += quantity; checkCartBook.SubTotal += book.CurrentPrice * quantity; context.CartBook.Update(checkCartBook); await context.SaveChangesAsync(); return(new Response("Success", true, 1, checkCartBook)); } } catch (Exception e) { return(Response.CatchError(e.Message)); } }
public bool CheckBookInUserCart(int userID, int bookID) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { Cart cart = GetCartNotInOrder(userID); //Order order = _context.Orders.Where(o => o.CartID == cart.CartID).SingleOrDefault(); if (cart == null) { return(false); // Book is not exist in User Cart } else { int cartID = cart.CartID; CartBook cartBook = _context.CartBooks.Where(cb => cb.CartID == cartID && cb.BookID == bookID).SingleOrDefault(); if (cartBook == null) { return(false); // Book is not exist in User Cart } return(true); // Book is already added in User Cart } } }
public borrowBook() { InitializeComponent(); txtUserName.Content = User.getUserName(); dateTimePicker1.Text = DateTime.Today.ToString(); listBook = CartBook.GetList(); ListBook.ItemsSource = listBook; //for(int i=0;i<CartBook.GetList().Count();i++) //{ // //bookDAO.FindBookbyId(CartBook.getList()[i]); //} //MessageBox.Show(listId.ToString()); for (int i = 0; i < listBook.Count(); i++) { totalMoney += listBook[i].Gia; } txtTotalMoney.Content = totalMoney.ToString() + " đ"; }
private void RemoveBookFromCart(Cart cart, CartBook cartBook) { cart.CartBooks.Remove(cartBook); _cartService.UpdateCart(cart); }
private Cart CreateNewCartWithBook(string userId, CartBook cartBook) { var newCart = new Cart() { UserId = userId, CartBooks = new Collection<CartBook>() { cartBook } }; _cartService.InsertCart(newCart); return newCart; }
// Such a complex coding - to check the cart exist in the orders table and which cart is not in the order then the book is added in the cart // if the user dont have any cart then it create a new cart and add the book // if the user has only one cart in orders then it creates new cart and add the book in the cart public int AddToCart(int userID, int bookID) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { List <Cart> user_carts = _context.Carts.Where(c => c.UserID == userID).ToList(); Cart _cart = new Cart(); int no_of_carts = user_carts.Count(); if (no_of_carts == 2) // When User Has Two Carts Probably One In Orders Another One For New Add To Carts { Cart cartOne = user_carts.ElementAt(0); Cart cartTwo = user_carts.ElementAt(1); if (cartOne.IsOrdered == false) // Check Wheather The First One Is Not In Orders To Add The New Book { CartBook cartbooks = new CartBook() { CartID = cartOne.CartID, BookID = bookID, }; // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); cartOne.CartRate = cartOne.CartRate + book.Price; _context.CartBooks.Add(cartbooks); _context.SaveChanges(); return(cartOne.CartID); } else if (cartTwo.IsOrdered == false) //Or Else Check Wheather The First Ssecond Is Not In Orders To Add The New Book { CartBook cartbooks = new CartBook() { CartID = cartTwo.CartID, BookID = bookID, }; // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); cartOne.CartRate = cartOne.CartRate + book.Price; _context.CartBooks.Add(cartbooks); _context.SaveChanges(); return(cartTwo.CartID); } } else if (no_of_carts == 1) // When The User Has One Cart That May Be In Orders Or In Carts To Add New Books { Cart cart = user_carts.ElementAt(0); if (cart.IsOrdered == false) // Check If the Cart Is Not Added Into the Orders { CartBook cartbooks = new CartBook() { CartID = cart.CartID, BookID = bookID, }; // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); cart.CartRate = cart.CartRate + book.Price; _context.CartBooks.Add(cartbooks); _context.SaveChanges(); return(cart.CartID); } else if (cart.IsOrdered != false)// Check If The Cart Is Already Added In To The Orders { // Create A New Cart For The User Cart new_cart = new Cart() { UserID = userID, }; _context.Carts.Add(new_cart); _context.SaveChanges(); _cart = GetCartNotInOrder(userID); if (_cart != null) { CartBook cartbooks = new CartBook() { CartID = _cart.CartID, BookID = bookID, }; // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); _cart.CartRate = _cart.CartRate + book.Price; _context.CartBooks.Add(cartbooks); _context.SaveChanges(); return(_cart.CartID); } } } else if (no_of_carts == 0) // Wheather The User Has No Carts { // Create A New Cart For The User Cart cart = new Cart() { UserID = userID, }; _context.Carts.Add(cart); _context.SaveChanges(); cart = GetCartNotInOrder(userID); using (var dbContextTransaction = _context.Database.BeginTransaction()) { CartBook cartbook = new CartBook() { CartID = cart.CartID, BookID = bookID, }; _context.CartBooks.Add(cartbook); // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); cart.CartRate = book.Price + cart.CartRate; _context.SaveChanges(); dbContextTransaction.Commit(); return(cart.CartID); } } //foreach (Cart cart in user_carts) //{ // // Order order = _context.Orders.Where(o => o.CartID == cart.CartID).SingleOrDefault(); // if (cart.IsOrdered == false) // The Current Cart Is Not In The Orders Table // { // CartBook cartbooks = new CartBook() // { // CartID = cart.CartID, // BookID = bookID, // }; // // Get Book Into Object And Add The Book Rate Into The Cart Rate // Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); // cart.CartRate = book.Price + cart.CartRate; // _context.CartBooks.Add(cartbooks); // _context.SaveChanges(); // return cart.CartID; // } // else if (cart.IsOrdered != false) // The Cart Is Already Added In The Orders // { // Cart new_cart = new Cart() // { // UserID = userID, // }; // _context.Carts.Add(new_cart); // _context.SaveChanges(); // _cart = GetCartNotInOrder(userID); // if (_cart!=null) // { // CartBook cartbooks = new CartBook() // { // CartID = _cart.CartID, // BookID = bookID, // }; // _context.CartBooks.Add(cartbooks); // _context.SaveChanges(); // return cart.CartID; // } // } //} //if (user_carts.Count==0) //{ // Cart cart = new Cart() // { // UserID = userID, // }; // _context.Carts.Add(cart); // _context.SaveChanges(); // _cart = GetCartNotInOrder(userID); // CartBook cartbook = new CartBook() // { // CartID = _cart.CartID, // BookID = bookID, // }; // _context.CartBooks.Add(cartbook); // _context.SaveChanges(); // return cart.CartID; //} return(0); } }
public void RemoveBook(CartBook b) { cartBooks.Remove(b); }
public void AddBook(Book b, int t, int q) { CartBook tempbook = new CartBook(b, t, q); cartBooks.Add(tempbook); }