private static List<Author> GetOrCreateAuthors(ICollection<Author> authors, BookstoreDbContext bookstoreDb) { if (authors == null || authors.Count == 0) { return null; } var authorsInContext = new List<Author>(); foreach (var author in authors) { var addedAuthor = bookstoreDb.Authors .FirstOrDefault(a => a.Name.ToLower() == author.Name.ToLower()); if (addedAuthor != null) { authorsInContext.Add(addedAuthor); } else { var newAuthor = new Author { Name = author.Name, }; bookstoreDb.Authors.Add(newAuthor); authorsInContext.Add(newAuthor); bookstoreDb.SaveChanges(); } } return authorsInContext; }
public static void SimpleAddBooks(List<NewBook> books) { var transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.RepeatableRead }); using (transaction) { var bookstoreDb = new BookstoreDbContext(); foreach (var book in books) { var newBook = new Book { Title = book.Title, WebSite = book.WebSite, Price = book.Price, ISBN = book.ISBN, Authors = GetOrCreateAuthors(book.Authors, bookstoreDb), Reviews = ReviewsAuthorsValidate(book.Reviews, bookstoreDb) }; bookstoreDb.Books.Add(newBook); } bookstoreDb.SaveChanges(); transaction.Complete(); } }
private static List<SimpleSearchResult> GenerateQuery(string author, string title, string isbn, BookstoreDbContext dbContext) { var books = dbContext.Books.Include("Authors").Include("Reviews").AsQueryable(); if (title != null) { books = books.Where(b => b.Title.ToLower() == title.ToLower()); } if (isbn != null) { books = books.Where(b => b.ISBN == isbn); } if (author != null) { books = books.Where(b => b.Authors.Any(a => a.Name.ToLower() == author.ToLower())); } books = books.OrderBy(b => b.Title); var foundBooks = books.Select(b => new SimpleSearchResult { Title = b.Title, ReviewsCount = b.Reviews.Count }).ToList(); return foundBooks; }
static void Main() { var xmlSearch = new System.Xml.XmlDocument(); //select one of the above Query, Query1 xmlSearch.Load(Query1); var query = xmlSearch.SelectSingleNode("/query"); var author = query[Author].GetText(); var title = query[Title].GetText(); var isbn = query[ISBN].GetText(); var dbContext = new BookstoreDbContext(); var queryLogEntry = new SearchLogEntry { Date = DateTime.Now, QueryXml = query.OuterXml }; var foundBooks = GenerateQuery(author, title, isbn, dbContext); dbContext.SearchLog.Add(queryLogEntry); dbContext.SaveChanges(); PrintResult(foundBooks); }
private static List<Review> ReviewsAuthorsValidate(ICollection<Review> reviews, BookstoreDbContext BookstoreDb) { if (reviews == null) { return null; } foreach (var review in reviews) { if (review.Author != null && review.Author.Name != null) { var author = GetOrCreateAuthors(new List<Author> { review.Author }, BookstoreDb); review.Author = author.First(); } } return reviews.ToList(); }
public EFBookstoreRepository(BookstoreDbContext context) { _context = context; }
public static void EnsurePopulated(IApplicationBuilder applicatoin) { BookstoreDbContext context = applicatoin.ApplicationServices. CreateScope().ServiceProvider.GetRequiredService <BookstoreDbContext>(); if (context.Database.GetPendingMigrations().Any()) { context.Database.Migrate(); } if (!context.Books.Any()) { context.Books.AddRange( new Book { Title = "Les Miserables", AuthorFirst = "Victor", AuthorMiddle = " ", AuthorLast = "Hugo", Publisher = "Signet", ISBN = "978-0451419439", Classification = "Fiction", Category = "Classic", Price = 9.95, }, new Book { Title = "Team of Rivals", AuthorFirst = "Doris", AuthorMiddle = "Kearns", AuthorLast = "Goodwin", Publisher = "Simon & Schuster", ISBN = "978-0743270755", Classification = "Non-Fiction", Category = "Biography", Price = 14.58, }, new Book { Title = "The Snowball", AuthorFirst = "Alice", AuthorMiddle = " ", AuthorLast = "Schroeder", Publisher = "Bantam", ISBN = "978-0553384611", Classification = "Non-Fiction", Category = "Biography", Price = 21.54, }, new Book { Title = "American Ulysses", AuthorFirst = "Ronald", AuthorMiddle = "C.", AuthorLast = "White", Publisher = "Random House", ISBN = "978-0812981254", Classification = "Non-Fiction", Category = "Biography", Price = 11.61, }, new Book { Title = "Unbroken", AuthorFirst = "Laura", AuthorMiddle = " ", AuthorLast = "Hillenbrand", Publisher = "Random House", ISBN = "978-0812974492", Classification = "Non-Fiction", Category = "Historical", Price = 13.33, }, new Book { Title = "The Great Train Robbery", AuthorFirst = "Michael", AuthorMiddle = " ", AuthorLast = "Crichton", Publisher = "Vintage", ISBN = "978-0804171281", Classification = "Fiction", Category = "Historical Fiction", Price = 15.95, }, new Book { Title = "Deep Work", AuthorFirst = "Cal", AuthorMiddle = " ", AuthorLast = "Newport", Publisher = "Grand Central Publishing", ISBN = "978-1455586691", Classification = "Non-Fiction", Category = "Self-Help", Price = 14.99, }, new Book { Title = "It's Your Ship", AuthorFirst = "Michael", AuthorMiddle = " ", AuthorLast = "Abrashoff", Publisher = "Grand Central Publishing", ISBN = "978-1455523023", Classification = "Non-Fiction", Category = "Self-Help", Price = 21.66, }, new Book { Title = "The Virgin Way", AuthorFirst = "Richard", AuthorMiddle = " ", AuthorLast = "Branson", Publisher = "Portfolio", ISBN = "978-1591847984", Classification = "Non-Fiction", Category = "Business", Price = 29.16, }, new Book { Title = "Sycamore Row", AuthorFirst = "John", AuthorMiddle = " ", AuthorLast = "Grisham", Publisher = "Bantam", ISBN = "978-0553393613", Classification = "Fiction", Category = "Thrillers", Price = 15.03, } ); context.SaveChanges(); } }
public static List<Review> FindReviews(string type, string startDate, string endDate, string author) { using (var dbContex = new BookstoreDbContext()) { var reviews = dbContex.Reviews.Include("Author").Include("Book").AsQueryable(); if (startDate != null) { var starOfPeriod = DateTime.Parse(startDate); var endOfPeriod = DateTime.Parse(endDate); reviews = reviews.Where(r => r.CreationDate >= starOfPeriod && r.CreationDate <= endOfPeriod); } if (author != null) { reviews = reviews.Where(r => r.Author.Name.ToLower() == author.ToLower()); } reviews = reviews.OrderBy(r => r.CreationDate).ThenBy(r => r.Text).Select(r => new Review { CreationDate = r.CreationDate, Text = r.Text, Book = new Book { Authors = r.Book.Authors, Title = r.Book.Title, WebSite = r.Book.WebSite, ISBN = r.Book.ISBN, } }); return reviews.ToList(); } }
public static void EnsurePopulated(IApplicationBuilder application) { BookstoreDbContext context = application.ApplicationServices.CreateScope().ServiceProvider.GetRequiredService <BookstoreDbContext>(); //Check if there are any pendin migrations and migrate them if (context.Database.GetPendingMigrations().Any()) { context.Database.Migrate(); } //Check if there are not any books if (!context.Books.Any()) { //Add books if there are not any yet context.Books.AddRange( new Book { ISBN = "978-0451419439", Title = "Les Miserables", AuthorFirstName = "Victor", AuthorLastName = "Hugo", Publisher = "Signet", Classification = "Fiction", Category = "Classic", Price = 9.95, Pages = 1488 }, new Book { ISBN = "978-0743270755", Title = "Team of Rivals", AuthorFirstName = "Doris", AuthorMiddleName = "Kearns", AuthorLastName = "Goodwin", Publisher = "Simon & Schuster", Classification = "Non-Fiction", Category = "Biography", Price = 14.58, Pages = 944 }, new Book { ISBN = "978-0553384611", Title = "The Snowball", AuthorFirstName = "Alice", AuthorLastName = "Schroeder", Publisher = "Bantam", Classification = "Non-Fiction", Category = "Biography", Price = 21.54, Pages = 832 }, new Book { ISBN = "978-0812981254", Title = "American Ulysses", AuthorFirstName = "Ronald", AuthorMiddleName = "C.", AuthorLastName = "White", Publisher = "Random House", Classification = "Non-Fiction", Category = "Biography", Price = 21.54, Pages = 864 }, new Book { ISBN = "978-0812974492", Title = "Unbroken", AuthorFirstName = "Laura", AuthorLastName = "Hillenbrand", Publisher = "Random House", Classification = "Non-Fiction", Category = "Historical", Price = 13.33, Pages = 528 }, new Book { ISBN = "978-0804171281", Title = "The Great Train Robbery", AuthorFirstName = "Michael ", AuthorLastName = "Crichton", Publisher = "Vintage", Classification = "Fiction", Category = "Historical Fiction", Price = 15.95, Pages = 288 }, new Book { ISBN = "978-1455586691", Title = "Deep Work", AuthorFirstName = "Cal", AuthorLastName = "Newport", Publisher = "Grand Central Publishing", Classification = "Non-Fiction", Category = "Self-Help", Price = 14.99, Pages = 304 }, new Book { ISBN = "978-1455523023", Title = "It's Your Ship", AuthorFirstName = "Michael", AuthorLastName = "Abrashoff", Publisher = "Grand Central Publishing", Classification = "Non-Fiction", Category = "Self-Help", Price = 21.66, Pages = 240 }, new Book { ISBN = "978-1591847984", Title = "The Virgin Way", AuthorFirstName = "Richard", AuthorLastName = "Branson", Publisher = "Portfolio", Classification = "Non-Fiction", Category = "Business", Price = 29.16, Pages = 400 }, new Book { ISBN = "978-0553393613", Title = "Sycamore Row", AuthorFirstName = "John", AuthorLastName = "Grisham", Publisher = "Bantam", Classification = "Non-Fiction", Category = "Thrillers", Price = 15.03, Pages = 642 }, new Book { ISBN = "978-1432859329", Title = "Blackmoore", AuthorFirstName = "Julianne", AuthorLastName = "Donaldson", Publisher = "Shadow Mountain", Classification = "Fiction", Category = "Romance", Price = 9.51, Pages = 320 }, new Book { ISBN = "978-2382261101", Title = "How to Win Friends and Influence People", AuthorFirstName = "Dale", AuthorLastName = "Carnegie", Publisher = "Pocket Books", Classification = "Non-Fiction", Category = "Self-Help", Price = 10.38, Pages = 288 }, new Book { ISBN = "978-2382261101", Title = "Grit", AuthorFirstName = "Angela", AuthorLastName = "Duckworth", Publisher = "Scribner Book Company", Classification = "Non-Fiction", Category = "Self-Help", Price = 19.04, Pages = 352 } ); //Save the changes context.SaveChanges(); } }
private BookstoreDbContext _context; // private BookstoreDbContext type called context _ means private public EFBookstoreRepository(BookstoreDbContext context) // Constructor { _context = context; }
//Constructor public EFBookstoreRepository(BookstoreDbContext context) { //Store the parameter context into the private context _context = context; }