Allow saving stocks to the database

This commit is contained in:
2026-03-08 19:42:14 -07:00
parent 78bdf250ff
commit 04631398ed
+12 -8
View File
@@ -37,7 +37,7 @@
<div>@cur.Symbol</div>
<div>@cur.Quantity</div>
<div>@cur.PurchasePrice</div>
<div>@cur.PurchaseDate.ToString("M-D-YYYY")</div>
<div>@cur.PurchaseDate.ToString("M-dd-yyyy")</div>
}
</div>
</div>
@@ -54,9 +54,12 @@
<button disabled>@trainButtonText</button>
<button disabled>@predictButtonText</button>
}
<button @onclick="buyStock">Buy Stock</button>
<input placeholder="Stock Symbol [NVDA]" @bind="buyStockSymbol" />
<input placeholder="1.0" @bind="buyStockQuantity" />
<br/>
<div>
<input placeholder="Stock Symbol [NVDA]" @bind="buyStockSymbol" />
<input placeholder="Stock Quantity [1.0]" @bind="buyStockQuantity" />
<button @onclick="buyStock">Buy Stock</button>
</div>
<span>@resultError</span>
</div>
</div>
@@ -120,7 +123,7 @@
string dbPrefix = $"[{userName.ToLower()}]:"; // Set the DB prefix for the get and set
string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash
if (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid
List<PurchasedStock>? stocks = JsonConvert.DeserializeObject<List<PurchasedStock>>( dbDriver.Get( dbPrefix + "stock-data" ) );
List<PurchasedStock>? stocks = JsonConvert.DeserializeObject<List<PurchasedStock>>( dbDriver.Get( dbPrefix + "Stocks" ) );
Session = new loginSession(){
UserName = userName.ToLower(),
TrackedStocks = stocks != null ? stocks : new List<PurchasedStock>()
@@ -225,7 +228,7 @@
float StockPrice = 0;
void buyStock(){
if (Session != null){
string dbPrefix = $"[{userName.ToLower()}]:";
// Try Parse the quantitiy input
bool success = float.TryParse(buyStockQuantity, out float QuantityResult);
if (!success){
@@ -240,13 +243,14 @@
return;
}
// Add the item
// Add the stock
Session.TrackedStocks.Add( new PurchasedStock(){
Symbol = buyStockSymbol.ToLower(),
Symbol = buyStockSymbol.ToUpper(),
PurchasePrice = StockPrice,
Quantity = QuantityResult,
PurchaseDate = DateTime.Now
} );
dbDriver.Set( dbPrefix + "Stocks", Newtonsoft.Json.JsonConvert.SerializeObject(Session.TrackedStocks) );
// Reset the Impodent Key
result = PaymentProcessor.CreatePayment();