add auto module fetch on startup and finished module creation form for real now

This commit is contained in:
Rene Schwarz
2021-08-09 15:08:26 +02:00
parent f1f717a37f
commit cdb86331e5
30 changed files with 269 additions and 124 deletions

View File

@@ -5,6 +5,7 @@ using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace Server_Dashboard {
@@ -65,12 +66,77 @@ namespace Server_Dashboard {
//Open the connection
con.Open();
//SQL Query
string query = "SELECT UserID, Username, Email, CreationTime, ModuleName, MI.Image FROM UserData LEFT JOIN ModuleData MD on UserData.ID = MD.UserID LEFT JOIN ModuleIcon MI on MD.ID = MI.Module WHERE Username = @username";
string query = "SELECT ID, Username, Email, RegistrationDate FROM UserData WHERE Username = @username";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@username", username);
//Execute query and return number of rows affected
DataTable resultTable = new DataTable() { TableName = "Userdata" };
using SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(resultTable);
return resultTable;
//Checks if there are any rows successful
//If the query returns 0 the query wasn't successful
//if its any number above 0 it was successfull
//Catch any error
} catch (SqlException ex) {
return null;
} finally {
//Always close the connection
con.Close();
}
}
public static DataTable GetUserModuleData(int uid) {
//Creates the database connection
using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString);
try {
//Open the connection
con.Open();
//SQL Query
string query = "SELECT Creator, CreationTime, ModuleName, MI.Image FROM ModuleData LEFT JOIN ModuleIcon MI on ModuleData.ID = MI.Module WHERE UserID = @userID";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@userID", uid);
//Execute query and return number of rows affected
DataTable resultTable = new DataTable();
using SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(resultTable);
return resultTable;
//Checks if there are any rows successful
//If the query returns 0 the query wasn't successful
//if its any number above 0 it was successfull
//Catch any error
} catch (SqlException ex) {
return null;
} finally {
//Always close the connection
con.Close();
}
}
/// <summary>
/// This function will fetch every Serverdata for each module
/// This will need some optimization, for now we just asynchronously
/// fetch the serverdata for each module
/// </summary>
/// <param name="mid">ModuleID to fetch the data from</param>
/// <returns></returns>
public static DataTable GetServerData(string mid) {
//Creates the database connection
using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString);
try {
//Open the connection
con.Open();
//SQL Query
string query = "SELECT * FROM ServerData WHERE ModuleID = @mid";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@mid", mid);
//Execute query and return number of rows affected
DataTable resultTable = new DataTable();
using SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(resultTable);
@@ -113,7 +179,10 @@ namespace Server_Dashboard {
com.Parameters.AddWithValue("@time", DateTime.Now);
com.Parameters.AddWithValue("@moduleName", moduleName);
com.Parameters.AddWithValue("@serverName", serverName);
com.Parameters.Add("@moduleIcon", SqlDbType.VarBinary, int.MaxValue).Value = moduleIcon;
com.Parameters.Add("@moduleIcon", SqlDbType.VarBinary, -1).Value = moduleIcon;
if (moduleIcon == null)
com.Parameters["@moduleIcon"].Value = DBNull.Value;
//com.Parameters.AddWithValue("@moduleIcon", moduleIcon);
com.Parameters.AddWithValue("@ipAdress", ipAdress);
com.Parameters.AddWithValue("@port", port);
//Execute query and return number of rows affected
@@ -150,21 +219,19 @@ namespace Server_Dashboard {
//Open the connection
con.Open();
//SQL Query
string query = "EXEC CheckUserCookie @Cookie = @cookie, @UserName = @username, @Valid = @valid OUTPUT";
string query = "((SELECT Cookie FROM UserData WHERE Username = @username) = @cookie)";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);
//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@cookie", cookie);
com.Parameters.AddWithValue("@username", username);
com.Parameters.Add("@valid", SqlDbType.Bit);
com.Parameters["@valid"].Direction = ParameterDirection.Output;
//Execute query and return number of rows affected
int sqlResponse = com.ExecuteNonQuery();
//Checks if there are any rows successful
//If the query returns 0 the query wasn't successful
//if its any number above 0 it was successfull
if ((int)com.Parameters["@Valid"].Value == 0) {
if (sqlResponse == 0) {
//Error, not successful
return 1;
} else {
@@ -191,7 +258,7 @@ namespace Server_Dashboard {
//Open the connection
con.Open();
//SQL Query
string query = "EXEC DeleteUserCookie @Username = @username";
string query = "UPDATE UserData SET Cookie = null WHERE Username = @username";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);
//For security reasons the values are added with this function
@@ -224,19 +291,19 @@ namespace Server_Dashboard {
/// <param name="cookie">The delicious locally stored cookie</param>
/// <param name="username">The User who deserves a cookie :3</param>
/// <returns>[0] is false, [1] is true, [2] connection error</returns>
public static int AddCookie(string cookie, string username) {
public static int AddCookie(string username, string cookie) {
//Creates the database connection
using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString);
try {
//Open the connection
con.Open();
//SQL Query
string query = "EXEC AddCookieToUser @Cookie = @cookie, @UserName = @username";
string query = "UPDATE UserData SET Cookie = @cookie WHERE Username = @username";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);
//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@cookie", cookie);
com.Parameters.Add("@cookie", SqlDbType.NVarChar, -1).Value = cookie;
com.Parameters.AddWithValue("@username", username);
//Execute query and return number of rows affected
int sqlResponse = com.ExecuteNonQuery();