protected override async Task <string> DataReceived(UserCommandType command) { string result = ""; try { var trigger = SellTriggerTimer.RemoveUserTrigger(command.username, command.stockSymbol); if (trigger != null) { command.fundsSpecified = true; command.funds = trigger.Amount; // Check if trigger exists MySQL db = new MySQL(); result = await db.PerformTransaction(CancelSell, command).ConfigureAwait(false); } else { return(LogErrorEvent(command, $"No trigger set for {command.stockSymbol}")); } } catch (Exception ex) { LogDebugEvent(command, ex.Message); return(LogErrorEvent(command, "Error processing command")); } return(result); }
async Task <string> CheckUserStock(MySqlCommand cmd, UserCommandType command) { cmd.CommandText = "get_user_stock"; cmd.Parameters.AddWithValue("@pUserId", command.username); cmd.Parameters["@pUserId"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@pStock", command.stockSymbol); cmd.Parameters["@pStock"].Direction = ParameterDirection.Input; cmd.Parameters.Add("@pStockAmount", DbType.Int32); cmd.Parameters["@pStockAmount"].Direction = ParameterDirection.Output; await cmd.ExecuteNonQueryAsync().ConfigureAwait(false); if (cmd.Parameters["@pStockAmount"].Value == DBNull.Value) { return(LogErrorEvent(command, "User does not exist or does not have this stock")); } if (Convert.ToInt32(cmd.Parameters["@pStockAmount"].Value) < command.funds) { return(LogErrorEvent(command, "Insufficient user stocks")); } command.fundsSpecified = false; return(await SellTriggerTimer.StartOrUpdateTimer(command).ConfigureAwait(false));; }
public static async Task <string> StartOrUpdateTimer(UserCommandType command) { SellTriggerTimer timer; bool newTimer; lock (_timerLock) { newTimer = !_timers.TryGetValue(command.stockSymbol, out timer); if (newTimer) { if (command.fundsSpecified) { return("Set sell amount before creating trigger"); } timer = new SellTriggerTimer(command.stockSymbol); if (!_timers.TryAdd(command.stockSymbol, timer)) { LogDebugEvent(command, "Error adding timer"); } } } var msg = await timer.AddOrUpdateUser(command).ConfigureAwait(false); if (newTimer) { var _ = timer.Start(); } return(msg); }
protected override async Task <string> DataReceived(UserCommandType command) { try { command.fundsSpecified = true; var msg = await SellTriggerTimer.StartOrUpdateTimer(command).ConfigureAwait(false); if (msg == null) { return($"Trigger amount set successfully for stock {command.stockSymbol}"); } return(LogErrorEvent(command, msg)); } catch (Exception ex) { LogDebugEvent(command, ex.Message); return(LogErrorEvent(command, "Error processing command.")); } }