public BookDetail Load() { BookDetail bookDetail = new BookDetail(); StreamReader reader = new StreamReader("..\\..\\..\\Data\\BX-Books.csv"); string line = reader.ReadLine(); while (!reader.EndOfStream) { Book book = new Book(); line = reader.ReadLine(); //line.Replace('"', ' '); string[] details = line.Split(';'); for (int i = 0; i < details.Length; i++) { details[i] = details[i].Trim('"'); } ; book.ISBN = details[0]; book.BookTitle = details[1]; book.BookAuthor = details[2]; book.Year = int.Parse(details[3]); book.Publisher = details[4]; book.ImageUrlSmall = details[5]; book.ImageUrlMedium = details[6]; book.ImageUrlLarge = details[7]; bookDetail.Books.Add(book); } reader = new StreamReader("..\\..\\..\\Data\\BX-Users.csv"); line = reader.ReadLine(); while (!reader.EndOfStream) { User user = new User(); line = reader.ReadLine(); //line.Replace('"', ' '); string[] details = line.Split(';'); for (int i = 0; i < details.Length; i++) { details[i] = details[i].Trim('"'); } ; user.UserID = int.Parse(details[0].Trim('"')); string[] location = details[1].Split(','); user.City = location[0]; user.State = location[1]; user.Country = location[2]; user.Age = int.Parse(details[2].Trim('"')); bookDetail.Users.Add(user); } reader = new StreamReader("..\\..\\..\\Data\\BX-Book-Ratings.csv"); line = reader.ReadLine(); while (!reader.EndOfStream) { bool isValidUser = false, isValidBook = false; BookUserRating Rating = new BookUserRating(); line = reader.ReadLine(); string[] details = line.Split(';'); for (int i = 0; i < details.Length; i++) { details[i] = details[i].Trim('"'); } ; int uid = int.Parse(details[0].Trim('"')); //Parallel.ForEach(bookDetail.Users, new ParallelOptions { MaxDegreeOfParallelism = (Environment.ProcessorCount / 3) }, (user) => //{ //Task task = Task.Factory.StartNew(()=> { foreach (var user in bookDetail.Users) { if (user.UserID == uid) { isValidUser = true; Rating.User = user; user.UserRatings.Add(Rating); } }//); string isbn = details[1]; //Parallel.ForEach(bookDetail.Books, new ParallelOptions { MaxDegreeOfParallelism = (Environment.ProcessorCount / 3) }, (book) => //Task task2 = Task.Factory.StartNew(() =>{ foreach (var book in bookDetail.Books) { if (book.ISBN == isbn) { isValidBook = true; Rating.Book = book; book.Ratings.Add(Rating); } } //); //task.Wait(); //task2.Wait(); if (isValidUser && isValidBook) { Rating.Rating = int.Parse(details[2].Trim('"')); Rating.UserID = uid; Rating.ISBN = isbn; bookDetail.BookRatings.Add(Rating); } } return(bookDetail); }
public BookDetails Load() { BookDetails bookDetails = new BookDetails(); IDbConnection connection = null; string provider = ConfigurationManager.ConnectionStrings["BookUserRatingDatabase"].ProviderName; DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(provider); connection = dbProviderFactory.CreateConnection(); connection.ConnectionString = ConfigurationManager.ConnectionStrings["BookUserRatingDatabase"].ConnectionString; IDbCommand cmd = connection.CreateCommand(); cmd.Connection = connection; string getUsersQuery = "Select * from bx_users"; string getBooksQuery = "Select * from bx_books"; string getBookRatingsQuery = "Select * from bx_book_ratings"; List <User> users = new List <User>(); List <Book> books = new List <Book>(); List <BookUserRating> bookUserRatings = new List <BookUserRating>(); IDataReader reader = null; using (connection) { connection.Open(); //try { cmd.CommandText = getUsersQuery; reader = cmd.ExecuteReader(); while (reader.Read()) { User user = new User(); user.UserId = int.Parse($"{reader[0]}"); user.City = reader.GetString(1).Split(',')[0].Trim(); user.State = reader.GetString(1).Split(',')[1].Trim(); user.Country = reader.GetString(1).Split(',')[2].Trim(); int?val = reader["Age"] as int?; user.Age = (val == null ? 0 : (int)val); users.Add(user); } reader.Close(); cmd.CommandText = getBooksQuery; reader = cmd.ExecuteReader(); while (reader.Read()) { Book book = new Book { ISBN = $"{reader[0]}", BookTitle = $"{reader[1]}", BookAuthor = $"{reader[2]}", YearOfPublication = int.Parse($"{reader[3]}"), Publisher = $"{reader[4]}", Image_URL_S = $"{reader[5]}", Image_URL_M = $"{reader[6]}", Image_URL_L = $"{reader[7]}" }; books.Add(book); } reader.Close(); cmd.CommandText = getBookRatingsQuery; reader = cmd.ExecuteReader(); while (reader.Read()) { BookUserRating bookUserRating = new BookUserRating { UserId = int.Parse($"{reader[0]}"), ISBN = $"{reader[1]}", BookRating = long.Parse($"{reader[2]}") }; bookUserRatings.Add(bookUserRating); } } //catch (SystemException se) //{ // throw se; //} //finally //{ reader.Close(); //} } bookDetails.users = users; bookDetails.books = books; bookDetails.bookUserRatings = bookUserRatings; return(bookDetails); }