Apply guildId to all repository methods

This commit is contained in:
Daniel_I_Am 2020-06-04 03:15:21 +02:00
parent 344871d392
commit 0354224f46
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
3 changed files with 80 additions and 19 deletions

View File

@ -4,14 +4,15 @@ namespace ChaosBot.Database.Entity
{ {
public int id { get; } public int id { get; }
public string userId { get; private set; } public string userId { get; private set; }
public string guildId { get; private set; }
public Raffle(int id, string userId) public Raffle(int id, string userId, string guildId)
{ {
this.id = id; this.id = id;
this.userId = userId; this.userId = userId;
} }
public Raffle(string userId) public Raffle(string userId, string guildId)
{ {
this.userId = userId; this.userId = userId;
} }

View File

@ -20,8 +20,29 @@ namespace ChaosBot.Database.Repository
{ {
int id = Convert.ToInt32((long) row["id"]); int id = Convert.ToInt32((long) row["id"]);
string userId = row["userId"].ToString(); string userId = row["userId"].ToString();
string guildId = row["guildId"].ToString();
raffles.Add(new Raffle(id, userId)); raffles.Add(new Raffle(id, userId, guildId));
}
return raffles.ToArray();
}
public static Raffle[] All(string guildId)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict);
List<Raffle> raffles = new List<Raffle>();
foreach (DataRow row in dataTable.Rows)
{
int idFetch = Convert.ToInt32((long) row["id"]);
string userIdFetch = row["userId"].ToString();
string guildIdFetch = row["guildId"].ToString();
raffles.Add(new Raffle(idFetch, userIdFetch, guildIdFetch));
} }
return raffles.ToArray(); return raffles.ToArray();
@ -34,17 +55,28 @@ namespace ChaosBot.Database.Repository
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]); return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
} }
public static int Count(string userId) public static int Count(string guildId)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)", filterDict);
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
}
public static int Count(string userId, string guildId)
{ {
Dictionary<string, object> filterDict = new Dictionary<string, object>(); Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId); filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)", filterDict); DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)", filterDict);
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]); return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
} }
public static Raffle[] selectUser(string userId) public static Raffle[] SelectUser(string userId)
{ {
Dictionary<string, object> filterDict = new Dictionary<string, object>(); Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId); filterDict.Add("userId", userId);
@ -56,8 +88,30 @@ namespace ChaosBot.Database.Repository
{ {
int id = Convert.ToInt32((long) row["id"]); int id = Convert.ToInt32((long) row["id"]);
string userIdFetch = row["userId"].ToString(); string userIdFetch = row["userId"].ToString();
string guildIdFetch = row["guildId"].ToString();
raffles.Add(new Raffle(id, userIdFetch)); raffles.Add(new Raffle(id, userIdFetch, guildIdFetch));
}
return raffles.ToArray();
}
public static Raffle[] SelectUser(string userId, string guildId)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict);
List<Raffle> raffles = new List<Raffle>();
foreach (DataRow row in dataTable.Rows)
{
int id = Convert.ToInt32((long) row["id"]);
string userIdFetch = row["userId"].ToString();
string guildIdFetch = row["guildId"].ToString();
raffles.Add(new Raffle(id, userIdFetch, guildIdFetch));
} }
return raffles.ToArray(); return raffles.ToArray();
@ -68,6 +122,7 @@ namespace ChaosBot.Database.Repository
Dictionary<string, object> dict = new Dictionary<string, object>(); Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("userId", raffle.userId); dict.Add("userId", raffle.userId);
dict.Add("guildId", raffle.guildId);
Controller.InsertQuery(Table, dict); Controller.InsertQuery(Table, dict);
} }
@ -79,29 +134,34 @@ namespace ChaosBot.Database.Repository
Dictionary<string, object> dict = new Dictionary<string, object>(); Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("userId", raf.userId); dict.Add("userId", raf.userId);
dict.Add("guildId", raf.guildId);
Controller.InsertQuery(Table, dict); Controller.InsertQuery(Table, dict);
} }
} }
public static Raffle PickRandom() public static Raffle PickRandom(string guildId)
{ {
DataTable dataTable = Controller.SelectQuery(Table, "*", orderByKey: "RANDOM()"); Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "*", filterDict, "RANDOM()");
if (dataTable.Rows.Count == 0) return null; if (dataTable.Rows.Count == 0) return null;
DataRow row = dataTable.Rows[0]; DataRow row = dataTable.Rows[0];
int idFetch = Convert.ToInt32((long)row["id"]); int idFetch = Convert.ToInt32((long)row["id"]);
string userIdFetch = row["userId"].ToString(); string userIdFetch = row["userId"].ToString();
return new Raffle(idFetch, userIdFetch); string guildIdFetch = row["guildId"].ToString();
return new Raffle(idFetch, userIdFetch, guildIdFetch);
} }
public static int ClearRaffle() public static void ClearRaffle(string guildId)
{ {
List<SqliteCommand> cmds = new List<SqliteCommand>(); Dictionary<string,object> filterDict = new Dictionary<string, object>();
cmds.Add(new SqliteCommand("delete from RaffleTable;")); filterDict.Add("guildId", guildId);
cmds.Add(new SqliteCommand("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE name = 'RaffleTable';"));
Controller.DeleteQuery(Table, filterDict);
return Controller.TransactionQuery(cmds);
} }
public static void Delete(int id) public static void Delete(int id)

View File

@ -152,23 +152,23 @@ namespace ChaosBot.Discord.Modules
sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to <@{userId}>."); sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to <@{userId}>.");
sb.AppendLine(); sb.AppendLine();
sb.AppendLine($"<@{userId}> now has {RaffleRepository.Count(userId.ToString())} rafflepoints!"); sb.AppendLine($"<@{userId}> now has {RaffleRepository.Count(userId.ToString(), Context.Guild.Id.ToString())} rafflepoints!");
} }
private void PickRaffle(StringBuilder sb) private void PickRaffle(StringBuilder sb)
{ {
Raffle winner = RaffleRepository.PickRandom(Context.Guild.Id.ToString()); Raffle winner = RaffleRepository.PickRandom(Context.Guild.Id.ToString());
RaffleRepository.Delete(winner.id, Context.Guild.Id.ToString()); RaffleRepository.Delete(winner.id);
sb.Append($"<@{winner.userId}> has won the raffle!"); sb.Append($"<@{winner.userId}> has won the raffle!");
} }
private void ClearRaffle(StringBuilder sb, bool noOutput = false) private void ClearRaffle(StringBuilder sb, bool noOutput = false)
{ {
int Removed = RaffleRepository.Count(); int removed = RaffleRepository.Count();
RaffleRepository.ClearRaffle(Context.Guild.Id.ToString()); RaffleRepository.ClearRaffle(Context.Guild.Id.ToString());
sb.AppendLine($"{Context.User.Mention} has cleared all {Removed} rafflepoints"); sb.AppendLine($"{Context.User.Mention} has cleared all {removed} rafflepoints");
} }
private void StatusRaffle(StringBuilder sb, string user) private void StatusRaffle(StringBuilder sb, string user)