Esempio n. 1
0
        private async Task <InvestmentOrderResult> PlaceTradeInvestment(InvestmentOrder order, Wallet wallet)
        {
            var resource = await _context.TradeAssets.FindAsync(order.ProductId);

            if (resource?.Open is null)
            {
                return(new InvestmentOrderResult
                {
                    Errors = new[] { "Unavailable investment option." }
                });
            }

            var investment = new Investment
            {
                Description = resource.Symbol,
                Quantity    = order.Quantity,
                AssetId     = resource.Id,
                Value       = order.Quantity * resource.Open.Value,
                Wallet      = wallet
            };

            _context.Add(investment);
            await _context.SaveChangesAsync();

            return(new InvestmentOrderResult
            {
                Success = true
            });
        }
Esempio n. 2
0
        private async Task <InvestmentOrderResult> PlaceCryptoInvestment(InvestmentOrder order, Wallet wallet)
        {
            var resource = await _context.CryptoCoins.FindAsync(order.ProductId);

            if (resource is null || !decimal.TryParse(resource.PriceUsd, out var price))
            {
                return(new InvestmentOrderResult
                {
                    Errors = new[] { "Unavailable investment option." }
                });
            }

            var investment = new Investment
            {
                Description = resource.AssetId,
                Quantity    = order.Quantity,
                AssetId     = resource.Id,
                Value       = order.Quantity * price,
                Wallet      = wallet
            };

            _context.Add(investment);
            await _context.SaveChangesAsync();

            return(new InvestmentOrderResult
            {
                Success = true
            });
        }
Esempio n. 3
0
        public async Task <InvestmentOrderResult> PlaceInvestmentOrder(InvestmentOrder order)
        {
            var user = await _userManager.FindByIdAsync(order.UserId);

            if (user is null)
            {
                return(new InvestmentOrderResult
                {
                    Errors = new[] { "Invalid user." }
                });
            }

            var wallet = await _context.Wallets.SingleOrDefaultAsync(w => w.UserId == user.Id);

            if (wallet is null)
            {
                wallet = new Wallet
                {
                    User = user
                };
                _context.Add(wallet);
                await _context.SaveChangesAsync();
            }

            if (order.IsCrypto)
            {
                return(await PlaceCryptoInvestment(order, wallet));
            }

            return(await PlaceTradeInvestment(order, wallet));
        }