Example #1
0
        public static EmbedBuilder BuildBuyView(PapertradeBuyResult data)
        {
            var embed = new EmbedBuilder()
                        .WithTitle($"Bought {data.Amount} shares of {data.StockCode} at {data.Price}")
                        .AddField(fb => fb.WithName(":money_with_wings: " + Format.Bold("Total cost")).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);
        }
        public async Task <Result <PapertradeBuyResult> > BuyShares(long userId, string stockCode, long amount)
        {
            var stockCodeUpper = stockCode.ToUpper();
            var lookup         = await _asxService.Lookup(stockCodeUpper);

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

            var data = lookup.Payload;

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

            var price = data.Last_Price.Value;
            var cost  = amount * price;
            var user  = await GetOrCreateUser(userId);

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

            if (user.Money < cost)
            {
                return new Result <PapertradeBuyResult>()
                       {
                           IsSuccess = false, Message = "You don't have enough money for that."
                       }
            }
            ;

            user.Money  -= cost;
            result.Money = user.Money;

            if (user.OwnedStocks != null && user.OwnedStocks.Any(x => x.StockCode == stockCodeUpper))
            {
                user.OwnedStocks.First(x => x.StockCode == stockCodeUpper).Shares += amount;

                result.TotalAmount = user.OwnedStocks.First(x => x.StockCode == stockCodeUpper).Shares;
            }
            else
            {
                if (user.OwnedStocks == null)
                {
                    user.OwnedStocks = new List <PapertradeOwnedStock>();
                }

                user.OwnedStocks.Add(new PapertradeOwnedStock()
                {
                    StockCode = stockCodeUpper,
                    Shares    = amount,
                    User      = user
                });

                result.TotalAmount = amount;
            }

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

            await _databaseContext.SaveChangesAsync();

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