Exemplo n.º 1
0
        public static EmbedBuilder BuildSellView(PapertradeSellResult data)
        {
            var embed = new EmbedBuilder()
                        .WithTitle($"Sold {data.Amount} shares of {data.StockCode} at {data.Price}")
                        .AddField(fb => fb.WithName(":money_with_wings: " + Format.Bold("Total gain")).WithValue(data.Cost))
                        .AddField(fb => fb.WithName(":moneybag: ").WithValue($"You now have a total of {data.TotalAmount} shares of {data.StockCode}").WithIsInline(true))
                        .WithOkColor();

            return(embed);
        }
Exemplo n.º 2
0
        public async Task <Result <PapertradeSellResult> > SellShares(long userId, string stockCode, long amount)
        {
            var stockCodeUpper = stockCode.ToUpper();
            var lookup         = await _asxService.Lookup(stockCodeUpper);

            if (!lookup.IsSuccess)
            {
                return new Result <PapertradeSellResult> {
                           IsSuccess = false, Message = lookup.Message
                }
            }
            ;

            var data = lookup.Payload;

            if (!data.Last_Price.HasValue)
            {
                return new Result <PapertradeSellResult>()
                       {
                           IsSuccess = false, Message = "Current price could not be retrieved."
                       }
            }
            ;

            var price = data.Last_Price.Value;
            var cost  = amount * price;
            var user  = _databaseContext.PapertradeUser.First(x => x.UserId == userId);

            var result = new PapertradeSellResult()
            {
                UserId    = userId,
                Amount    = amount,
                StockCode = stockCodeUpper,
                Cost      = cost,
                Price     = price
            };

            if (user.OwnedStocks == null || user.OwnedStocks.All(x => x.StockCode != stockCodeUpper) ||
                user.OwnedStocks.First(x => x.StockCode == stockCodeUpper).Shares < amount)
            {
                return new Result <PapertradeSellResult> {
                           IsSuccess = false, Message = "You don't have those shares to sell."
                }
            }
            ;

            user.Money  += cost;
            result.Money = user.Money;

            var ownedStock = user.OwnedStocks.First(x => x.StockCode == stockCodeUpper);

            ownedStock.Shares -= amount;
            result.TotalAmount = user.OwnedStocks.First(x => x.StockCode == stockCodeUpper).Shares;

            if (result.TotalAmount == 0)
            {
                user.OwnedStocks.Remove(ownedStock);
            }

            user.Transactions.Add(new PapertradeTransaction()
            {
                StockCode       = stockCodeUpper,
                Shares          = amount,
                TransactionType = PapertradeTransactionType.SELL,
                User            = user,
                Price           = (decimal)price
            });

            await _databaseContext.SaveChangesAsync();

            return(new Result <PapertradeSellResult>()
            {
                IsSuccess = true,
                Payload = result
            });
        }
    }
}