Files
MistoxCom-Angular/src/MistoxWebsite.Server/Services/DatabaseService/AccountInventory.cs
T
2025-06-16 17:04:55 -07:00

116 lines
5.1 KiB
C#
Executable File

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
} );
}
}
}
}
}
}