Exemple #1
0
        public async Task InsertUserTrend(int userId, string trendName)
        {
            List <SqlParameter> paras = new List <SqlParameter>();

            paras.Add(new SqlParameter("@USERID", userId));
            paras.Add(new SqlParameter("@NAME", trendName));
            await DBUtility.ExecuteQueryAsync(opts.ConnectionString, "spInsertUserTrend", paras);
        }
Exemple #2
0
        public async Task <(int ReturnCode, decimal?Balance)> InsertOrder(Order newOrder, Guid orderId, int status)
        {
            var dt = await DBUtility.GetDataTableAsync(opts.ConnectionString, "spSelectUserBalance", new List <SqlParameter> {
                new SqlParameter("@USERID", this.userId)
            });

            var balance = Convert.ToDecimal(dt.Rows[0][0]);

            dt = await DBUtility.GetDataTableAsync(opts.ConnectionString, "spSelectUserShares", new List <SqlParameter> {
                new SqlParameter("@USERID", this.userId), new SqlParameter("@TRENDNAME", newOrder.TrendName)
            });

            var totalTrendShares = Convert.ToDecimal(dt.Rows[0][0]);

            decimal amount           = newOrder.Price * newOrder.Quantity;
            decimal newBalance       = (newOrder.TradeTypeId == 1) ? balance - amount : balance + amount;
            decimal newShareQuantity = 0;

            //// IM TRYING TO BUY/SELL ZERO SHARES
            if (newOrder.Quantity <= 0)
            {
                return(1, null);
            }

            //// IM TRYING TO BUY/SELL TRENDS WITH A PRICE OF ZERO
            if (newOrder.Price == 0)
            {
                return(4, null);
            }

            //// IM TRYING TO BUY STUFF
            if (newOrder.TradeTypeId == 1)
            {
                // I DONT HAVE ENOUGH FUNDS TO BUY SHARES
                if (newBalance < 0)
                {
                    return(2, null);
                }
                // I DO HAVE ENOUGH FUNDS TO BUY SHARES
                else
                {
                    newShareQuantity = totalTrendShares + newOrder.Quantity;
                }
            }

            //// IM TRYING TO SELL STUFF
            if (newOrder.TradeTypeId == 2)
            {
                // I DONT HAVE ANY OR ENOUGH SHARES THAT IM TRYING TO SELL
                if (totalTrendShares < newOrder.Quantity)
                {
                    return(3, null);
                }
                // I DO HAVE ENOUGH SHARES TO SELL
                else
                {
                    newShareQuantity = totalTrendShares - newOrder.Quantity;
                }
            }

            List <SqlParameter> parameters = new List <SqlParameter>();

            parameters.Add(new SqlParameter("@ORDERID", orderId));
            parameters.Add(new SqlParameter("@USERID", newOrder.UserId));
            parameters.Add(new SqlParameter("@TRENDID", newOrder.TrendId));
            parameters.Add(new SqlParameter("@TRENDNAME", newOrder.TrendName));
            parameters.Add(new SqlParameter("@TRADETYPEID", newOrder.TradeTypeId));
            parameters.Add(new SqlParameter("@ACTIVITYID", newOrder.TradeTypeId));
            parameters.Add(new SqlParameter("@PRICE", newOrder.Price));
            parameters.Add(new SqlParameter("@QUANTITY", newOrder.Quantity));
            parameters.Add(new SqlParameter("@TOTALAMOUNT", amount));
            parameters.Add(new SqlParameter("@NEWBALANCE", newBalance));
            parameters.Add(new SqlParameter("@STATUS", status));
            parameters.Add(new SqlParameter("@NEWQUANTITY", newShareQuantity));
            await DBUtility.ExecuteQueryAsync(opts.ConnectionString, "spInsertOrder", parameters);

            return(0, newBalance);
        }