private static Author CreateOrLoadAuthor( BookStoreDB context, string authorName) { Author existingAuthor = (from u in context.Authors where u.Name == authorName select u).FirstOrDefault(); if (existingAuthor != null) { return existingAuthor; } Author newAuthor = new Author(); newAuthor.Name = authorName; context.Authors.Add(newAuthor); context.SaveChanges(); return newAuthor; }
private static void ParseComplexXml() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("../../complex-books.xml"); var books = xmlDoc.DocumentElement.ChildNodes; using (var db = new BookStoreDBEntities()) { foreach (XmlNode book in books) { TransactionScope tran = new TransactionScope( TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.RepeatableRead }); using (tran) { string title = book["title"].InnerText.Trim(); ICollection<Author> authors = null; if (book.SelectSingleNode("authors") != null) { authors = new List<Author>(); foreach (XmlNode author in book.SelectSingleNode("authors").ChildNodes) { string authorName = null; if (author.InnerText != null) { authorName = author.InnerText.Trim(); } var authorInDb = (from a in db.Authors where a.AuthorName == authorName select a).FirstOrDefault(); if (authorInDb == null && authorName != null) { Author newAuthor = new Author() { AuthorName = authorName }; db.Authors.Add(newAuthor); db.SaveChanges(); authors.Add(newAuthor); } } } string webSite = null; if (book.SelectSingleNode("web-site") != null) { webSite = book["web-site"].InnerText.Trim(); } ICollection<Rewiew> reviews = null; if (book.SelectSingleNode("reviews") != null) { reviews = new List<Rewiew>(); foreach (XmlNode review in book.SelectSingleNode("reviews").ChildNodes) { Rewiew currentReview = new Rewiew(); string reviewText = null; if (review.InnerText != null) { reviewText = review.InnerText.Trim(); currentReview.RewiewText = reviewText; } string authorName = null; if (review.Attributes["author"] != null) { authorName = review.Attributes["author"].Value; } if (authorName != null) { authorName = authorName.Trim(); var authorInDb = (from a in db.Authors where a.AuthorName == authorName select a).FirstOrDefault(); if (authorInDb == null) { Author newAuthor = new Author() { AuthorName = authorName }; db.Authors.Add(newAuthor); db.SaveChanges(); currentReview.Author = newAuthor; } else { currentReview.Author = authorInDb; } } DateTime reviewDate = DateTime.Now; if (review.Attributes["date"] != null) { reviewDate = DateTime.Parse(review.Attributes["date"].Value); } currentReview.RewiewDate = reviewDate; reviews.Add(currentReview); } } string isbn = null; if (book.SelectSingleNode("isbn") != null) { isbn = book["isbn"].InnerText.Trim(); } decimal price = 0m; if (book.SelectSingleNode("price") != null) { price = decimal.Parse(book["price"].InnerText.Trim(), CultureInfo.InvariantCulture); } CreateAndAddReviewInDb(db, authors, reviews, title, isbn, price, webSite); tran.Complete(); } } } }
private static void CreateAndAddBookInDb( BookStoreDBEntities db, string author, string title, string isbn, decimal price, string webSite) { ICollection<Author> bookAuthors = new List<Author>(); // Check is title exist var titleInDb = (from bookDb in db.Books where bookDb.BookTitle == title select bookDb).FirstOrDefault(); var authorInDb = (from authorDb in db.Authors where authorDb.AuthorName == author select authorDb).FirstOrDefault(); if (titleInDb != null && authorInDb != null) { return; } if (authorInDb == null) { Author newAuthor = new Author() { AuthorName = author }; db.Authors.Add(newAuthor); db.SaveChanges(); bookAuthors.Add(newAuthor); if (titleInDb != null) { titleInDb.Authors.Add(newAuthor); } } Book newBook = new Book() { Authors = bookAuthors, BookTitle = title }; if (isbn != null) { newBook.ISBN = isbn; var isbnInDb = (from b in db.Books where b.ISBN == isbn select b).FirstOrDefault(); if (isbnInDb != null) { throw new ApplicationException("Duplicated ISBN"); } } if (price != 0m) { newBook.Price = price; } if (webSite != null) { newBook.WebSite = webSite; } db.Books.Add(newBook); db.SaveChanges(); }
private static void InitializeBookStore(BookContext context) { if (!context.Author.Any()) { Author auth_01 = new Author { Name = "Chris Sakellarios", ContactNumber = "1234", Address = "5th avenue,NCY", CreateDate = DateTime.Now }; Author auth_02 = new Author { Name = "Charlene Campbell", ContactNumber = "1234", Address = "5th avenue,NCY", CreateDate = DateTime.Now }; Author auth_03 = new Author { Name = "Mattie Lyons", ContactNumber = "1234", Address = "5th avenue,NCY", CreateDate = DateTime.Now }; Author auth_04 = new Author { Name = "Kelly Alvarez", ContactNumber = "1234", Address = "5th avenue,NCY", CreateDate = DateTime.Now }; context.Author.Add(auth_01); context.Author.Add(auth_02); context.Author.Add(auth_03); context.Author.Add(auth_04); context.SaveChanges(); } if (!context.Books.Any()) { Books book_01 = new Books { Title = "Meeting", Isbn = "234-456-678", Price = 50, AvailableQuantity = 10, CreateDate = DateTime.Now, AuthorId = 1 }; Books book_02 = new Books { Title = "Comics", Isbn = "231-246-678", Price = 100, AvailableQuantity = 10, CreateDate = DateTime.Now, AuthorId = 2 }; Books book_03 = new Books { Title = "Business", Isbn = "674-456-645", Price = 500, AvailableQuantity = 10, CreateDate = DateTime.Now, AuthorId = 1 }; Books book_04 = new Books { Title = "Crime", Isbn = "534-456-678", Price = 150, AvailableQuantity = 10, CreateDate = DateTime.Now, AuthorId = 3 }; //Schedule schedule_05 = new Schedule //{ // Title = "Friends", // Description = "Friends giving day", // Location = "Home", // CreatorId = 5, // Status = ScheduleStatus.Cancelled, // Type = ScheduleType.Other, // TimeStart = DateTime.Now.AddHours(5), // TimeEnd = DateTime.Now.AddHours(7), // DateCreated = DateTime.Now, // DateUpdated = DateTime.Now, // Attendees = new List<Attendee> // { // new Attendee() { ScheduleId = 4, UserId = 1 }, // new Attendee() { ScheduleId = 4, UserId = 2 }, // new Attendee() { ScheduleId = 4, UserId = 3 }, // new Attendee() { ScheduleId = 4, UserId = 4 }, // new Attendee() { ScheduleId = 4, UserId = 5 } // } //}; context.Books.Add(book_01); context.Books.Add(book_02); context.Books.Add(book_03); context.Books.Add(book_04); } if (!context.Orders.Any()) { Order order_01 = new Order { BookId = 10, OrderQuantity = 3, Cost = 150, CreateDate = DateTime.Now, OrderDate = Convert.ToDateTime("2017-02-02") }; Order order_02 = new Order { BookId = 11, OrderQuantity = 2, Cost = 100, CreateDate = DateTime.Now, OrderDate = Convert.ToDateTime("2018-01-02") }; Order order_03 = new Order { BookId = 12, OrderQuantity = 2, Cost = 300, CreateDate = DateTime.Now, OrderDate = Convert.ToDateTime("2019-01-02") }; Order order_04 = new Order { BookId = 13, OrderQuantity = 2, Cost = 1000, CreateDate = DateTime.Now, OrderDate = Convert.ToDateTime("2017-01-02") }; context.Orders.Add(order_01); context.Orders.Add(order_02); context.Orders.Add(order_03); context.Orders.Add(order_04); context.SaveChanges(); } }