Ejemplo n.º 1
0
        public async Task <bool> AddTransactionAsync(Transaction transaction, int portfolioId)
        {
            // this probably needs to be redesigned to be wrapped in a database transaction
            try
            {
                int securityId;
                int holdingId;

                securityId = await securityRepository.CheckSecurityExists(transaction.Symbol);

                if (securityId is 0)
                {
                    securityId = await securityRepository.AddSecurity(transaction.Symbol, transaction.Description);
                }

                await transactionRepository.AddTransactionAsync(transaction, securityId);

                holdingId = await portfolioRepository.CheckIfHoldingInPortfolio();

                var position = new PortfolioPositionDto()
                {
                    PortfolioId = portfolioId,
                    SecurityId  = securityId,
                    Shares      = transaction.Quantity
                };

                if (holdingId is 0)
                {
                    await portfolioRepository.AddPortfolioPosition(position);
                }
                else
                {
                    await portfolioRepository.UpdatePortfolioPosition(position, transaction.Action, holdingId);
                }

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }