using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using ChaosBot.Database.Entity; using Microsoft.Data.Sqlite; namespace ChaosBot.Database.Repository { public static class PointsRepository { private static readonly string Table = "PointsTable"; public static Points[] All() { DataTable dataTable = Controller.SelectQuery(Table); List pointslist = new List(); foreach (DataRow row in dataTable.Rows) { int id = Convert.ToInt32((long)row["id"]); int points = Convert.ToInt32(row["points"]); string userId = row["userId"].ToString(); string guildId = row["guildId"].ToString(); pointslist.Add(new Points(id, userId, guildId, points)); } return pointslist.ToArray(); } public static Points[] All(string guildId) { Dictionary filterDict = new Dictionary(); filterDict.Add("guildId", guildId); DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict); List pointslist = new List(); foreach (DataRow row in dataTable.Rows) { int idFetch = Convert.ToInt32((long)row["id"]); int pointsFetch = Convert.ToInt32(row["points"]); string userIdFetch = row["userId"].ToString(); string guildIdFetch = row["guildId"].ToString(); pointslist.Add(new Points(idFetch, userIdFetch, guildIdFetch, pointsFetch)); } return pointslist.ToArray(); } public static int Total(string userId, string guildId) { Dictionary selectfilterDict = new Dictionary(); selectfilterDict.Add("userId", userId); selectfilterDict.Add("guildId", guildId); DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict); if (Convert.ToInt32(dt.Rows.Count) != 0) return Convert.ToInt32(dt.Rows[0]["points"]); else { Dictionary dict = new Dictionary(); dict.Add("userId", userId); dict.Add("guildId", guildId); dict.Add("points", 0); Controller.InsertQuery(Table, dict); } return 0; } public static int Count(string userId, string guildId) { Dictionary selectfilterDict = new Dictionary(); selectfilterDict.Add("userId", userId); selectfilterDict.Add("guildId", guildId); DataTable dt = Controller.SelectQuery(Table, "COUNT(*)", selectfilterDict); return Convert.ToInt32(dt.Rows[0]["COUNT(*)"]); } public static int Add(string userId, int points, string guildId) { Dictionary selectfilterDict = new Dictionary(); selectfilterDict.Add("userId", userId); selectfilterDict.Add("guildId", guildId); DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict); if (Convert.ToInt32(dt.Rows.Count) != 0) { Dictionary filterDict = new Dictionary(); filterDict.Add("userId", userId); filterDict.Add("guildId", guildId); Dictionary updateDict = new Dictionary(); updateDict.Add("points", Convert.ToInt32(dt.Rows[0]["points"]) + points); Controller.UpdateQuery(Table, updateDict, filterDict); return Convert.ToInt32(dt.Rows[0]["points"]) + points; } else { Dictionary dict = new Dictionary(); dict.Add("userId", userId); dict.Add("guildId", guildId); dict.Add("points", points); Controller.InsertQuery(Table, dict); return points; } } public static int Remove(string userId, int points, string guildId) { Dictionary selectfilterDict = new Dictionary(); selectfilterDict.Add("userId", userId); selectfilterDict.Add("guildId", guildId); DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict); Dictionary filterDict = new Dictionary(); filterDict.Add("userId", userId); filterDict.Add("guildId", guildId); Dictionary updateDict = new Dictionary(); updateDict.Add("points", Convert.ToInt32(dt.Rows[0]["points"]) - points); Controller.UpdateQuery(Table, updateDict, filterDict); return Convert.ToInt32(dt.Rows[0]["points"]) - points; } public static void Delete(int userId) { Dictionary filterDict = new Dictionary(); filterDict.Add("userId", userId); Controller.DeleteQuery(Table, filterDict); } } }