Skip to content

Commit

Permalink
Cleaned up Quote & RequestQuote
Browse files Browse the repository at this point in the history
  • Loading branch information
MythicalCuddles committed Oct 19, 2019
1 parent 66badbb commit 3b01f63
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 68 deletions.
91 changes: 53 additions & 38 deletions DiscordBot/Objects/Quote.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
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
{
public class Quote
{
internal static List<Quote> 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;
Expand All @@ -30,15 +30,15 @@ internal Quote(int quoteId, ulong createdBy, ulong acceptedBy, string quote, Dat
CreatedIn = createdIn;
AcceptedIn = acceptedIn;
}

internal static List<Quote> LoadAll()
{
List<Quote> quotes = new List<Quote>();
var quotes = new List<Quote>();
(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"),
Expand All @@ -51,39 +51,54 @@ internal static List<Quote> 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()),
("@quoteText", quote),
("@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,
Expand All @@ -94,32 +109,32 @@ 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;
}

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;
}
}
Expand Down
74 changes: 44 additions & 30 deletions DiscordBot/Objects/RequestQuote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,79 @@ namespace DiscordBot.Objects
public class RequestQuote
{
internal static List<RequestQuote> 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;
QuoteText = quoteText;
QCreatedTimestamp = createdTimestamp;
CreatedIn = createdIn;
}

internal static List<RequestQuote> LoadAll()
{
List<RequestQuote> requests = new List<RequestQuote>();
(MySqlDataReader dr, MySqlConnection conn) reader = DatabaseActivity.ExecuteReader("SELECT * FROM requested_quotes;");

var requests = new List<RequestQuote>();
(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,
Expand All @@ -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");
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 3b01f63

Please sign in to comment.