// // Add quotes from a json file that is in the same format as the // seeding file. // public static void AddQuotesByFile(IConfiguration config) { _logger.Trace("Adding new quotes to table..."); string cs = null; try { cs = GetConnectionString(); } catch (System.ArgumentException ex) { _logger.Fatal($"Cannot get connection string: {ex.Message}"); Console.WriteLine($"Error: Operation aborted - {ex.Message}."); return; } catch (Exception ex) { _logger.Fatal($"Unable to access database : {ex.Message}"); Console.WriteLine($"Error: Operation aborted - {ex.Message}."); return; } MySqlConnection conn = new MySqlConnection(cs); PrincipalTable.DatabaseConnection = conn; if (!PrincipalTable.TableExists()) { _logger.Fatal("Table does not exist, cannot add new quotes."); Console.WriteLine("Error: Operation aborted."); return; } string filename = null; filename = config["add"]; if (null == filename) { _logger.Error("No quote file specified - " + "no quotes added."); Console.WriteLine( "Error: Operation aborted - no file specified."); return; } _logger.Trace($"Adding quotes from file {filename}."); int count = PrincipalTable.AddQuotesFromFile(filename); if (0 >= count) { Console.WriteLine("No quotes added into database."); } else { Console.WriteLine($"Number of quotes added = {count}."); } return; }
// // Set up the database table(s) and seed the table where // appropriate. // public static void SetupAndSeed() { _logger.Trace("Set up and seed tables..."); string cs = null; try { cs = GetConnectionString(); } catch (System.ArgumentException ex) { _logger.Error("Error: {0}.", ex.Message); string errorMessage = "Cannot get connection string for " + "database, unable to proceed"; _logger.Fatal(errorMessage); Console.WriteLine($"Error: Operation aborted - {ex.Message}."); return; } catch (Exception ex) { _logger.Error("Error: {0}.", ex.Message); string errorMessage = "Failed to set up database table."; _logger.Fatal(errorMessage); Console.WriteLine($"Error: Operation aborted - {ex.Message}."); return; } MySqlConnection conn = new MySqlConnection(cs); PrincipalTable.DatabaseConnection = conn; bool tableExists = false; try { tableExists = PrincipalTable.TableExists(); } catch (MySql.Data.MySqlClient.MySqlException ex) { _logger.Fatal("Abort operation due to database error: " + ex.Message); Console.WriteLine($"Error: Operation aborted - {ex.Message}."); return; } catch (Exception ex) { _logger.Fatal("Abort operation to set up and seed " + "database due to error: " + ex.Message); Console.WriteLine($"Error: Operation aborted - {ex.Message}."); return; } if (tableExists) { string errorMessage = "Database table " + PrincipalTable.Name + " " + "already exists - " + "table not created."; _logger.Warn(errorMessage); Console.WriteLine($"No action required - {errorMessage}."); return; } try { PrincipalTable.CreateTable(); PrincipalTable.AddTableTriggers(); } catch (Exception ex) { string message = "Failed table/trigger creation: " + ex.Message; _logger.Error(message); _logger.Fatal("Abort operation to set up database!"); Console.WriteLine($"Error: Operation aborted - {ex.Message}."); return; } QuoteInput seed = new QuoteInput(); string filename = null; filename = Configuration["seed"]; if (null == filename) { string errorMessage = "No seed file specified - " + "table not seeded."; _logger.Warn(errorMessage); Console.WriteLine( "Warn: Database not seeded, missing seed file."); return; } _logger.Info("Seeding newly created database with " + $"file {filename}."); int count = PrincipalTable.AddQuotesFromFile(filename); if (0 >= count) { Console.WriteLine("No quotes seeded into database."); } else { Console.WriteLine($"Number of quotes seeded = {count}."); } return; }