using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ChaosBot.Database.Entity;
using Microsoft.Data.Sqlite;
namespace ChaosBot.Database.Repository
{
public static class RaffleRepository
{
private static readonly string Table = "RaffleTable";
///
/// Fetch all Raffles
///
/// List of raffles
public static Raffle[] All()
{
DataTable dataTable = Controller.SelectQuery(Table);
List raffles = new List();
foreach (DataRow row in dataTable.Rows)
{
int id = Convert.ToInt32((long) row["id"]);
string userId = row["userId"].ToString();
string guildId = row["guildId"].ToString();
raffles.Add(new Raffle(id, userId, guildId));
}
return raffles.ToArray();
}
///
/// Fetch all Raffles filtered by guildId
///
///
/// List of raffles
public static Raffle[] All(string guildId)
{
Dictionary filterDict = new Dictionary();
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict);
List raffles = new List();
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();
}
///
/// Count amount of Raffles
///
/// Amount of raffles
public static int Count()
{
DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)");
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
}
///
/// Count amount of Raffles filtered by guildId
///
///
/// Amount of raffles
public static int Count(string guildId)
{
Dictionary filterDict = new Dictionary();
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)", filterDict);
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
}
///
/// Count amount of Raffles filtered by guildId
///
///
///
/// Amount of raffles
public static int Count(string userId, string guildId)
{
Dictionary filterDict = new Dictionary();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)", filterDict);
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
}
///
/// Get all Raffles from a user
///
///
/// List of raffles
public static Raffle[] SelectUser(string userId)
{
Dictionary filterDict = new Dictionary();
filterDict.Add("userId", userId);
DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict);
List raffles = new List();
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();
}
///
/// Get all Raffles from a user filtered by guild
///
///
///
/// List of raffles
public static Raffle[] SelectUser(string userId, string guildId)
{
Dictionary filterDict = new Dictionary();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict);
List raffles = new List();
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();
}
///
/// Insert a Raffle into the database
///
///
public static void Insert(Raffle raffle)
{
Dictionary dict = new Dictionary();
dict.Add("userId", raffle.userId);
dict.Add("guildId", raffle.guildId);
Controller.InsertQuery(Table, dict);
}
///
/// Insert a List of Raffles into the database
///
///
public static void MassInsert(List raffles)
{
foreach (var raf in raffles)
{
Dictionary dict = new Dictionary();
dict.Add("userId", raf.userId);
dict.Add("guildId", raf.guildId);
Controller.InsertQuery(Table, dict);
}
}
///
/// Pick a random raffle from the database filtered to a guild
///
///
/// Random raffle
public static Raffle PickRandom(string guildId)
{
Dictionary filterDict = new Dictionary();
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "*", filterDict, "RANDOM()");
if (dataTable.Rows.Count == 0) return null;
DataRow row = dataTable.Rows[0];
int idFetch = Convert.ToInt32((long)row["id"]);
string userIdFetch = row["userId"].ToString();
string guildIdFetch = row["guildId"].ToString();
return new Raffle(idFetch, userIdFetch, guildIdFetch);
}
///
/// Clear all Raffles for a given guild
///
///
public static void ClearRaffle(string guildId)
{
Dictionary filterDict = new Dictionary();
filterDict.Add("guildId", guildId);
Controller.DeleteQuery(Table, filterDict);
}
///
/// Delete a Raffle by id
///
///
public static void Delete(int id)
{
Dictionary filterDict = new Dictionary();
filterDict.Add("id", id);
Controller.DeleteQuery(Table, filterDict);
}
}
}