public void UpdateShoppingCartDatabase(String cartId, ShoppingCartUpdates[] CartItemUpdates) { using (var db = new BCM.DAL.ApplicationDbContext()) { try { int CartItemCount = CartItemUpdates.Count(); List<CartItem> myCart = GetCartItems(); foreach (var cartItem in myCart) { // Iterate through all rows within shopping cart list for (int i = 0; i < CartItemCount; i++) { if (cartItem.Book.ID == CartItemUpdates[i].BookId) { if (CartItemUpdates[i].PurchaseQuantity < 1 || CartItemUpdates[i].RemoveItem == true) { RemoveItem(cartId, cartItem.Book.ID); } else { UpdateItem(cartId, cartItem.Book.ID, CartItemUpdates[i].PurchaseQuantity); } } } } } catch (Exception exp) { throw new Exception("ERROR: Unable to Update Cart Database - " + exp.Message.ToString(), exp); } } }
public void Dispose() { if (_db != null) { _db.Dispose(); _db = null; } }
public void UpdateItem(string updateCartID, int updateProductID, int quantity) { using (var _db = new BCM.DAL.ApplicationDbContext()) { try { var myItem = (from c in _db.ShoppingCartItems where c.CartId == updateCartID && c.Book.ID == updateProductID select c).FirstOrDefault(); if (myItem != null) { myItem.Quantity = quantity; _db.SaveChanges(); } } catch (Exception exp) { throw new Exception("ERROR: Unable to Update Cart Item - " + exp.Message.ToString(), exp); } } }
public void RemoveItem(string removeCartID, int removeProductID) { using (var _db = new BCM.DAL.ApplicationDbContext()) { try { var myItem = (from c in _db.ShoppingCartItems where c.CartId == removeCartID && c.Book.ID == removeProductID select c).FirstOrDefault(); if (myItem != null) { // Remove Item. _db.ShoppingCartItems.Remove(myItem); _db.SaveChanges(); } } catch (Exception exp) { throw new Exception("ERROR: Unable to Remove Cart Item - " + exp.Message.ToString(), exp); } } }
/// <summary> /// Execute method /// </summary> /// <param name="connectionString">Microsoft Access connection string</param> public void Execute(string fullPath) { if (!File.Exists(fullPath)) { throw new FileNotFoundException("File not found!", fullPath); } OleDbConnection con = new OleDbConnection(); con.ConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", fullPath); try { con.Open(); OleDbCommand cmdCategories = new OleDbCommand(); OleDbCommand cmdBooks = new OleDbCommand(); cmdBooks.CommandText = "SELECT * FROM Book"; cmdBooks.Connection = con; Dictionary <int, Book> books = new Dictionary <int, Book>(); OleDbDataReader readerBooks = cmdBooks.ExecuteReader(); while (readerBooks.Read()) { Book book = new Book(); int id = readerBooks.GetInt32(0); books.Add(id, book); if (!String.IsNullOrEmpty(readerBooks["CopyrightYear"].ToString())) { book.CopyrightYear = int.Parse(readerBooks["CopyrightYear"].ToString()); } if (!String.IsNullOrEmpty(readerBooks["CoverType"].ToString())) { book.CoverType = readerBooks["CoverType"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["DatePurchased"].ToString())) { book.DatePurchased = DateTime.Parse(readerBooks["DatePurchased"].ToString()); } if (!String.IsNullOrEmpty(readerBooks["EditionNumber"].ToString())) { book.EditionNumber = int.Parse(readerBooks["EditionNumber"].ToString()); } if (!String.IsNullOrEmpty(readerBooks["ISBNNumber"].ToString())) { book.ISBNNumber = readerBooks["ISBNNumber"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["ListPrice"].ToString())) { book.ListPrice = decimal.Parse(readerBooks["ListPrice"].ToString()); } if (!String.IsNullOrEmpty(readerBooks["ShelfNumber"].ToString())) { book.Location = readerBooks["ShelfNumber"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["Notes"].ToString())) { book.Notes = readerBooks["Notes"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["Pages"].ToString())) { book.Pages = int.Parse(readerBooks["Pages"].ToString()); } if (!String.IsNullOrEmpty(readerBooks["PlaceOfPublication"].ToString())) { book.PlaceOfPublication = readerBooks["PlaceOfPublication"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["PublisherName"].ToString())) { book.PublisherName = readerBooks["PublisherName"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["PublishingCompany"].ToString())) { book.PublishingCompany = readerBooks["PublishingCompany"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["PublishingYear"].ToString())) { book.PublishingYear = int.Parse(readerBooks["PublishingYear"].ToString()); } if (!String.IsNullOrEmpty(readerBooks["PurchasePrice"].ToString())) { book.PurchasePrice = decimal.Parse(readerBooks["PurchasePrice"].ToString()); } if (!String.IsNullOrEmpty(readerBooks["Title"].ToString())) { book.Title = readerBooks["Title"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["VolumeNumber"].ToString())) { book.VolumeNumber = int.Parse(readerBooks["VolumeNumber"].ToString()); } if (!String.IsNullOrEmpty(readerBooks["ImageUrl"].ToString())) { book.ImageUrl = readerBooks["ImageUrl"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["ImageUrl2"].ToString())) { book.ImageUrl2 = readerBooks["ImageUrl2"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["ImageUrl3"].ToString())) { book.ImageUrl3 = readerBooks["ImageUrl3"].ToString(); } if (!String.IsNullOrEmpty(readerBooks["ImageUrl4"].ToString())) { book.ImageUrl4 = readerBooks["ImageUrl4"].ToString(); } } readerBooks.Close(); OleDbCommand cmdBookAuthor = new OleDbCommand(); cmdBookAuthor.CommandText = "SELECT Author.*" + " FROM Author INNER JOIN BookAuthor ON Author.AuthorID = BookAuthor.AuthorID" + " WHERE (((BookAuthor.BookID)=@p1));"; cmdBookAuthor.Parameters.Add("@p1", OleDbType.Integer); cmdBookAuthor.Connection = con; foreach (KeyValuePair <int, Book> kvp in books) { Book book = (kvp.Value) as Book; cmdBookAuthor.Parameters["@p1"].Value = kvp.Key.ToString(); OleDbDataReader readerBookAuthor = cmdBookAuthor.ExecuteReader(); if (readerBookAuthor.HasRows) { while (readerBookAuthor.Read()) { Author author = new Author(); if (!String.IsNullOrEmpty(readerBookAuthor["BirthDate"].ToString())) { author.BirthDate = DateTime.Parse(readerBookAuthor["BirthDate"].ToString()); } author.BirthName = String.Empty; if (!String.IsNullOrEmpty(readerBookAuthor["BirthPlace"].ToString())) { author.BirthPlace = readerBookAuthor["BirthPlace"].ToString(); } if (!String.IsNullOrEmpty(readerBookAuthor["DateOfDeath"].ToString())) { author.DateOfDeath = DateTime.Parse(readerBookAuthor["DateOfDeath"].ToString()); } if (!String.IsNullOrEmpty(readerBookAuthor["FirstName"].ToString())) { author.FirstName = readerBookAuthor["FirstName"].ToString(); } if (!String.IsNullOrEmpty(readerBookAuthor["LastName"].ToString())) { author.LastName = readerBookAuthor["LastName"].ToString(); } if (!String.IsNullOrEmpty(readerBookAuthor["Nationality"].ToString())) { author.Nationality = readerBookAuthor["Nationality"].ToString(); } if (!String.IsNullOrEmpty(readerBookAuthor["Notes"].ToString())) { author.Notes = readerBookAuthor["Notes"].ToString(); } author.Photograph = String.Empty; book.Authors.Add(author); } } readerBookAuthor.Close(); } BCM.DAL.ApplicationDbContext context = new BCM.DAL.ApplicationDbContext(); List <Category> categories = new List <Category> { new Category { Name = "Books", Description = String.Empty, ParentCategoryID = -1 }, new Category { Name = "Antiques & Collectibles", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Architecture", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Art", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Biography & Autobiography", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Computers & Internet", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Education", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Fiction", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Children's Fiction", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Humor", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "History", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Philosophy", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Music", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Science", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Travel", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Business & Economics", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Reference", Description = String.Empty, ParentCategoryID = 1 }, new Category { Name = "Non-Classifiable", Description = String.Empty, ParentCategoryID = 1 } }; categories.ForEach(c => context.Categories.Add(c)); context.SaveChanges(); Category parentCategory = categories.Find(c => c.Name.Equals("Antiques & Collectibles")); Category catMilitary = new Category { Name = "Military", Description = String.Empty, ParentCategoryID = parentCategory.ID }; context.Categories.Add(catMilitary); context.SaveChanges(); Category category = categories.Find(c => c.Name.Equals("Non-Classifiable")); foreach (KeyValuePair <int, Book> kvp in books) { Book book = ((Book)kvp.Value); if ((book.Title.StartsWith("Biennial report of THE CHIEF OF STAFF")) || (book.Title.StartsWith("Die Kämpfe der deutschen Truppen"))) { book.Categories.Add(catMilitary); } else { book.Categories.Add(category); } context.Books.Add(book); Console.WriteLine("{0}\t{1}", kvp.Key, book.Title); foreach (Author author in book.Authors) { Console.WriteLine("\t\t{0}\t{1}", author.LastName, author.FirstName); } } context.SaveChanges(); } catch (Exception e) { Console.WriteLine("Failed!\n{0}\n", e.Message); } finally { con.Close(); } }