public static void Main(string[] args) { //pripremi CSV putanju string projectPath = PathUtils.GetProjectDirectoryPath(); string csvPath = Path.Combine(projectPath, "Datasets", "books.csv"); //procitaj App.config bool readEverything = bool.Parse(ConfigurationManager.AppSettings["readfullcsv"]); if (readEverything) { //procitaj csv datoteku List <BookCSV> bookRecords = new BookCSVReader().GetIntances(csvPath); //posalji podatke na API endpoint Task.Run(async() => { await LibraryClientFactory.GetBookClient().AddBooks(bookRecords); }); } Console.WriteLine("END OF WORK!"); Console.ReadLine(); }
public void Start() { //pripremi putanju do books.csv datoteke string projectPath = PathUtils.GetProjectDirectoryPath(); string csvPath = Path.Combine(projectPath, "Datasets", "books.csv"); //procitaj csv datoteku List <BookCSV> bookRecords = new BookCSVReader().GetIntances(csvPath); //pomocna lista List <BookCSV> booksForCsv = new List <BookCSV>(); using (LibraryDBContext context = new LibraryDBContext(Options)) { if (context.Books.Count() == 0) { //kreiranje nasumicnih brojeva Random = new Random(); //broj kategorija int categoryCount = context.Categories.Count(); foreach (var book in bookRecords) { //odaberi nasumicni broj za kategoriju int categoryID = Random.Next(0, categoryCount); //Mapiranje u objekt tipa Book var newBook = new Book() { AvarageRating = book.AvarageRating, CategoryID = categoryID, ISBN = book.ISBN, ISBN13 = book.ISBN13, LanguageCode = book.LanguageCode, PageNumber = book.PageNumber, Title = book.Title, RatingsCount = book.RatingsCount, Authors = book.Authors }; //ako je dobar upis u bazu newBook ce biti razlicit od NULL i imati ce ID context.Books.Add(newBook); if (newBook != null) { //keiraj 30 kopija knjige for (int i = 0; i < 20; i++) { //napravi kopiju knjige BookCopy copy = new BookCopy() { BookId = newBook.Id, Borrowed = false, }; //dodaj kopiju knige u bazu context.BookCopies.Add(copy); } } try { context.SaveChanges(); } catch { } } //upisi u novi CSV samo titlove i kod jezika string csvPathForTitles = Path.Combine(projectPath, "Datasets", "titles.csv"); new BookCSVTitleWriter().WriteToCSVFile(booksForCsv, csvPathForTitles); } } }