async Task <bool> SellStock(MySqlConnection cnn, SellTrigger trigger) { int numStock = trigger.Amount / _cost; // most whole number stock that can Sell int amount = numStock * _cost; // total amount spent int leftover = trigger.Amount - amount; using (MySqlCommand cmd = new MySqlCommand()) { cmd.Connection = cnn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sell_trigger"; cmd.Parameters.AddWithValue("@pUserId", trigger.User); cmd.Parameters["@pUserId"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@pStock", trigger.StockSymbol); cmd.Parameters["@pStock"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@pStockAmount", amount); cmd.Parameters["@pStockAmount"].Direction = ParameterDirection.Input; cmd.Parameters.AddWithValue("@pStockLeftover", leftover); cmd.Parameters["@pStockLeftover"].Direction = ParameterDirection.Input; await cmd.ExecuteNonQueryAsync().ConfigureAwait(false); } return(true); }
public async Task <string> AddOrUpdateUser(UserCommandType command) { if (_users.TryGetValue(command.username, out SellTrigger trigger)) { if (!command.fundsSpecified) // Using fundsSpecified to indicate if it was SET_SELL_AMOUNT (false) or SET_SELL_TRIGGER (true) { return("Sell amount already set for this stock"); } if ((int)(command.funds) == trigger.Trigger) { return("Trigger amount already set for this stock"); } if ((int)command.funds > trigger.Amount) { return("Trigger amount can not be greater than Sell amount"); } trigger.Trigger = (int)(command.funds); if (trigger.Trigger <= _cost) { await SellStockAndRemoveUserTrigger(new SellTrigger[] { trigger }).ConfigureAwait(false); } return(null); // Signals success, output in Service } if (command.fundsSpecified) { return("Set sell amount before creating trigger"); } trigger = new SellTrigger(command.username, command.stockSymbol, (int)(command.funds)); while (!_users.TryAdd(command.username, trigger)) { ; } return(null); // Signals success, output in Service }