diff --git a/DiscordBot/Objects/Quote.cs b/DiscordBot/Objects/Quote.cs index f18e92d..65e8344 100644 --- a/DiscordBot/Objects/Quote.cs +++ b/DiscordBot/Objects/Quote.cs @@ -1,10 +1,6 @@ using System; using System.Collections.Generic; -using System.Globalization; -using System.Threading.Tasks; -using Discord; using DiscordBot.Database; -using DiscordBot.Extensions; using MySql.Data.MySqlClient; namespace DiscordBot.Objects @@ -12,15 +8,19 @@ namespace DiscordBot.Objects public class Quote { internal static List Quotes; - - internal int QId; + internal ulong CreatorId, AcceptedBy, CreatedIn, AcceptedIn; - internal string QuoteText; internal DateTime QCreatedTimestamp; - private Quote() { } - - internal Quote(int quoteId, ulong createdBy, ulong acceptedBy, string quote, DateTime createdTimestamp, ulong createdIn, ulong acceptedIn) + internal int QId; + internal string QuoteText; + + private Quote() + { + } + + internal Quote(int quoteId, ulong createdBy, ulong acceptedBy, string quote, DateTime createdTimestamp, + ulong createdIn, ulong acceptedIn) { QId = quoteId; CreatorId = createdBy; @@ -30,15 +30,15 @@ internal Quote(int quoteId, ulong createdBy, ulong acceptedBy, string quote, Dat CreatedIn = createdIn; AcceptedIn = acceptedIn; } - + internal static List LoadAll() { - List quotes = new List(); + var quotes = new List(); (MySqlDataReader dr, MySqlConnection conn) reader = DatabaseActivity.ExecuteReader("SELECT * FROM quotes;"); - + while (reader.dr.Read()) { - Quote q = new Quote + var q = new Quote { QId = reader.dr.GetInt32("quoteId"), CreatorId = reader.dr.GetUInt64("createdBy"), @@ -51,19 +51,27 @@ internal static List LoadAll() quotes.Add(q); } - + reader.dr.Close(); reader.conn.Close(); return quotes; } - internal static bool AddQuote(string quote, ulong creatorId, ulong createdIn, ulong acceptedBy = 0, ulong acceptedIn = 0, DateTime? createdTimestamp = null) + internal static bool AddQuote(string quote, ulong creatorId, ulong createdIn, ulong acceptedBy = 0, + ulong acceptedIn = 0, DateTime? createdTimestamp = null) { - if (acceptedBy == 0) { acceptedBy = creatorId; } - if (acceptedIn == 0) { acceptedIn = createdIn; } - - List<(string, string)> queryParams = new List<(string id, string value)>() + if (acceptedBy == 0) + { + acceptedBy = creatorId; + } + + if (acceptedIn == 0) + { + acceptedIn = createdIn; + } + + List<(string, string)> queryParams = new List<(string id, string value)> { ("@createdBy", creatorId.ToString()), ("@acceptedBy", acceptedBy.ToString()), @@ -71,19 +79,26 @@ internal static bool AddQuote(string quote, ulong creatorId, ulong createdIn, ul ("@createdIn", createdIn.ToString()), ("@acceptedIn", acceptedIn.ToString()) }; - - int rowsUpdated = DatabaseActivity.ExecuteNonQueryCommand( + + var rowsUpdated = DatabaseActivity.ExecuteNonQueryCommand( "INSERT IGNORE INTO " + "quotes(quoteId,createdBy,acceptedBy,quoteText,dateCreated,createdIn,acceptedIn) " + - "VALUES (NULL, @createdBy, @acceptedBy, @quoteText, CURRENT_TIMESTAMP, @createdIn, @acceptedIn);", queryParams); - + "VALUES (NULL, @createdBy, @acceptedBy, @quoteText, CURRENT_TIMESTAMP, @createdIn, @acceptedIn);", + queryParams); + // Add quote to the current loaded quote-list - (MySqlDataReader dr, MySqlConnection conn) reader = DatabaseActivity.ExecuteReader("SELECT * FROM quotes ORDER BY quoteId DESC LIMIT 1"); - int newId = 0; - while (reader.dr.Read()) { newId = reader.dr.GetInt32("quoteId"); } + (MySqlDataReader dr, MySqlConnection conn) reader = + DatabaseActivity.ExecuteReader("SELECT * FROM quotes ORDER BY quoteId DESC LIMIT 1"); + + var newId = 0; + while (reader.dr.Read()) + { + newId = reader.dr.GetInt32("quoteId"); + } + reader.dr.Close(); reader.conn.Close(); - + Quotes.Add(new Quote { QId = newId, @@ -94,21 +109,21 @@ internal static bool AddQuote(string quote, ulong creatorId, ulong createdIn, ul CreatedIn = createdIn, AcceptedIn = acceptedIn }); - + return rowsUpdated == 1; } internal static bool UpdateQuote(int quoteId, string quoteText) { - Quote quote = Quotes.Find(q => q.QId == quoteId); - int index = Quotes.IndexOf(Quotes.Find(q => q.QId == quoteId)); + var quote = Quotes.Find(q => q.QId == quoteId); + var index = Quotes.IndexOf(Quotes.Find(q => q.QId == quoteId)); quote.QuoteText = quoteText; Quotes[index] = quote; - string formattedText = quoteText.Replace("\"", "\\\""); - - int rowsAffected = DatabaseActivity.ExecuteNonQueryCommand( - "UPDATE quotes SET `quoteText`=\"" + formattedText + "\" WHERE `quoteId`=" + quoteId +";"); + var formattedText = quoteText.Replace("\"", "\\\""); + + var rowsAffected = DatabaseActivity.ExecuteNonQueryCommand( + "UPDATE quotes SET `quoteText`=\"" + formattedText + "\" WHERE `quoteId`=" + quoteId + ";"); return rowsAffected == 1; } @@ -116,10 +131,10 @@ internal static bool UpdateQuote(int quoteId, string quoteText) internal static bool DeleteQuote(int quoteId) { Quotes.Remove(Quotes.Find(quote => quote.QId == quoteId)); - - int rowsAffected = DatabaseActivity.ExecuteNonQueryCommand( + + var rowsAffected = DatabaseActivity.ExecuteNonQueryCommand( "DELETE FROM quotes WHERE quoteId=" + quoteId + ";"); - + return rowsAffected == 1; } } diff --git a/DiscordBot/Objects/RequestQuote.cs b/DiscordBot/Objects/RequestQuote.cs index 104f5eb..c286db1 100644 --- a/DiscordBot/Objects/RequestQuote.cs +++ b/DiscordBot/Objects/RequestQuote.cs @@ -8,15 +8,19 @@ namespace DiscordBot.Objects public class RequestQuote { internal static List RequestQuotes; - - internal int RequestId; + internal ulong CreatedBy, CreatedIn; - internal string QuoteText; internal DateTime QCreatedTimestamp; - - private RequestQuote() { } + internal string QuoteText; + + internal int RequestId; + + private RequestQuote() + { + } - internal RequestQuote(int requestId, ulong createdBy, ulong createdIn, string quoteText, DateTime createdTimestamp) + internal RequestQuote(int requestId, ulong createdBy, ulong createdIn, string quoteText, + DateTime createdTimestamp) { RequestId = requestId; CreatedBy = createdBy; @@ -24,53 +28,59 @@ internal RequestQuote(int requestId, ulong createdBy, ulong createdIn, string qu QCreatedTimestamp = createdTimestamp; CreatedIn = createdIn; } - + internal static List LoadAll() { - List requests = new List(); - (MySqlDataReader dr, MySqlConnection conn) reader = DatabaseActivity.ExecuteReader("SELECT * FROM requested_quotes;"); - + var requests = new List(); + (MySqlDataReader dr, MySqlConnection conn) reader = + DatabaseActivity.ExecuteReader("SELECT * FROM requested_quotes;"); + while (reader.dr.Read()) { - RequestQuote q = new RequestQuote + var q = new RequestQuote { - RequestId = reader.dr.GetInt32("requestQuoteId"), + RequestId = reader.dr.GetInt32("requestQuoteId"), CreatedBy = reader.dr.GetUInt64("requestedBy"), QuoteText = reader.dr.GetString("quoteText"), QCreatedTimestamp = reader.dr.GetDateTime("dateCreated"), CreatedIn = reader.dr.GetUInt64("requestedIn") }; - + requests.Add(q); } - + reader.dr.Close(); reader.conn.Close(); return requests; } - - // note: remember to update the requestQuote List above! + internal static bool AddRequestQuote(string quote, ulong creatorId, ulong createdIn) { - List<(string, string)> queryParams = new List<(string id, string value)>() + List<(string, string)> queryParams = new List<(string id, string value)> { ("@requestedBy", creatorId.ToString()), ("@quoteText", quote), ("@createdIn", createdIn.ToString()) }; - int rowsUpdated = DatabaseActivity.ExecuteNonQueryCommand( + var rowsUpdated = DatabaseActivity.ExecuteNonQueryCommand( "INSERT IGNORE INTO " + "requested_quotes(requestQuoteId, requestedBy, quoteText, dateCreated, requestedIn) " + "VALUES(NULL, @requestedBy, @quoteText, CURRENT_TIMESTAMP, @createdIn);", queryParams); - (MySqlDataReader dr, MySqlConnection conn) reader = DatabaseActivity.ExecuteReader("SELECT * FROM requested_quotes ORDER BY requestQuoteId DESC LIMIT 1"); - int id = 0; - while (reader.dr.Read()) { id = reader.dr.GetInt32("requestQuoteId"); } + (MySqlDataReader dr, MySqlConnection conn) reader = + DatabaseActivity.ExecuteReader("SELECT * FROM requested_quotes ORDER BY requestQuoteId DESC LIMIT 1"); + + var id = 0; + while (reader.dr.Read()) + { + id = reader.dr.GetInt32("requestQuoteId"); + } + reader.dr.Close(); reader.conn.Close(); - + RequestQuotes.Add(new RequestQuote { RequestId = id, @@ -82,11 +92,13 @@ internal static bool AddRequestQuote(string quote, ulong creatorId, ulong create return rowsUpdated == 1; } - + internal static bool ApproveRequestQuote(int quoteId, ulong approvedBy, ulong approvedIn) { - (MySqlDataReader dr, MySqlConnection conn) reader = DatabaseActivity.ExecuteReader("SELECT * FROM requested_quotes WHERE requestQuoteId = " + quoteId + " LIMIT 1"); - RequestQuote quote = new RequestQuote(); + (MySqlDataReader dr, MySqlConnection conn) reader = + DatabaseActivity.ExecuteReader("SELECT * FROM requested_quotes WHERE requestQuoteId = " + quoteId + + " LIMIT 1"); + var quote = new RequestQuote(); while (reader.dr.Read()) { quote.RequestId = reader.dr.GetInt32("requestQuoteId"); @@ -95,22 +107,24 @@ internal static bool ApproveRequestQuote(int quoteId, ulong approvedBy, ulong ap quote.QCreatedTimestamp = reader.dr.GetDateTime("dateCreated"); quote.CreatedIn = reader.dr.GetUInt64("requestedIn"); } + reader.dr.Close(); reader.conn.Close(); - bool added = Quote.AddQuote(quote.QuoteText, quote.CreatedBy, quote.CreatedIn, approvedBy, approvedIn, quote.QCreatedTimestamp); + var added = Quote.AddQuote(quote.QuoteText, quote.CreatedBy, quote.CreatedIn, approvedBy, approvedIn, + quote.QCreatedTimestamp); RemoveRequestQuote(quote.RequestId); return added; } - + internal static bool RemoveRequestQuote(int quoteId) { RequestQuotes.Remove(RequestQuotes.Find(quote => quote.RequestId == quoteId)); - - int rowsAffected = DatabaseActivity.ExecuteNonQueryCommand( + + var rowsAffected = DatabaseActivity.ExecuteNonQueryCommand( "DELETE FROM requested_quotes WHERE requestQuoteId=" + quoteId + ";"); - + return rowsAffected == 1; } }