115 lines
3.7 KiB
C#
115 lines
3.7 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";
|
|
|
|
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();
|
|
|
|
raffles.Add(new Raffle(id, userId));
|
|
}
|
|
|
|
return raffles.ToArray();
|
|
}
|
|
|
|
public static int Count()
|
|
{
|
|
DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)");
|
|
|
|
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
|
|
}
|
|
|
|
public static int Count(string userId)
|
|
{
|
|
Dictionary<string, object> filterDict = new Dictionary<string, object>();
|
|
filterDict.Add("userId", userId);
|
|
|
|
DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)", filterDict);
|
|
|
|
return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]);
|
|
}
|
|
|
|
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();
|
|
|
|
raffles.Add(new Raffle(id, userIdFetch));
|
|
}
|
|
|
|
return raffles.ToArray();
|
|
}
|
|
|
|
public static void Insert(Raffle raffle)
|
|
{
|
|
Dictionary<string, object> dict = new Dictionary<string, object>();
|
|
|
|
dict.Add("userId", raffle.userId);
|
|
|
|
Controller.InsertQuery(Table, dict);
|
|
}
|
|
|
|
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);
|
|
Controller.InsertQuery(Table, dict);
|
|
}
|
|
}
|
|
|
|
public static Raffle PickRandom()
|
|
{
|
|
DataTable dataTable = Controller.SelectQuery(Table, "*", orderByKey: "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();
|
|
return new Raffle(idFetch, userIdFetch);
|
|
}
|
|
|
|
public static int ClearRaffle()
|
|
{
|
|
List<SqliteCommand> cmds = new List<SqliteCommand>();
|
|
|
|
cmds.Add(new SqliteCommand("delete from RaffleTable;"));
|
|
cmds.Add(new SqliteCommand("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE name = 'RaffleTable';"));
|
|
|
|
return Controller.TransactionQuery(cmds);
|
|
}
|
|
|
|
public static void Delete(int id)
|
|
{
|
|
Dictionary<string, object> filterDict = new Dictionary<string, object>();
|
|
filterDict.Add("id", id);
|
|
|
|
Controller.DeleteQuery(Table, filterDict);
|
|
}
|
|
}
|
|
} |