chaosbot/ChaosBot/Database/Repository/PointsRepository.cs
2020-06-04 22:25:53 -04:00

146 lines
5.1 KiB
C#

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<Points> pointslist = new List<Points>();
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<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict);
List<Points> pointslist = new List<Points>();
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<string, object> selectfilterDict = new Dictionary<string, object>();
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<string, object> dict = new Dictionary<string, object>();
dict.Add("userId", userId);
dict.Add("guildId", guildId);
dict.Add("points", 0);
Controller.InsertQuery(Table, dict);
}
return 0;
}
public static int Add(int userId, int points, int guildId)
{
Dictionary<string, object> selectfilterDict = new Dictionary<string, object>();
selectfilterDict.Add("userId", userId);
selectfilterDict.Add("guildId", guildId);
int curPoints = Convert.ToInt32(Controller.SelectQuery(Table, "points", selectfilterDict).Rows[0]["points"]);
Console.WriteLine($"{curPoints}");
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
Dictionary<string, object> updateDict = new Dictionary<string, object>();
// updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) + points);
return 0;
//Controller.UpdateQuery(Table, updateDict, filterDict);
//return Convert.ToInt32(dataTable.Rows[0]["points"]) + points;
}
public static void ClearPoints(string guildId)
{
Dictionary<string,object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
Controller.DeleteQuery(Table, filterDict);
}
public static void Delete(int userId)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
Controller.DeleteQuery(Table, filterDict);
}
public static int Remove(int userId, int points, int guildId )
{
Dictionary<string, object> selectfilterDict = new Dictionary<string, object>();
selectfilterDict.Add("userId", userId);
selectfilterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "points", selectfilterDict);
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
Dictionary<string, object> updateDict = new Dictionary<string, object>();
updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) - points);
// Controller.UpdateQuery(Table, updateDict, filterDict);
return Convert.ToInt32(dataTable.Rows[0]["points"]) - points;
}
}
}