Init Commit

This commit is contained in:
2025-06-16 17:04:55 -07:00
commit d21315cf32
43 changed files with 3089 additions and 0 deletions
@@ -0,0 +1,194 @@
using MistoxWebsite.Shared;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace MistoxWebsite.Server.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
Left Join WebsiteData
On Account.ID = WebsiteData.AccountID
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.GetInt32("ID");
string _username = reader.GetString("UserName");
string _email = reader.GetString("Email");
bool _emailVerified = reader.GetBoolean("EmailVerified");
string _passwordhash = reader.GetString("PasswordHash");
bool _failedpasswordlock = reader.GetBoolean( "FailedPasswordLock" );
int _passwordattempts = reader.GetInt32( "PasswordAttempts" );
int _curpasswordattempts = reader.GetInt32( "CurrentPasswordAttempts" );
string _role = reader.GetString( "Role" );
string _emailtoken = reader.GetString( "EmailToken" );
account = new Account() {
ID = _id,
UserName = _username,
Email = _email,
EmailVerified = _emailVerified,
PasswordHash = _passwordhash,
SiteData = new WebSiteData() {
AccountID = _id,
CurrentPasswordAttempts = _curpasswordattempts,
PasswordAttempts = _passwordattempts,
EmailToken = _emailtoken,
FailedPasswordLock = _failedpasswordlock,
Role = _role,
}
};
}
}
}
return account;
}
public async Task<Account?> GetAccountByID( int ID ) {
Account? account = null;
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT *
FROM Account
Left Join WebsiteData
On Account.ID = WebsiteData.AccountID
WHERE ID = @ID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@ID", ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = reader.GetInt32("ID");
string _username = reader.GetString("UserName");
string _email = reader.GetString("Email");
bool _emailVerified = reader.GetBoolean("EmailVerified");
string _passwordhash = reader.GetString("PasswordHash");
bool _failedpasswordlock = reader.GetBoolean( "FailedPasswordLock" );
int _passwordattempts = reader.GetInt32( "PasswordAttempts" );
int _curpasswordattempts = reader.GetInt32( "CurrentPasswordAttempts" );
string _role = reader.GetString( "Role" );
string _emailtoken = reader.GetString( "EmailToken" );
account = new Account() {
ID = _id,
UserName = _username,
Email = _email,
EmailVerified = _emailVerified,
PasswordHash = _passwordhash,
SiteData = new WebSiteData() {
AccountID = _id,
CurrentPasswordAttempts = _passwordattempts,
PasswordAttempts = _passwordattempts,
EmailToken = _emailtoken,
FailedPasswordLock = _failedpasswordlock,
Role = _role,
}
};
}
}
}
return account;
}
public async Task SetAccount( Account Update ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
UPDATE Account SET
UserName = @UserName,
Email = @Email,
EmailVerified = @EmailVerified,
PasswordHash = @PasswordHash
WHERE ID = @ID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@UserName", Update.UserName);
cmd.Parameters.AddWithValue("@Email", Update.Email);
cmd.Parameters.AddWithValue("@EmailVerified", Update.EmailVerified);
cmd.Parameters.AddWithValue("@PasswordHash", Update.PasswordHash);
cmd.Parameters.AddWithValue("@ID", Update.ID);
await cmd.ExecuteNonQueryAsync();
await UpdateWebsiteData( Update, Update.SiteData );
}
}
public async Task NewAccount( Account Profile ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
int EmailVer = Profile.EmailVerified ? 1 : 0;
string command = @"
INSERT INTO Account
(UserName,Email,EmailVerified,PasswordHash)
VALUES
(@UserName,@Email,@EmailVerified,@PasswordHash);
SELECT ID FROM Account
WHERE UserName = @UserName;
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@UserName", Profile.UserName);
cmd.Parameters.AddWithValue("@Email", Profile.Email);
cmd.Parameters.AddWithValue("@EmailVerified", Profile.EmailVerified);
cmd.Parameters.AddWithValue("@PasswordHash", Profile.PasswordHash);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = reader.GetInt32("ID");
Profile.ID = _id;
}
}
await NewWebsiteData( Profile, Profile.SiteData );
}
}
public async Task DeleteAccount( Account Profile ) {
using( MySqlConnection connection = GetConnection() ) {
MySqlCommand cmd;
connection.Open();
string command = @"
DELETE FROM Account WHERE ID = @ID;
DELETE FROM AccountInventory WHERE AccountID = @ID;
DELETE FROM ProjectMistData WHERE AccountID = @ID;
DELETE FROM Cart WHERE AccountID = @ID;
DELETE FROM WebsiteData WHERE AccountID = @ID;
";
cmd = new MySqlCommand( command, connection );
cmd.Parameters.AddWithValue("@ID", Profile.ID);
await cmd.ExecuteNonQueryAsync();
}
}
}
}
@@ -0,0 +1,116 @@
using MistoxWebsite.Shared;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
// Account inventory needs to know whether there is already an object with the specified PK before making a new item
// If item exists already update the one that already exists
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
public async Task<List<UserInventory>> GetInventory( Account account, Product product ) {
List<UserInventory> list = new List<UserInventory>();
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROM AccountInventory
WHERE AccountID = @AccountID AND ProductID = @ProductID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
cmd.Parameters.AddWithValue("@ProductID", product.ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
string _item = reader.GetString("Item");
int _quantity = reader.GetInt32("Quantity");
string _stats = reader.GetString("Stats");
list.Add( new UserInventory() {
Item = _item,
Quantity = _quantity,
Stats = _stats
} );
}
}
}
return list;
}
async Task UpdateInventory( MySqlConnection connection, AccountInventory item ) {
string command = @"
UPDATE AccountInventory
SET AccountID = @AccountID,
ProductID = @ProductID,
Item = @Item,
Quantity = @Quantity,
Stats = @Stats
WHERE (AccountID = @AccountID AND ProductID = @ProductID AND Item = @Item);
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", item.AccountID);
cmd.Parameters.AddWithValue("@ProductID", item.ProductID);
cmd.Parameters.AddWithValue("@Item", item.Item);
cmd.Parameters.AddWithValue("@Quantity", item.Quantity);
cmd.Parameters.AddWithValue("@Stats", item.Stats);
await cmd.ExecuteNonQueryAsync();
}
async Task NewInventory( MySqlConnection connection, AccountInventory item ) {
string command = @"
INSERT INTO AccountInventory (AccountID, ProductID, Item, Quantity, Stats)
VALUES
(@AccountID, @ProductID, @Item, @Quantity, @Stats);
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@AccountID", item.AccountID);
cmd.Parameters.AddWithValue("@ProductID", item.ProductID);
cmd.Parameters.AddWithValue("@Item", item.Item);
cmd.Parameters.AddWithValue("@Quantity", item.Quantity);
cmd.Parameters.AddWithValue("@Stats", item.Stats);
await cmd.ExecuteNonQueryAsync();
}
// Test to see if reader read does what its supposed to
// Not fully implimented
public async Task SetInventory( Account account, Product game, List<UserInventory> Item ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
foreach( UserInventory item in Item ) {
bool exists = false;
MySqlCommand cmd = new MySqlCommand("SELECT * FROM AccountInventory WHERE AccountID = '" + account.ID + "' AND ProductID = '" + game.ID + "' AND Item = '" + item.Item.ToLower() + "'", connection);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
exists = reader.HasRows;
}
if( exists ) {
await UpdateInventory( connection, new AccountInventory() {
AccountID = account.ID,
ProductID = game.ID,
Item = item.Item,
Quantity = item.Quantity,
Stats = item.Stats
} );
} else {
await NewInventory( connection, new AccountInventory() {
AccountID = account.ID,
ProductID = game.ID,
Item = item.Item,
Quantity = item.Quantity,
Stats = item.Stats
} );
}
}
}
}
}
}
+82
View File
@@ -0,0 +1,82 @@
using MistoxWebsite.Shared;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
public async Task<List<Cart>> GetCart( Account account ) {
List<Cart> list = new List<Cart>();
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROM Cart
WHERE AccountID = @AccountID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = reader.GetInt32("ID");
int _accountid = reader.GetInt32("AccountID");
int _productid = reader.GetInt32("ProductID");
list.Add( new Cart() {
ID = _id,
AccountID = _accountid,
ProductID = _productid
} );
}
}
}
return list;
}
public async Task AddToCart( Cart item ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
INSERT INTO Cart
(AccountID, ProductID)
VALUES
(@AccountID, @ProductID);
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@AccountID", item.AccountID);
cmd.Parameters.AddWithValue("@ProductID", item.ProductID);
await cmd.ExecuteNonQueryAsync();
}
}
public async Task RemoveFromCart( Cart item ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = "DELETE FROM Cart WHERE AccountID=" + item.AccountID + " AND ProductID=" + item.ProductID + ";";
MySqlCommand cmd = new MySqlCommand( command , connection);
await cmd.ExecuteNonQueryAsync();
}
}
public async Task ClearCart( Account account ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
DELETE FROM Cart
WHERE AccountID = @AccountID;
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
await cmd.ExecuteNonQueryAsync();
}
}
}
}
@@ -0,0 +1,15 @@
using MySql.Data.MySqlClient;
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
public string ConnectionString {
get; set;
}
public DatabaseService( string connectionString ) {
ConnectionString = connectionString;
}
MySqlConnection GetConnection() {
return new MySqlConnection( ConnectionString );
}
}
}
@@ -0,0 +1,122 @@
using MistoxWebsite.Shared;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
public async Task<PageLoadObject> getPageLoadObject( int AccountID ) {
PageLoadObject account = new PageLoadObject();
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROM Account
INNER JOIN WebsiteData
ON Account.ID = WebsiteData.AccountID
WHERE ID = @AccountID
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", AccountID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = reader.GetInt32(0);
string _username = reader.GetString(1);
string _email = reader.GetString(2);
bool _emailVerified = reader.GetBoolean(3);
string _passwordhash = reader.GetString(4);
bool _failedPasswordLock = reader.GetBoolean(6);
int _passwordAttempts = reader.GetInt32(7);
int _currentPasswordAttempts = reader.GetInt32(8);
string _role = reader.GetString(9);
string _emailToken = reader.GetString(10);
account.claims = new AccountClaims() {
Email = _email,
EmailVerified = _emailVerified.ToString(),
FailedPasswordLock = _failedPasswordLock.ToString(),
Role = _role,
UserName = _username,
};
account.user = new Account() {
ID = _id,
UserName = _username,
Email = _email,
EmailVerified = _emailVerified,
PasswordHash = _passwordhash,
SiteData = new WebSiteData() {
AccountID = _id,
CurrentPasswordAttempts = _currentPasswordAttempts,
PasswordAttempts = _passwordAttempts,
EmailToken = _emailToken,
FailedPasswordLock = _failedPasswordLock,
Role = _role,
}
};
}
}
account.products = new List<Product>();
account.receipts = new List<Receipt>();
command = @"
SELECT * FROM Product
LEFT JOIN Receipt
ON ID = Receipt.ProductID
WHERE AccountID is Null or AccountID = @AccountID;
";
MySqlCommand cmd2 = new MySqlCommand(command, connection);
cmd2.Parameters.AddWithValue("@AccountID", AccountID);
using( DbDataReader reader = await cmd2.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _productID = !await reader.IsDBNullAsync(0) ? reader.GetInt32(0) : -1;
string _gameName = !await reader.IsDBNullAsync(1) ? reader.GetString(1) : "";
string _gameDesc = !await reader.IsDBNullAsync(2) ? reader.GetString(2) : "";
string _gameImg = !await reader.IsDBNullAsync(3) ? reader.GetString(3) : "";
int _gameCost = !await reader.IsDBNullAsync(4) ? reader.GetInt32(4) : 37707;
string _gameURL = !await reader.IsDBNullAsync(5) ? reader.IsDBNull(5) ? "" : reader.GetString(5) : "Something not common";
int _receiptAccountID = !await reader.IsDBNullAsync(6) ? reader.IsDBNull(6) ? -1 : reader.GetInt32(6) : -1;
string _receiptID = !await reader.IsDBNullAsync(8) ? reader.IsDBNull(8) ? "" : reader.GetString(8) : "";
DateTime _receiptTime = !await reader.IsDBNullAsync(10) ? reader.GetDateTime(10) : DateTime.Now;
string[] _imageList = _gameImg.Split('|', StringSplitOptions.RemoveEmptyEntries);
account.products.Add( new Product {
ID = _productID,
Cost = _gameCost,
Description = _gameDesc,
Name = _gameName,
URL = _gameURL,
Images = _imageList.ToList()
} );
if( _receiptAccountID != -1 ) {
account.receipts.Add( new Receipt {
AccountID = _receiptAccountID,
ProductID = _productID,
ReceiptID = _receiptID,
Time = _receiptTime
} );
}
}
}
}
return account;
}
}
}
@@ -0,0 +1,140 @@
using MistoxWebsite.Shared;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
public async Task<Product?> GetProduct( int ID ) {
Product? items = null;
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROM Product
WHERE ID = @ID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@ID", ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = reader.GetInt32("ID");
string _name = reader.GetString("Name");
string _description = reader.GetString("Description");
string _images = reader.GetString("Images");
int _cost = reader.GetInt32("Cost");
string _url = reader.GetString("URL");
string[] _imageList = _images.Split('|', StringSplitOptions.RemoveEmptyEntries);
items = new Product() {
ID = _id,
Name = _name,
Description = _description,
Cost = _cost,
Images = _imageList.ToList(),
URL = _url
};
}
}
}
return items;
}
public async Task<List<Product>> GetAllProducts() {
List<Product> items = new List<Product>();
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM Product", connection);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = reader.GetInt32("ID");
string _name = reader.GetString("Name");
string _description = reader.GetString("Description");
string _images = reader.GetString("Images");
int _cost = reader.GetInt32("Cost");
string _url = reader.GetString("URL");
string[] _imageList = _images.Split('|', StringSplitOptions.RemoveEmptyEntries);
items.Add( new Product() {
ID = _id,
Name = _name,
Description = _description,
Cost = _cost,
Images = _imageList.ToList(),
URL = _url
} );
}
}
}
return items;
}
public async Task NewProduct( Product Item ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string buildingImages = "";
foreach( string cur in Item.Images ) {
buildingImages = buildingImages + "|" + cur;
}
string command = @"
INSERT INTO Product
(Name, Description, Images, Cost, URL)
VALUES
(@Name, @Description, @Images, @Cost, @URL);
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@Name", Item.Name);
cmd.Parameters.AddWithValue("@Description", Item.Description);
cmd.Parameters.AddWithValue("@Images", buildingImages);
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
cmd.Parameters.AddWithValue("@URL", Item.URL);
await cmd.ExecuteNonQueryAsync();
}
}
public async Task UpdateProduct( Product Item ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string buildingImages = "";
foreach( string cur in Item.Images ) {
buildingImages = buildingImages + "|" + cur;
}
string command = @"UPDATE Product SET
Name = @Name,
Description = @Description,
Images = @Images,
Cost = @Cost,
URL = @URL
WHERE ID = @ID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@Name", Item.Name);
cmd.Parameters.AddWithValue("@Description", Item.Description);
cmd.Parameters.AddWithValue("@Images", Item.Images);
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
cmd.Parameters.AddWithValue("@URL", Item.URL);
cmd.Parameters.AddWithValue("@ID", Item.ID);
await cmd.ExecuteNonQueryAsync();
}
}
}
}
@@ -0,0 +1,71 @@
using MistoxWebsite.Shared;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
public async Task<ProjectMistData?> GetProjectMistData( int ID ) {
ProjectMistData? items = null;
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROM ProjectMistData
WHERE AccountID = @AccountID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = reader.GetInt32("AccountID");
items = new ProjectMistData() {
AccountID = _id,
};
}
}
}
return items;
}
public async Task NewProjectMistData( ProjectMistData data ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
INSERT INTO ProjectMistData
(AccountID)
VALUES
(@AccountID);
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@AccountID", data.AccountID);
await cmd.ExecuteNonQueryAsync();
}
}
public async Task UpdateProjectMistData( ProjectMistData data ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
UPDATE ProjectMistData SET
AccountID = @AccountID
WHERE AccountID = @AccountID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", data.AccountID);
await cmd.ExecuteReaderAsync();
}
}
}
}
@@ -0,0 +1,170 @@
using MistoxWebsite.Shared;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
public async Task<List<Receipt>> GetAllReceipts( Account account ) {
List<Receipt> receipts = new List<Receipt> ();
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROM Receipt
WHERE AccountID = @AccountID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _accountid = reader.GetInt32("AccountID");
int _gameid = reader.GetInt32("ProductID");
string _receiptid = reader.GetString("ReceiptID");
int _lineitem = reader.GetInt32("LineItem");
DateTime _receiptdate = reader.GetDateTime("Time");
int _taxamount = reader.GetInt32("TaxAmount");
int _totalcost = reader.GetInt32("TotalCost");
receipts.Add( new Receipt() {
AccountID = _accountid,
ProductID = _gameid,
ReceiptID = _receiptid,
Time = _receiptdate,
TotalCost = _totalcost,
TaxAmount = _taxamount,
LineItem = _lineitem
} );
}
}
}
return receipts;
}
public async Task<List<ReceiptProduct>> GetAllReceiptsJoinedToProduct( Account account ) {
List<ReceiptProduct> join = new List<ReceiptProduct> ();
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROM Receipt
LEFT JOIN Product
ON Receipt.ProductID = Product.ID
WHERE AccountID = @AccountID
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _accountid = !reader.IsDBNull( "AccountID" ) ? reader.GetInt32("AccountID") : -1;
int _gameid = !reader.IsDBNull( "ProductID" ) ? reader.GetInt32("ProductID") : 0;
string _receiptid = !reader.IsDBNull( "ReceiptID" ) ? reader.GetString("ReceiptID") : "";
int _lineitem = !reader.IsDBNull( "LineItem" ) ? reader.GetInt32("LineItem") : 0;
DateTime _receiptdate = !reader.IsDBNull( "Time" ) ? reader.GetDateTime("Time") : DateTime.Now;
int _taxamount = !reader.IsDBNull( "TaxAmount" ) ? reader.GetInt32("TaxAmount") : 0;
int _totalcost = !reader.IsDBNull( "TotalCost" ) ? reader.GetInt32("TotalCost") : 0;
int _id = !reader.IsDBNull( "ID" ) ? reader.GetInt32("ID") : 0;
string _name = !reader.IsDBNull( "Name" ) ? reader.GetString("Name") : "";
string _desc = !reader.IsDBNull( "Description" ) ? reader.GetString("Description") : "";
int _cost = !reader.IsDBNull( "Cost" ) ? reader.GetInt32("Cost") : 0;
string _url = !reader.IsDBNull( "URL" ) ? reader.GetString("URL") : "Something Random That Wont Ever Be In A URL";
join.Add( new ReceiptProduct() {
receipt = new Receipt {
AccountID = _accountid,
ProductID = _gameid,
ReceiptID = _receiptid,
Time = _receiptdate,
TotalCost = _totalcost,
TaxAmount = _taxamount,
LineItem = _lineitem
},
product = new Product() {
ID = _id,
Cost = _cost,
Description = _desc,
Name = _name,
URL = _url
}
} );
}
}
}
return join;
}
public async Task<Receipt?> GetReceipt( Account account, Product game ) {
Receipt? receipt = null;
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROMReceipt
WHERE AccountID = @AccountID AND ProductID = @ProductID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
cmd.Parameters.AddWithValue("@ProductID", game.ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _accountid = reader.GetInt32("AccountID");
int _gameid = reader.GetInt32("ProductID");
string _receiptid = reader.GetString("ReceiptID");
int _lineitem = reader.GetInt32("LineItem");
DateTime _receiptdate = reader.GetDateTime("Time");
int _taxamount = reader.GetInt32("TaxAmount");
int _totalcost = reader.GetInt32("TotalCost");
receipt = new Receipt() {
AccountID = _accountid,
ProductID = _gameid,
ReceiptID = _receiptid,
Time = _receiptdate,
TotalCost = _totalcost,
TaxAmount = _taxamount,
LineItem = _lineitem
};
}
}
}
return receipt;
}
public async Task NewReceipt( Receipt receipt ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
INSERT INTO Receipt
(AccountID, ProductID, ReceiptID, LineItem, TaxAmount, TotalCost, Time)
VALUES
(@AccountID, @ProductID, @ReceiptID, @LineItem, @TaxAmount, @TotalCost, @Time)
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@AccountID", receipt.AccountID);
cmd.Parameters.AddWithValue("@ProductID", receipt.ProductID);
cmd.Parameters.AddWithValue("@ReceiptID", receipt.ReceiptID);
cmd.Parameters.AddWithValue("@LineItem", receipt.LineItem);
cmd.Parameters.AddWithValue("@TaxAmount", receipt.TaxAmount);
cmd.Parameters.AddWithValue("@TotalCost", receipt.TotalCost);
cmd.Parameters.AddWithValue("@Time", receipt.Time); // Just incase i need this in the future | receipt.Time.ToString( "yyyy-MM-dd hh:mm:ss" )
await cmd.ExecuteNonQueryAsync();
}
}
}
}
@@ -0,0 +1,115 @@
using MistoxWebsite.Shared;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
public async Task<WebSiteData?> GetWebsiteData( Account account ) {
WebSiteData? webSiteData = null;
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
SELECT * FROM WebsiteData
WHERE AccountID = @AccountID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
while( await reader.ReadAsync() ) {
if( reader == null ) {
break;
}
int _id = 0;
bool _failedpasswordlock = false;
int _passwordattempts = 5;
int _curpasswordattempts = 0;
string _role = "";
string _emailtoken = "";
if( !reader.IsDBNull( "AccountID" ) ) {
_id = reader.GetInt32( "AccountID" );
}
if( !reader.IsDBNull( "FailedPasswordLock" ) ) {
_failedpasswordlock = reader.GetBoolean( "FailedPasswordLock" );
}
if( !reader.IsDBNull( "PasswordAttempts" ) ) {
_passwordattempts = reader.GetInt32( "PasswordAttempts" );
}
if( !reader.IsDBNull( "CurrentPasswordAttempts" ) ) {
_curpasswordattempts = reader.GetInt32( "CurrentPasswordAttempts" );
}
if( !reader.IsDBNull( "Role" ) ) {
_role = reader.GetString( "Role" );
}
if( !reader.IsDBNull( "EmailToken" ) ) {
_emailtoken = reader.GetString( "EmailToken" );
}
webSiteData = new WebSiteData() {
AccountID = _id,
FailedPasswordLock = _failedpasswordlock,
CurrentPasswordAttempts = _curpasswordattempts,
PasswordAttempts = _passwordattempts,
EmailToken = _emailtoken,
Role = _role,
};
}
}
}
return webSiteData;
}
public async Task NewWebsiteData( Account account, WebSiteData data ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
INSERT INTO WebsiteData
(AccountID, FailedPasswordLock, PasswordAttempts, CurrentPasswordAttempts, Role, EmailToken)
VALUES
(@AccountID, @FailedPasswordLock, @PasswordAttempts, @CurrentPasswordAttempts, @Role, @EmailToken);
";
MySqlCommand cmd = new MySqlCommand( command , connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
cmd.Parameters.AddWithValue("@FailedPasswordLock", data.FailedPasswordLock);
cmd.Parameters.AddWithValue("@PasswordAttempts", data.PasswordAttempts);
cmd.Parameters.AddWithValue("@CurrentPasswordAttempts", data.CurrentPasswordAttempts);
cmd.Parameters.AddWithValue("@Role", data.Role);
cmd.Parameters.AddWithValue("@EmailToken", data.EmailToken);
await cmd.ExecuteNonQueryAsync();
}
}
public async Task UpdateWebsiteData( Account account, WebSiteData data ) {
using( MySqlConnection connection = GetConnection() ) {
connection.Open();
string command = @"
UPDATE WebsiteData SET
FailedPasswordLock = @FailedPasswordLock,
PasswordAttempts = @PasswordAttempts,
CurrentPasswordAttempts = @CurrentPasswordAttempts,
Role = @Role,
EmailToken = @EmailToken
WHERE AccountID = @AccountID;
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@AccountID", account.ID);
cmd.Parameters.AddWithValue("@FailedPasswordLock", data.FailedPasswordLock);
cmd.Parameters.AddWithValue("@PasswordAttempts", data.PasswordAttempts);
cmd.Parameters.AddWithValue("@CurrentPasswordAttempts", data.CurrentPasswordAttempts);
cmd.Parameters.AddWithValue("@Role", data.Role);
cmd.Parameters.AddWithValue("@EmailToken", data.EmailToken);
await cmd.ExecuteNonQueryAsync();
}
}
}
}