Files
auth-mistox/src/Server/Services/DatabaseService/Account.cs
T
derek 72a500405a
Docker Build and Release Upload / build (push) Successful in 1m22s
Fixed database crashing on NULL returns
2025-07-26 10:21:27 -07:00

179 lines
10 KiB
C#
Executable File

using Auth.Entities;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace Auth.Services.DatabaseService {
public partial class DatabaseService {
public async Task<Account?> GetAccount( string UserNameOrEmail ) {
Account? account = null;
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT *
FROM Account
WHERE UserName = @UorE OR Email = @UorE;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@UorE", UserNameOrEmail);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) { break; }
int _id = !reader.IsDBNull("ID") ? reader.GetInt32("ID") : -1;
string _username = !reader.IsDBNull("UserName") ? reader.GetString("UserName") : "";
string _email = !reader.IsDBNull("Email") ? reader.GetString("Email") : "";
bool _emailVerified = !reader.IsDBNull("EmailVerified") ? reader.GetBoolean("EmailVerified") : false;
string _passwordhash = !reader.IsDBNull("PasswordHash") ? reader.GetString("PasswordHash") : "";
bool _failedpasswordlock = !reader.IsDBNull("FailedPasswordLock") ? reader.GetBoolean("FailedPasswordLock") : false;
int _passwordattempts = !reader.IsDBNull("PasswordAttempts") ? reader.GetInt32("PasswordAttempts") : -1;
int _curpasswordattempts = !reader.IsDBNull("CurrentPasswordAttempts") ? reader.GetInt32("CurrentPasswordAttempts") : -1;
string _role = !reader.IsDBNull("Role") ? reader.GetString("Role") : "";
string _emailtoken = !reader.IsDBNull("EmailToken") ? reader.GetString("EmailToken") : "";
DateTime _emailtokencreated = !reader.IsDBNull("EmailTokenCreation") ? reader.GetDateTime("EmailTokenCreation") : DateTime.MinValue;
string _passwordtoken = !reader.IsDBNull("PasswordToken") ? reader.GetString("PasswordToken") : "";
DateTime _passwordtokencreated = !reader.IsDBNull("PasswordTokenCreation") ? reader.GetDateTime("PasswordTokenCreation") : DateTime.MinValue;
string _dataserver = !reader.IsDBNull("DataServer") ? reader.GetString("DataServer") : "";
account = new Account() {
ID = _id,
UserName = _username,
Email = _email,
EmailVerified = _emailVerified,
PasswordHash = _passwordhash,
CurrentPasswordAttempts = _curpasswordattempts,
PasswordAttempts = _passwordattempts,
EmailToken = _emailtoken,
EmailTokenCreated = _emailtokencreated,
PasswordToken = _passwordtoken,
PasswordTokenCreated = _passwordtokencreated,
FailedPasswordLock = _failedpasswordlock,
Role = _role,
DataServer = _dataserver
};
}
}
}
return account;
}
public async Task<Account?> GetAccount( int AccountID ) {
Account? account = null;
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT *
FROM Account
WHERE ID = @ID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@ID", AccountID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = !reader.IsDBNull("ID") ? reader.GetInt32("ID") : -1;
string _username = !reader.IsDBNull("UserName") ? reader.GetString("UserName") : "";
string _email = !reader.IsDBNull("Email") ? reader.GetString("Email") : "";
bool _emailVerified = !reader.IsDBNull("EmailVerified") ? reader.GetBoolean("EmailVerified") : false;
string _passwordhash = !reader.IsDBNull("PasswordHash") ? reader.GetString("PasswordHash") : "";
bool _failedpasswordlock = !reader.IsDBNull("FailedPasswordLock") ? reader.GetBoolean("FailedPasswordLock") : false;
int _passwordattempts = !reader.IsDBNull("PasswordAttempts") ? reader.GetInt32("PasswordAttempts") : -1;
int _curpasswordattempts = !reader.IsDBNull("CurrentPasswordAttempts") ? reader.GetInt32("CurrentPasswordAttempts") : -1;
string _role = !reader.IsDBNull("Role") ? reader.GetString("Role") : "";
string _emailtoken = !reader.IsDBNull("EmailToken") ? reader.GetString("EmailToken") : "";
DateTime _emailtokencreated = !reader.IsDBNull("EmailTokenCreation") ? reader.GetDateTime("EmailTokenCreation") : DateTime.MinValue;
string _passwordtoken = !reader.IsDBNull("PasswordToken") ? reader.GetString("PasswordToken") : "";
DateTime _passwordtokencreated = !reader.IsDBNull("PasswordTokenCreation") ? reader.GetDateTime("PasswordTokenCreation") : DateTime.MinValue;
string _dataserver = !reader.IsDBNull("DataServer") ? reader.GetString("DataServer") : "";
account = new Account() {
ID = _id,
UserName = _username,
Email = _email,
EmailVerified = _emailVerified,
PasswordHash = _passwordhash,
CurrentPasswordAttempts = _passwordattempts,
PasswordAttempts = _passwordattempts,
EmailToken = _emailtoken,
EmailTokenCreated = _emailtokencreated,
PasswordToken = _passwordtoken,
PasswordTokenCreated = _passwordtokencreated,
FailedPasswordLock = _failedpasswordlock,
Role = _role,
DataServer = _dataserver
};
}
}
}
return account;
}
public async Task SetAccount( Account Profile ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
INSERT INTO Account
(ID,UserName,Email,EmailVerified,PasswordHash,FailedPasswordLock,PasswordAttempts,CurrentPasswordAttempts,Role,EmailToken,EmailTokenCreation,PasswordToken,PasswordTokenCreation,DataServer)
VALUES
(@ID,@UserName,@Email,@EmailVerified,@PasswordHash,@FailedPasswordLock,@PasswordAttempts,@CurrentPasswordAttempts,@Role,@EmailToken,@EmailTokenCreation,@PasswordToken,@PasswordTokenCreation,@DataServer)
ON DUPLICATE KEY UPDATE
UserName = @UserName,
Email = @Email,
EmailVerified = @EmailVerified,
PasswordHash = @PasswordHash,
FailedPasswordLock = @FailedPasswordLock,
PasswordAttempts = @PasswordAttempts,
CurrentPasswordAttempts = @CurrentPasswordAttempts,
Role = @Role,
EmailToken = @EmailToken,
EmailTokenCreation = @EmailTokenCreation,
PasswordToken = @PasswordToken,
PasswordTokenCreation = @PasswordTokenCreation,
DataServer = @DataServer;
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@ID", Profile.ID);
cmd.Parameters.AddWithValue("@UserName", Profile.UserName);
cmd.Parameters.AddWithValue("@Email", Profile.Email);
cmd.Parameters.AddWithValue("@EmailVerified", Profile.EmailVerified);
cmd.Parameters.AddWithValue("@PasswordHash", Profile.PasswordHash);
cmd.Parameters.AddWithValue("@FailedPasswordLock", Profile.FailedPasswordLock);
cmd.Parameters.AddWithValue("@PasswordAttempts", Profile.PasswordAttempts);
cmd.Parameters.AddWithValue("@CurrentPasswordAttempts", Profile.CurrentPasswordAttempts);
cmd.Parameters.AddWithValue("@Role", Profile.Role);
cmd.Parameters.AddWithValue("@EmailToken", Profile.EmailToken);
cmd.Parameters.AddWithValue("@EmailTokenCreation", Profile.EmailTokenCreated);
cmd.Parameters.AddWithValue("@PasswordToken", Profile.PasswordToken);
cmd.Parameters.AddWithValue("@PasswordTokenCreation", Profile.PasswordTokenCreated);
cmd.Parameters.AddWithValue("@DataServer", Profile.DataServer);
await cmd.ExecuteNonQueryAsync();
}
}
public async Task DeleteAccount( int AccountID ) {
using( MySqlConnection connection = GetConnection() ) {
MySqlCommand cmd;
connection.Open();
string command = @"
DELETE FROM Account WHERE ID = @ID;
";
cmd = new MySqlCommand( command, connection );
cmd.Parameters.AddWithValue("@ID", AccountID);
await cmd.ExecuteNonQueryAsync();
}
}
}
}