Beispiel #1
0
        public async Task <ActionResult <Sale> > Sell([FromBody] SellRequest sellRequest)
        {
            var account = await _accountQuery.Evaluate(sellRequest.AccountNumber);

            if (account == null)
            {
                return(NotFound("Account does not exist"));
            }

            var currentPrice = await _currentPriceQuery.Evaluate(sellRequest.ProductCode);

            if (currentPrice == DataAccess.CurrentPriceQuery.ErrorConditions.ProductDoesNotExist)
            {
                return(NotFound("Product does not exist"));
            }
            if (currentPrice == DataAccess.CurrentPriceQuery.ErrorConditions.PriceDoesNotExist)
            {
                return(BadRequest("No valid price"));
            }

            if (currentPrice < sellRequest.MinUnitPrice)
            {
                return(new Sale()
                {
                    UnitPrice = currentPrice,
                    Message = $"The current price of {currentPrice} is lower than the minimum you specified of {sellRequest.MinUnitPrice}.",
                    Success = false,
                    ProductCode = sellRequest.ProductCode,
                });
            }

            var totalIncome = currentPrice * sellRequest.Quantity;

            var sharesCurrentlyHeld = account.Portfolio.FirstOrDefault(investment => investment.ProductCode == sellRequest.ProductCode)?.Quantity ?? 0;

            if (sellRequest.Quantity > sharesCurrentlyHeld)
            {
                return(new Sale()
                {
                    UnitPrice = currentPrice,
                    Message = $"You have requested to sell {sellRequest.Quantity} but you only have {sharesCurrentlyHeld}.",
                    Success = false,
                    ProductCode = sellRequest.ProductCode,
                });
            }

            var transaction = new Transaction()
            {
                AccountNumber = account.AccountNumber,
                Quantity      = -sellRequest.Quantity,
                Time          = DateTime.Now,
                UnitPrice     = currentPrice,
                TotalValue    = totalIncome,
                ProductCode   = sellRequest.ProductCode,
                ID            = Guid.NewGuid()
            };
            await _storeTransactionAction.Execute(transaction);

            return(new Sale()
            {
                Message = "Success",
                Quantity = sellRequest.Quantity,
                ProductCode = sellRequest.ProductCode,
                Success = true,
                TotalValue = totalIncome,
                UnitPrice = currentPrice,
                TransactionID = transaction.ID
            });
        }
Beispiel #2
0
        public async Task <ActionResult <Purchase> > Buy([FromBody] BuyRequest buyRequest)
        {
            var account = await _accountQuery.Evaluate(buyRequest.AccountNumber);

            if (account == null)
            {
                return(NotFound("Account does not exist"));
            }

            var currentPrice = await _currentPriceQuery.Evaluate(buyRequest.ProductCode);

            if (currentPrice == DataAccess.CurrentPriceQuery.ErrorConditions.ProductDoesNotExist)
            {
                return(NotFound("Product does not exist"));
            }
            if (currentPrice == DataAccess.CurrentPriceQuery.ErrorConditions.PriceDoesNotExist)
            {
                return(BadRequest("No valid price"));
            }

            if (currentPrice > buyRequest.MaxUnitPrice)
            {
                return(new Purchase()
                {
                    ProductCode = buyRequest.ProductCode,
                    Message = $"The current price of {currentPrice} is higher then the maximum you specified of {buyRequest.MaxUnitPrice}.",
                    Success = false,
                    UnitPrice = currentPrice,
                });
            }

            var availableCash = account.OpeningCash + account.TotalFromTransactions;
            var totalCost     = currentPrice * buyRequest.Quantity;

            if (totalCost > availableCash)
            {
                return(new Purchase()
                {
                    ProductCode = buyRequest.ProductCode,
                    Message = $"The trade will cost {totalCost} but you only have {availableCash}.",
                    Success = false,
                    UnitPrice = currentPrice,
                });
            }

            var transaction = new Transaction()
            {
                AccountNumber = account.AccountNumber,
                Quantity      = buyRequest.Quantity,
                Time          = DateTime.Now,
                UnitPrice     = currentPrice,
                TotalValue    = -totalCost,
                ProductCode   = buyRequest.ProductCode,
                ID            = Guid.NewGuid()
            };
            await _storeTransactionAction.Execute(transaction);

            return(new Purchase()
            {
                Message = "Success",
                Quantity = buyRequest.Quantity,
                ProductCode = buyRequest.ProductCode,
                Success = true,
                TotalValue = totalCost,
                UnitPrice = currentPrice,
                TransactionID = transaction.ID
            });
        }