-
@cur.Symbol
+
@cur.Symbol
+
Purchased Price: @cur.PurchasePrice
+
Purchased Quantity: @cur.Quantity
+
Purchased Date: @cur.PurchaseDate.ToString("M-dd-yyyy")
->
@@ -102,20 +100,10 @@
loginSession? Session = null;
-
-
-
-
-
-
// On Page Load
string PaymentKey = "";
protected override async Task OnInitializedAsync(){
- (bool, string) result = PaymentProcessor.CreatePayment();
- if (!result.Item1){
- resultError = result.Item2;
- }
- PaymentKey = result.Item2;
+
}
@@ -133,10 +121,17 @@
string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash
if (BCrypt.Verify( passWord, passwordhash )){ // If the password is valid
List
? stocks = JsonConvert.DeserializeObject>( dbDriver.Get( dbPrefix + "Stocks" ) );
+ bool moneyLoaded = float.TryParse(dbDriver.Get( dbPrefix + "money" ), out float moneyResult);
Session = new loginSession(){
UserName = userName.ToLower(),
- TrackedStocks = stocks != null ? stocks : new List()
+ TrackedStocks = stocks != null ? stocks : new List(),
+ Money = moneyLoaded ? moneyResult : 1000
};
+ (bool, string) result = PaymentProcessor.CreatePayment(Session.UserName);
+ if (!result.Item1){
+ resultError = result.Item2;
+ }
+ PaymentKey = result.Item2;
}else{
loginError = "wrong password";
}
@@ -147,9 +142,11 @@
string passwordhash = dbDriver.Get( dbPrefix + "password" );
if (string.IsNullOrEmpty(passwordhash)){
dbDriver.Set( dbPrefix + "password", BCrypt.HashPassword( passWord, BCrypt.GenerateSalt() ) );
+ dbDriver.Set( dbPrefix + "money", "1000" );
Session = new loginSession(){
UserName = userName.ToLower(),
- TrackedStocks = new List()
+ TrackedStocks = new List(),
+ Money = 1000
};
}else{
loginError = "account is taken";
@@ -273,10 +270,14 @@
} );
dbDriver.Set( dbPrefix + "Stocks", Newtonsoft.Json.JsonConvert.SerializeObject(Session.TrackedStocks) );
+ // Reload the users money
+ bool moneyLoaded = float.TryParse(dbDriver.Get( dbPrefix + "money" ), out float moneyResult);
+ Session.Money = moneyLoaded ? moneyResult : 1000;
+
// Reset the Impodent Key
- result = PaymentProcessor.CreatePayment();
+ result = PaymentProcessor.CreatePayment(Session.UserName);
if (!result.Item1){
- resultError = "[Payment suceeded but new payment session failed] : " + result.Item2;
+ resultError = "[New payment session failed] : " + result.Item2;
return;
}
PaymentKey = result.Item2;
diff --git a/WebServer/Components/Pages/Home.razor.css b/WebServer/Components/Pages/Home.razor.css
index 516a066d..98d6028a 100644
--- a/WebServer/Components/Pages/Home.razor.css
+++ b/WebServer/Components/Pages/Home.razor.css
@@ -67,7 +67,6 @@
.signalBlock > :nth-child(1) {
border: solid #040 1px;
border-radius: 15px;
- width: 40px;
padding: 0px 20px;
background-color: greenyellow;
}
diff --git a/WebServer/Controllers/Payments/IPayments.cs b/WebServer/Controllers/Payments/IPayments.cs
index 3194747a..53f1d9b0 100644
--- a/WebServer/Controllers/Payments/IPayments.cs
+++ b/WebServer/Controllers/Payments/IPayments.cs
@@ -3,7 +3,7 @@ namespace Controllers.Payment {
public interface IPayment {
// [ Success, ErrorMessage | ImpodentKey ]
- public (bool, string) CreatePayment();
+ public (bool, string) CreatePayment(string User);
// [ Success, ErrorMessage ]
public (bool, string) TryPayment(string ImpodentKey, float Price);
diff --git a/WebServer/Controllers/Payments/PaymentTester.cs b/WebServer/Controllers/Payments/PaymentTester.cs
index f9da68f2..b3a0c586 100644
--- a/WebServer/Controllers/Payments/PaymentTester.cs
+++ b/WebServer/Controllers/Payments/PaymentTester.cs
@@ -1,27 +1,72 @@
+using Controllers.DataBase;
+
namespace Controllers.Payment {
public class PaymentTestor : IPayment {
+
+ static List ImpodentKeys = new List();
+ DbDriver? _DBdriver = null;
- public static List ImpodentKeys = new List();
+ // Inject the DB driver
+ public PaymentTestor() { _DBdriver = new DbDriver(); }
- public (bool, string) CreatePayment() {
+ public (bool, string) CreatePayment(string user) {
string guid = Guid.NewGuid().ToString();
- ImpodentKeys.Add(guid);
+ ImpodentKeys.Add(new ImpodentKey{ Key = guid, User = user });
return (true, guid);
}
public (bool, string) TryPayment(string ImpodentKey, float Price) {
try {
- if (ImpodentKeys.Contains(ImpodentKey)) {
- ImpodentKeys.Remove(ImpodentKey);
- return (true, "");
- } else {
+ // Find the impotency key
+ ImpodentKey? found = null;
+ foreach( ImpodentKey key in ImpodentKeys) {
+ if (key.Key == ImpodentKey) {
+ found = key;
+ break;
+ }
+ }
+ if (found == null) {
return (false, "Payment session closed or never existed");
}
+
+ // Make sure the database is injected correctly
+ if (_DBdriver == null) {
+ found.Processed = true;
+ return (false, "The Database cannot be loaded");
+ }
+
+ // Get the users money
+ string dbPrefix = $"[{found.User.ToLower()}]:";
+ bool passed = float.TryParse(_DBdriver.Get( dbPrefix + "money" ), out float result);
+ if (!passed) {
+ found.Processed = true;
+ return (false, "Unable to parse money from the database");
+ }
+
+ // Make sure the user has enough money
+ if (result < Price) {
+ found.Processed = true;
+ return (false, "You dont have enough money to purchase this item");
+ }
+
+ // Remove the money and save
+ _DBdriver.Set( dbPrefix + "money", (result - Price).ToString());
+ found.PaymentSuccess = true;
+ found.Processed = true;
+ return (true, "");
+
}catch(Exception e) {
return (false, e.ToString());
}
}
}
+ class ImpodentKey {
+ public string Key { get; set; } = "";
+ public string User { get; set; } = "";
+ public bool PaymentSuccess { get; set; } = false;
+ public bool Processed { get; set; } = false;
+ }
+
}
\ No newline at end of file
diff --git a/WebServer/Entities/LoginSession.cs b/WebServer/Entities/LoginSession.cs
index c35c06ee..ce4d1dc7 100644
--- a/WebServer/Entities/LoginSession.cs
+++ b/WebServer/Entities/LoginSession.cs
@@ -2,6 +2,7 @@ namespace Entities {
class loginSession {
public string UserName { get; set; } = "";
+ public float Money { get; set; } = 0;
public List TrackedStocks { get; set; } = new List();
}