public static void AddBookComplex(string title, List<string> authors, string isbn, string price, string site, List<Review> reviewsList) { TransactionScope tran = new TransactionScope(); using (tran) { BookstoreDBEntities context = new BookstoreDBEntities(); Book curBook = new Book(); curBook.Title = title; if (authors.Count > 0) { foreach (var author in authors) { curBook.Authors.Add(CreateOrLoadAuthor(context, author)); } } curBook.ISBN = isbn; curBook.Price = Convert.ToDecimal(price); curBook.WebSite = site; if (reviewsList.Count > 0) { foreach (var review in reviewsList) { curBook.Reviews.Add(review); } } context.Books.Add(curBook); context.SaveChanges(); tran.Complete(); } }
public static void AddBookComplex(List<string> authors, string title, string isbn, string price, string website) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); foreach (var author in authors) { newBook.Authors.Add(CreateOrLoadAuthor(context, author)); } newBook.Title = title.Trim(); if (isbn != null) { newBook.ISBN = long.Parse(isbn.Trim()); } if (price != null) { newBook.Price = decimal.Parse(price.Trim()); } context.Books.Add(newBook); try { context.SaveChanges(); } catch (DbEntityValidationException e) { } }
public List<Book> FindBooksByTitleAuthorAndISBN(BookstoreEntities context, BookstoreDAL bookstoreDAL, string bookTitle, string authorName, string ISBN) { if (string.IsNullOrEmpty(bookTitle) && string.IsNullOrEmpty(authorName) && string.IsNullOrEmpty(ISBN)) { return new List<Book>(); } IQueryable<Book> booksQuery = context.Books.Include("Authors"); Book book = new Book(); if (!string.IsNullOrEmpty(bookTitle)) { booksQuery = booksQuery.Where( b => b.Title.ToLower() == bookTitle.ToLower()); } if (!string.IsNullOrEmpty(authorName)) { booksQuery = booksQuery.Where( b => b.Authors.Any(a => a.Name.ToLower() == authorName.ToLower())); } if (!string.IsNullOrEmpty(ISBN)) { booksQuery = booksQuery.Where(b => b.ISBN == ISBN); } booksQuery = booksQuery.OrderBy(b => b.Title); return booksQuery.ToList(); }
// Task 4 public void ComplexImportOfBook( IList<string> authorsNames, string title, IList<ReviewTransferObject> reviewTransferObjects, string isbn, decimal? price, string webSite) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); newBook.ISBN = isbn; newBook.Title = title; newBook.Price = price; newBook.WebSite = webSite; foreach (string authorName in authorsNames) { Author author = CreateOrLoadAuthor(context, authorName); newBook.Authors.Add(author); } foreach (ReviewTransferObject reviewTransferObject in reviewTransferObjects) { Review review = SerializeToReview(context, reviewTransferObject); newBook.Reviews.Add(review); } context.Books.Add(newBook); context.SaveChanges(); }
public int FindBookReviewCount(BookstoreEntities context, BookstoreDAL bookstoreDAL, Book book) { int bookReviewCount = context.Reviews.Include("Book").Where(r => r.BookId == book.Id).Count(); return bookReviewCount; }
public static void AddToDatabase(string author, string title, string isbn, string price, string website) { using (BookstoreDBEntities bookstoreContext = new BookstoreDBEntities()) { Book book = new Book(); var authorToCheck = CreateOrLoadAuthor(bookstoreContext, author); book.Authors.Add(authorToCheck); book.Title = title; book.ISBN = isbn; if (price == null) { book.Price = null; } else { var convertedPrice = decimal.Parse(price, System.Globalization.CultureInfo.InvariantCulture); book.Price = convertedPrice; } book.URL = website; bookstoreContext.Books.Add(book); bookstoreContext.SaveChanges(); } }
public static void AddBookWithReview(List<string> authorNames, string title, string isbn, string price, string website, List<Review> reviewList, BookstoreEntities context) { Book newBook = new Book(); foreach (var author in authorNames) { newBook.Authors.Add(CreateOrLoadAuthor(author.Trim(), context)); } newBook.Title = title.Trim(); if (isbn != null) { newBook.ISBN = isbn.Trim(); } if (price != null) { newBook.Price = decimal.Parse(price); } if (website != null) { newBook.WebSite = website.Trim(); } foreach (var review in reviewList) { newBook.Reviews.Add(review); } context.Books.Add(newBook); context.SaveChanges(); }
public static void AddBookstore(string author, string title, long? isbn, decimal? price, string webSite) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); newBook.Author = CreateOrLoadAuthor(context, author); newBook.Title = title; newBook.ISBN = isbn; newBook.Price = price; newBook.WebSite = webSite; context.Books.Add(newBook); context.SaveChanges(); }
// Tasks 3 public void SimpleImportOfBook(string authorName, string title, string isbn, decimal? price, string webSite) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); Author author = CreateOrLoadAuthor(context, authorName); newBook.Authors.Add(author); newBook.ISBN = isbn; newBook.Price = price; newBook.Title = title; newBook.WebSite = webSite; context.Books.Add(newBook); context.SaveChanges(); }
public static Review AddReview(Book book, string reviewAuthor, DateTime reviewDate, string reviewContent) { BookstoreEntities context = new BookstoreEntities(); Review newReview = new Review(); newReview.BookId = book.BookId; if (reviewAuthor != null) { Author author = CreateOrLoadAuthor(context, reviewAuthor); newReview.Author = author; } newReview.Date = reviewDate; newReview.Content = reviewContent; context.Reviews.Add(newReview); context.SaveChanges(); return newReview; }
public static Book AddBook( string title, string authorName, decimal? price, string isbn, string url) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); newBook.Title = title; if (authorName != null) { Author author = CreateOrLoadAuthor(context, authorName); newBook.Authors.Add(author); } newBook.Price = price; newBook.ISBN = isbn; newBook.Url = url; context.Books.Add(newBook); context.SaveChanges(); return newBook; }
public static void ComplexBookAdd(string title, string isbn, string price, string webSiteURL, List<string> authors, List<Dictionary<string, string>> reviews) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); foreach (var author in authors) { Author bookAuthor = CreateOrLoadAuthor(context, author); newBook.Authors.Add(bookAuthor); } foreach (var review in reviews) { Review bookReview = new Review(); if (review["author"] != null && review["author"] != string.Empty) { Author reviewAuthor = CreateOrLoadAuthor(context, review["author"]); bookReview.Author = reviewAuthor; } bookReview.Text = review["text"]; var creationDate = DateTime.Now; if (review["creationDate"] != null && review["creationDate"] != string.Empty) { creationDate = DateTime.Parse(review["creationDate"]); } bookReview.CreationDate = creationDate; newBook.Reviews.Add(bookReview); } newBook.Title = title; newBook.WebSiteURL = webSiteURL; if (isbn != string.Empty && isbn != null) { newBook.ISBN = long.Parse(isbn); } if (price != string.Empty && price != null) { newBook.Price = decimal.Parse(price); } context.Books.Add(newBook); context.SaveChanges(); }
public Book AddBook(BookstoreEntities context, IList<string> authors, string title, string ISBN, decimal? price, string websiteUrl) { Book newBook = new Book(); newBook.Title = title; newBook.ISBN = ISBN; newBook.Price = price; newBook.Website = websiteUrl; foreach (var authorName in authors) { Author author = CreateOrLoadAuthor(context, authorName); newBook.Authors.Add(author); } context.Books.Add(newBook); context.SaveChanges(); return newBook; }
public static void AddSimpleBook(string title, string authorName, string isbn, string price, string website) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); newBook.Title = title; Author existingAuthor = CreateOrLoadAuthor(context, authorName); newBook.Authors.Add(existingAuthor); newBook.ISBN = isbn; if (price != null) { newBook.Price = decimal.Parse(price); } newBook.OfficialWebsite = website; context.Books.Add(newBook); context.SaveChanges(); }
public static Book AddBook(string title, string isbn, decimal? price, string webSite, IEnumerable<string> authors) { Book book = new Book(); book.Title = title; book.ISBN = isbn; book.Price = price; book.WebSite = webSite; BookstoreEntities context = new BookstoreEntities(); foreach (string authorName in authors) { Author author = CreateOrLoadAuthor(context, authorName); book.Authors.Add(author); } context.Books.Add(book); context.SaveChanges(); return book; }
public static void AddBook(string author, string title, string isbn, string price, string website, BookstoreEntities context) { Book newBook = new Book(); newBook.Authors.Add(CreateOrLoadAuthor(author.Trim(), context)); newBook.Title = title.Trim(); if (isbn != null) { newBook.ISBN = isbn.Trim(); } if (price != null) { newBook.Price = decimal.Parse(price); } if (website != null) { newBook.WebSite = website.Trim(); } context.Books.Add(newBook); context.SaveChanges(); }
public static void AddBook(string isbn, string price, string site, string author, string title) { TransactionScope tran = new TransactionScope(); using (tran) { BookstoreDBEntities context = new BookstoreDBEntities(); Book newBook = new Book(); newBook.ISBN = isbn; if (price != null) { newBook.Price = decimal.Parse(price); } newBook.WebSite = site; newBook.Title = title; newBook.Authors.Add(CreateOrLoadAuthor(context, author)); context.Books.Add(newBook); context.SaveChanges(); tran.Complete(); } }
public static void SimpleBookAdd(string title, string authorName, string isbn, string price, string webSiteURL) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); Author bookAuthor = CreateOrLoadAuthor(context, authorName); newBook.Authors.Add(bookAuthor); newBook.Title = title; newBook.WebSiteURL = webSiteURL; if (isbn != string.Empty && isbn != null) { newBook.ISBN = long.Parse(isbn); } if (price != string.Empty && price != null) { newBook.Price = decimal.Parse(price); } context.Books.Add(newBook); context.SaveChanges(); }
public static void AddComplexBook(string title, List<string> authors, List<Tuple<string, string, string>> reviews, string isbn, string price, string website) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); newBook.Title = title; if (authors.Count > 0) { foreach (var authorName in authors) { Author author = CreateOrLoadAuthor(context, authorName); newBook.Authors.Add(author); } } if (reviews.Count > 0) { foreach (var review in reviews) { Review newReview = new Review(); newReview.ReviewContent = review.Item1; if (review.Item2 != null) { newReview.Author = CreateOrLoadAuthor(context, review.Item2); } newReview.DateOfCreation = DateTime.Parse(review.Item3); newBook.Reviews.Add(newReview); } } if (isbn != null) { newBook.ISBN = isbn; } if (price != null) { newBook.Price = decimal.Parse(price); } if (website != null) { newBook.OfficialWebsite = website; } context.Books.Add(newBook); try { context.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } }
private static void ProcessReviewNode(BookstoreEntities context, BookstoreDAL bookstoreDAL, Book bookToAddReviewsTo, XmlNode reviewNode) { string reviewAuthorName = GetChildText(reviewNode, "@author"); string reviewCreationDateString = GetChildText(reviewNode, "@date"); string reviewText = reviewNode.InnerText.Trim(); if (string.IsNullOrEmpty(reviewText)) { throw new ArgumentException("Review text cannot be empty."); } DateTime reviewCreationDate = DateTime.Now; if (!string.IsNullOrEmpty(reviewCreationDateString)) { reviewCreationDate = DateTime.Parse(reviewCreationDateString); } Review newReview = new Review(); newReview.CreationDate = reviewCreationDate; newReview.Text = reviewText; newReview.Book = bookToAddReviewsTo; if (!string.IsNullOrEmpty(reviewAuthorName)) { newReview.Author = bookstoreDAL.CreateOrLoadAuthor(context, reviewAuthorName); } context.Reviews.Add(newReview); context.SaveChanges(); }
private static void ImportXML(string filename, bool complexData = false) { var import = new XmlDocument(); import.Load(filename); var books = import.DocumentElement.ChildNodes; foreach (XmlNode book in books) { var newBook = new Book(); if (book["title"] == null || book["title"].InnerText.Length == 0) { throw new ApplicationException(string.Format("Invalid {0} file - Book Title is missing", filename)); } newBook.Title = book["title"].InnerText; if (book["isbn"] != null) { newBook.ISBN = book["isbn"].InnerText; } if (book["price"] != null) { newBook.Price = decimal.Parse(book["price"].InnerText); } if (book["web-site"] != null) { newBook.WebSite = book["web-site"].InnerText; } using (var bookstoreContext = new BookstoreEntities()) { using (var scope = new System.Transactions.TransactionScope()) { if (complexData) { // read current book auhtors - no need to check since authors are not obligatory in a complex import if (book["authors"] != null) { foreach (XmlNode author in book["authors"].ChildNodes) { newBook.Authors.Add(GetSingleAuthor(bookstoreContext, author)); } } // read current book reviews if (book["reviews"] != null) { foreach (XmlNode review in book["reviews"].ChildNodes) { newBook.Reviews.Add(GetSingleReview(bookstoreContext, review)); } } } else { // there is only one author in a simple mode if (book["author"] == null) { throw new ApplicationException(string.Format("Invalid {0} file - Book Author is missing", filename)); } newBook.Authors.Add(GetSingleAuthor(bookstoreContext, book["author"])); } // Check if there is a book with the same ISBN - skip it if (bookstoreContext.Books.Where(b => b.ISBN == newBook.ISBN).Count() == 0) { bookstoreContext.Books.Add(newBook); } bookstoreContext.SaveChanges(); scope.Complete(); } } } }
public static void AddToDbComplex(XmlNodeList authors, string title, string isbn, string price, string website, List<ReviewInfo> reviews) { using (BookstoreDBEntities bookstoreContext = new BookstoreDBEntities()) { Book book = new Book(); foreach (XmlNode author in authors) { var authorToCheck = CreateOrLoadAuthor(bookstoreContext, author.InnerText); book.Authors.Add(authorToCheck); } book.Title = title; book.ISBN = isbn; if (price == null) { book.Price = null; } else { var convertedPrice = decimal.Parse(price, System.Globalization.CultureInfo.InvariantCulture); book.Price = convertedPrice; } book.URL = website; if (reviews != null) { foreach (var review in reviews) { Review currentReview = new Review(); if (review.Author == null) { currentReview.AuthorId = null; } else { var currentReviewAuthor = CreateOrLoadAuthor(bookstoreContext, review.Author); currentReview.AuthorId = currentReviewAuthor.AuthorId; } currentReview.Text = review.Text; //GetAuthorId(bookstoreContext, review.Author); if (review.CreatedOn == null) { currentReview.CreatedOn = DateTime.Now; } currentReview.CreatedOn = review.CreatedOn; book.Reviews.Add(currentReview); bookstoreContext.SaveChanges(); } } bookstoreContext.Books.Add(book); bookstoreContext.SaveChanges(); } }