Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("Id,UserId,SymbolId,Quantity,Price,Action,CreationDate,Comment,Status,Reserved")] Trade trade)
        {
            if (ModelState.IsValid)
            {
                trade.UserId       = _userManager.GetUserId(HttpContext.User);
                trade.CreationDate = DateTime.Now;
                trade.Status       = TradeStatus.Approved;
                _context.Add(trade);
                await _context.SaveChangesAsync();

                //creating a transaction to pay or get paid for the trade. can be set status too? but
                // right now all will be set to approved because there is try-catch on all transactions and trade
                Transaction transaction = new Transaction();
                transaction = await _tradeHelper.createTradeTransaction(trade);

                await _tradeHelper.updateUserCashBalance(transaction);

                _logger.LogWarning("Before Wealth creation" + transaction.TransactionType);
                await _tradeHelper.createWealth(transaction);

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SymbolId"] = new SelectList(_context.TickerSymbols.Where(s => s.isEnabled == true), "Id", "Symbol", trade.SymbolId);
            return(View(trade));
        }
        public async Task createWealth(Transaction transaction)
        {
            int    symbolId = transaction.Trade.SymbolId;
            string userId   = transaction.UserId;
            string action   = transaction.Trade.Action.ToString();

            _logger.LogWarning(action);
            var wealth = await isWealthOwner(symbolId, userId);

            _logger.LogWarning("Wealth is null??????????????" + wealth);
            if (wealth != null)
            {
                _logger.LogWarning("Inside createWealth  if block: " + transaction.TransactionType.ToString().ToUpper());
                if (transaction.Trade.Action.ToString().ToUpper() == "BUY")
                {
                    wealth.Quantity    = wealth.Quantity + transaction.Trade.Quantity;
                    wealth.UpdatedDate = DateTime.Now;
                    await _dbContext.SaveChangesAsync();
                }
                else if (transaction.Trade.Action.ToString().ToUpper() == "SELL")
                {
                    _logger.LogWarning("Inside createWealth SELL if block qty: " + wealth.Quantity + " tras: " + transaction.Trade.Quantity);
                    if (transaction.Trade.Quantity <= wealth.Quantity)
                    {
                        wealth.Quantity    = wealth.Quantity - transaction.Trade.Quantity;
                        wealth.UpdatedDate = DateTime.Now;
                        await _dbContext.SaveChangesAsync();
                    }
                    else
                    {
                        //we had to check it before.
                        // Not happens
                        // ajax and jQuery already implemented. It will not happen if user doesn't have share enough.
                    }
                }
            }
            else
            {
                Wealth tempWealth = new Wealth();
                tempWealth.CreationDate = DateTime.Now;
                tempWealth.UpdatedDate  = DateTime.Now;
                tempWealth.Quantity     = transaction.Trade.Quantity;
                tempWealth.SymbolId     = transaction.Trade.SymbolId;
                tempWealth.UserId       = transaction.UserId;
                await CreateWealth(tempWealth);
            }
        }
        public async Task <IActionResult> Create([Bind("Id,UserId,SymbolId,CurrentPrice,OfferPrice,Action,ExpirationDate,CreationDate,UpdatedDate,Expired")] Offer offer)
        {
            if (ModelState.IsValid)
            {
                offer.UserId       = _userManager.GetUserId(HttpContext.User);
                offer.CreationDate = DateTime.Now;
                offer.UpdatedDate  = DateTime.Now;
                _context.Add(offer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SymbolId"] = new SelectList(_context.TickerSymbols.Where(s => s.isEnabled == true), "Id", "Symbol", offer.SymbolId);
            return(View(offer));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,UserId,TradeId,TransType,FromAccount,ToAccount,TotalAmount,Comment,TimeStamp,Reconciled")] Transaction transaction)
        {
            if (ModelState.IsValid)
            {
                transaction.UserId       = _userManager.GetUserId(HttpContext.User);
                transaction.CreationDate = DateTime.Now;
                transaction.Reconciled   = true;
                _context.Add(transaction);
                await _context.SaveChangesAsync();

                bool result = await _tradeHelperService.updateUserCashBalance(transaction);

                if (!result)
                {
                    ViewData["error"] = "Transaction Faild.";
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(transaction));
        }