chaosbot/ChaosBot/Database/Repository/RaffleRepository.cs

231 lines
8.2 KiB
C#

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";
/// <summary>
/// Fetch all <c>Raffle</c>s
/// </summary>
/// <returns>List of raffles</returns>
public static Raffle[] All()
{
DataTable dataTable = Controller.SelectQuery(Table);
List<Raffle> raffles = new List<Raffle>();
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();
}
/// <summary>
/// Fetch all <c>Raffle</c>s filtered by guildId
/// </summary>
/// <param name="guildId"></param>
/// <returns>List of raffles</returns>
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();
}
/// <summary>
/// Count amount of <c>Raffle</c>s
/// </summary>
/// <returns>Amount of raffles</returns>
public static int Count()
{
DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)");
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
}
/// <summary>
/// Count amount of <c>Raffle</c>s filtered by guildId
/// </summary>
/// <param name="guildId"></param>
/// <returns>Amount of raffles</returns>
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(*)"]);
}
/// <summary>
/// Count amount of <c>Raffle</c>s filtered by guildId
/// </summary>
/// <param name="userId"></param>
/// <param name="guildId"></param>
/// <returns>Amount of raffles</returns>
public static int Count(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, "COUNT(*)", filterDict);
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
}
/// <summary>
/// Get all <c>Raffle</c>s from a user
/// </summary>
/// <param name="userId"></param>
/// <returns>List of raffles</returns>
public static Raffle[] SelectUser(string userId)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
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();
}
/// <summary>
/// Get all <c>Raffle</c>s from a user filtered by guild
/// </summary>
/// <param name="userId"></param>
/// <param name="guildId"></param>
/// <returns>List of raffles</returns>
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();
}
/// <summary>
/// Insert a <c>Raffle</c> into the database
/// </summary>
/// <param name="raffle"></param>
public static void Insert(Raffle raffle)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("userId", raffle.userId);
dict.Add("guildId", raffle.guildId);
Controller.InsertQuery(Table, dict);
}
/// <summary>
/// Insert a <c>List</c> of <c>Raffle</c>s into the database
/// </summary>
/// <param name="raffles"></param>
public static void MassInsert(List<Raffle> raffles)
{
foreach (var raf in raffles)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("userId", raf.userId);
dict.Add("guildId", raf.guildId);
Controller.InsertQuery(Table, dict);
}
}
/// <summary>
/// Pick a random raffle from the database filtered to a guild
/// </summary>
/// <param name="guildId"></param>
/// <returns>Random raffle</returns>
public static Raffle PickRandom(string guildId)
{
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;
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);
}
/// <summary>
/// Clear all <c>Raffle</c>s for a given guild
/// </summary>
/// <param name="guildId"></param>
public static void ClearRaffle(string guildId)
{
Dictionary<string,object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
Controller.DeleteQuery(Table, filterDict);
}
/// <summary>
/// Delete a <c>Raffle</c> by id
/// </summary>
/// <param name="id"></param>
public static void Delete(int id)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("id", id);
Controller.DeleteQuery(Table, filterDict);
}
}
}