public async Task <ViewResult> SellRequests()
        {
            var sellRequestViewModels = new List <GenericRequestViewModel>();

            var(jwtToken, id) = JwtHelper.GetJwtAndIdFromJwt(Request);
            var sellRequestModels = await _stockTraderBrokerClient.GetSellRequests(id, jwtToken);

            foreach (var sellRequestModel in sellRequestModels)
            {
                var stock = await _publicShareOwnerControlClient.GetStock(sellRequestModel.StockId, jwtToken);

                sellRequestViewModels.Add(new GenericRequestViewModel
                {
                    AmountOfShares  = sellRequestModel.AmountOfShares,
                    Id              = sellRequestModel.Id,
                    TimeOut         = sellRequestModel.TimeOut,
                    OfferedPrice    = sellRequestModel.Price,
                    LastTradedValue = stock.LastTradedValue,
                    Name            = stock.Name
                });
            }
            return(View("SellRequests", sellRequestViewModels));
        }
Example #2
0
        public async Task <ActionResult <ValidationResult> > SetSharesForSale(SellRequestModel sellRequestModel)
        {
            // Validate Ownership

            var stock = await _publicShareOwnerControlClient.GetStock(sellRequestModel.StockId, "jwtToken");

            var shareholder = stock.ShareHolders.FirstOrDefault(sh => sh.ShareholderId == sellRequestModel.AccountId);

            if (shareholder == null)
            {
                _logger.LogError("ShareHolder with id {id} Not Found for stock {stockId}", sellRequestModel.AccountId, sellRequestModel.StockId);
                return(new ValidationResult {
                    Valid = false, ErrorMessage = $"ShareHolder with id {sellRequestModel.AccountId} Not Found for stock {sellRequestModel.StockId}"
                });
            }

            var sellRequestModels = await _stockTraderBrokerClient.GetSellRequests(sellRequestModel.AccountId, sellRequestModel.StockId, "jwtToken");

            var sharesAlreadyForSale = sellRequestModels.Sum(model => model.AmountOfShares);

            _logger.LogInformation("ShareHolder requests {requestAmount}, and he currently owns {amount} and already have {sharesAlreadyForSale} for sale", sellRequestModel.AmountOfShares, shareholder.Amount, sharesAlreadyForSale);
            if (shareholder.Amount < sellRequestModel.AmountOfShares + sharesAlreadyForSale)
            {
                return(new ValidationResult {
                    Valid = false, ErrorMessage = $"ShareHolder requests {sellRequestModel.AmountOfShares}, but he only has {shareholder.Amount}, and already {sharesAlreadyForSale} for sale"
                });
            }

            // Set Shares for Sale
            var validationResult = await _stockTraderBrokerClient.PostSellRequest(sellRequestModel, "jwtToken");

            if (!validationResult.Valid)
            {
                _logger.LogWarning("Failed to set {Amount} shares of stock with Id {stockId} for sale", sellRequestModel.AmountOfShares, sellRequestModel.StockId);
                return(validationResult);
            }

            _logger.LogInformation("Successfully set {Amount} shares of stock with Id {stockId} for sale", sellRequestModel.AmountOfShares, sellRequestModel.StockId);

            return(new ValidationResult {
                Valid = true, ErrorMessage = ""
            });
        }