public async Task <bool> UpdatePortfolioPosition(PortfolioPositionDto position, string action, int holdingId) { bool success = false; float newShareTotal = 0; int existingShares = await GetExistingShares(holdingId); if (action == "Buy") { newShareTotal = existingShares + position.Shares; } else if (action == "Sell") { newShareTotal = existingShares - position.Shares; } string sql = @"UPDATE PortfolioPosition p SET Shares = @Shares WHERE p.Id = @Id;"; object p = new { Id = holdingId, Shares = newShareTotal }; using var connection = connectionFactory.GetDbConnection(); var result = await connection.ExecuteAsync(sql : sql, commandType : CommandType.Text, param : p); if (result > 0) { success = true; } return(success); }
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; } }
public async Task <bool> AddPortfolioPosition(PortfolioPositionDto position) { bool success = false; string sql = @"INSERT INTO PortfolioPosition (PortfolioId, SecurityId, Shares) VALUES (@PortfolioId, @SecurityId, @Shares);"; object p = new { PortfolioId = position.PortfolioId, SecurityId = position.SecurityId, Shares = position.Shares }; using var connection = connectionFactory.GetDbConnection(); var result = await connection.ExecuteAsync(sql : sql, commandType : CommandType.Text, param : p); if (result > 0) { success = true; } return(success); }