Example #1
0
        //
        // Returns a random quote based on the internal random number generator.
        //
        public Quote Random()
        {
            Quote quote = null;

            if (PrincipalTable.TableExists())
            {
                quote = PrincipalTable.RandomQuote();
            }

            return(quote);
        }
Example #2
0
        //
        // Returns a list of quotes given a page number.
        //
        // By default, the function retrieves 10 quotes at a time.  The
        // caller can specify which "lot" of ten it would like to retrieve.
        //
        // The functiont throws exception if the page number specified is
        // less than or equal to 0, or if it is outside the total number
        // of pages given the numebr of quotes.
        //
        // It also throws an exception or if the page size specified is larger
        // than the max number of entries allowed, or if the
        //
        public IEnumerable <Quote> QuotesByPage(
            ulong pageNum,
            uint pageSize = 10)
        {
            if (0 >= pageNum)
            {
                throw new ArgumentOutOfRangeException(
                          "pageNum",
                          "Page number cannot be less than 1."
                          );
            }

            if ((0 >= pageSize) || (MaxEntriesPerPage < pageSize))
            {
                throw new ArgumentOutOfRangeException(
                          "pageSize",
                          "Number of entries per page is outside " +
                          "permissible range.");
            }

//             ulong quoteCount = Count();
//
//             ulong maxNumOfPages = (quoteCount + pageSize) / pageSize;

            ulong maxNumOfPages = PageCount(pageSize);

            if (pageNum > maxNumOfPages)
            {
                throw new ArgumentOutOfRangeException(
                          "pageNum",
                          "Page number specified exceeds max number of pages."
                          );
            }

            ulong beginningRow = (pageNum - 1) * pageSize + 1;

            ulong endRow = (beginningRow - 1) + pageSize;

            IEnumerable <Quote> quotes = null;

            if (PrincipalTable.TableExists())
            {
                Console.WriteLine("Retrieving entry {0} to {1}...",
                                  beginningRow,
                                  endRow);

                quotes = PrincipalTable.GetQuotesInRange(beginningRow, endRow);
            }

            return(quotes);
        }
Example #3
0
        //
        // Returns a quote given a quote ID.
        //
        public Quote Find(string quoteId)
        {
            if (null == _dbConn)
            {
                return(null);
            }

            Quote quote = null;

            if (PrincipalTable.TableExists())
            {
                quote = PrincipalTable.GetQuoteByQid(quoteId);
            }

            return(quote);
        }
Example #4
0
        //
        // Returns the number of quotes in the database.
        //
        public ulong Count()
        {
            ulong quoteCount = 0L;

            if (null == _dbConn)
            {
                return(0L);
            }

            if (PrincipalTable.TableExists())
            {
                quoteCount = PrincipalTable.QuotesCount();
            }

            return(quoteCount);
        }
Example #5
0
        //
        // Drops database table(s).
        //
        public static void CleanUpDB(IConfiguration config)
        {
            _logger.Trace("Dropping database tables...");

            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 (false == PrincipalTable.TableExists())
            {
                _logger.Info("Database table was not set up.  " +
                             "No clean up needed.");

                Console.WriteLine(
                    "No further actions required - table not setup.");

                return;
            }


            _logger.Trace("Dropping database table...");

            PrincipalTable.DropTable();


            if (PrincipalTable.TableExists())

            {
                _logger.Error("Failed to clean up database!");

                Console.WriteLine("Error: Failed to clean up database!");
            }
            else
            {
                _logger.Info("Database cleaned up");

                Console.WriteLine("Database cleaned up.");
            }

            return;
        }
Example #6
0
        //
        // Output via the console a single quote given the quote ID
        // from the commandline argument "--qid".
        //
        public static void GetQuoteByQid(IConfiguration config)
        {
            _logger.Trace("Retrieving quote...");

            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 already exist, " +
                              "cannot locate quotes by ID.");

                Console.WriteLine("Error: Operation aborted.");

                return;
            }

            string quoteId = config["qid"];

            string qid = config["qid"];

            Quote quote = null;

            if (null != qid)
            {
                quote = PrincipalTable.GetQuoteByQid(qid);
            }

            if (null == quote)
            {
                string message = $"No quote with ID '{qid}' found.";

                _logger.Info(message);

                Console.WriteLine(message);

                return;
            }

            _logger.Info("Quote = " + quote.ToString());

            Console.WriteLine("-- Quote ID = {0}.", quote.ID);

            Console.WriteLine("-- Ext Ref ID = {0}.", quote.ExternalRefID);

            Console.WriteLine("-- Quote text = {0}.", quote.Content);

            Console.WriteLine("-- Quote source = {0}.", quote.Source);

            Console.WriteLine("-- Source URL = {0}.", quote.SourceUrl);

            Console.WriteLine("-- Quote entry datetime = {0}.",
                              quote.EntryDatetime);

            Console.WriteLine("-- Quote last mod = {0}.",
                              quote.ModTimestamp);

            Console.WriteLine("-- Quote = {0}.", quote.ToString());
        }
Example #7
0
        //
        // 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;
        }
Example #8
0
        //
        // 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;
        }