public static void AddBook(string author, string title, string isbn, string price, string website) { BookstoreEntities context = new BookstoreEntities(); Book newBook = new Book(); 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 static void AddBooksReview(string title, string isbn, List <string> authors, decimal price, string website, List <string> reviews, List <string> reviewsDates, List <string> reviewsAuthors) { BookstoreEntities context = new BookstoreEntities(); Book book = new Book(); book.Title = title; book.ISBN = isbn; foreach (var author in authors) { Author newAuthor = CreateOrLoadAuthor(context, author); book.Authors.Add(newAuthor); } book.Price = price; book.Website = website; context.Books.Add(book); for (int i = 0; i < reviews.Count; i++) { Review review = CreateReview(context, book, reviews[i], reviewsDates[i], reviewsAuthors[i]); context.Reviews.Add(review); } context.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(); }
// 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 static Author CreateOrLoadAuthor(string author, BookstoreEntities context) { Author currentAuthor = context.Authors.FirstOrDefault(x => x.Name.ToLower() == author.ToLower()); if (currentAuthor == null) { currentAuthor = new Author(); currentAuthor.Name = author; context.Authors.Add(currentAuthor); context.SaveChanges(); } return currentAuthor; }
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(); }
public static Review AddReview(int? authorId, int? bookId, DateTime reviewDate, string reviewContent) { BookstoreEntities context = new BookstoreEntities(); Review review = new Review(); review.AuthorId = authorId; review.BookId = bookId; review.Date = reviewDate; review.Text = reviewContent; context.Reviews.Add(review); context.SaveChanges(); return review; }
private Author CreateOrLoadAuthor(BookstoreEntities context, string authorName) { Author author = context.Authors.FirstOrDefault<Author>(a => a.Name == authorName); if (author != null) { return author; } Author newAuthor = new Author() { Name = authorName }; context.Authors.Add(newAuthor); context.SaveChanges(); return newAuthor; }
// 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 void AddBook(string title, string isbn, string author, decimal price, string website) { BookstoreEntities context = new BookstoreEntities(); Book book = new Book(); book.Title = title; book.ISBN = isbn; Author newAuthor = CreateOrLoadAuthor(context, author); book.Authors.Add(newAuthor); book.Price = price; book.Website = website; context.Books.Add(book); context.SaveChanges(); }
public Author CreateOrLoadAuthor(BookstoreEntities context, string authorName) { Author existingAuthor = context.Authors.Where(a => a.Name == authorName) .FirstOrDefault(); if (existingAuthor != null) { return existingAuthor; } Author newAuthor = new Author(); newAuthor.Name = authorName; context.Authors.Add(newAuthor); context.SaveChanges(); return newAuthor; }
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; }
private static Review CreateReview(BookstoreEntities context, Book book, string reviewText, string reviewsDate, string reviewsAuthors) { Review review = new Review(); review.Date = DateTime.Parse(reviewsDate); review.AuthorName = "anonymous"; if (reviewsAuthors != null) { Author author = CreateOrLoadAuthor(context, reviewsAuthors); review.AuthorName = author.Name; } review.Text = reviewText; review.Book = book; context.Reviews.Add(review); context.SaveChanges(); return(review); }
private static Author CreateOrLoadAuthor( BookstoreEntities context, string authorName) { Author existingAuthor = (from a in context.Authors where a.Name.ToLower() == authorName.ToLower() select a).FirstOrDefault(); if (existingAuthor != null) { return existingAuthor; } Author newAuthor = new Author(); newAuthor.Name = authorName; context.Authors.Add(newAuthor); context.SaveChanges(); return newAuthor; }
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(); }
private static Author CreateOrLoadAuthor(BookstoreEntities context, string author) { Author existringAuthor = (from a in context.Authors where a.Name == author select a).FirstOrDefault(); if (existringAuthor != null) { return(existringAuthor); } Author newAuthor = new Author(); newAuthor.Name = author; context.Authors.Add(newAuthor); context.SaveChanges(); return(newAuthor); }
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; }
private static Author GetSingleAuthor(BookstoreEntities context, XmlNode authorItem) { if (authorItem.Name != "author" || authorItem.InnerText.Length == 0) { throw new ApplicationException("Invalid input XML file - Invalid Author tag"); } string authorName = authorItem.InnerText; var author = context.Authors.Where(a => a.Name == authorName).ToList(); if (author.Count == 0) { var newAuthor = new Author(); newAuthor.Name = authorName; context.Authors.Add(newAuthor); context.SaveChanges(); author.Add(newAuthor); } return author[0]; }
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 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(); }
private static Review CreateReview(BookstoreEntities context, string review) { Review newReview = new Review(); newReview.Notes=review; context.Reviews.Add(newReview); context.SaveChanges(); return newReview; }
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(); }
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); } } } }
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(); }
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(); } } } }