Exemple #1
0
 public IResponse Process(IRequest request)
 {
     try
     {
         return(_next.Process(request));
     }
     catch (Exception e)
     {
         //TODO: log
         Console.WriteLine(e);
         throw;
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async Task <Result> Scrape(IWebDriver webDriver)
        {
            webDriver.SwitchTo().Frame(webDriver.FindElement(By.Id(Constants.HeaderFrameId)));
            webDriver.FindElement(By.CssSelector(Constants.TradeTabSelector)).Click();
            Logger.Info($"trade tab click success");
            webDriver.FindElement(By.CssSelector(TradeHistorySelector)).Click();
            Logger.Info($"trade history click success");

            webDriver.SwitchTo().ParentFrame();
            webDriver.SwitchTo().Frame(webDriver.FindElement(By.Id(Constants.MainFrameId)));
            Logger.Info($"switching to main frame success");

            var webDriverWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));

            Thread.Sleep(TimeSpan.FromSeconds(3));
            webDriverWait.Until(d => d.FindElement(By.CssSelector(MonthlyRadioSelector)));
            //if (!isMonthlyHistoryValid) throw new Exception("monthly transaction history cannot be detected");

            webDriver.FindElement(By.CssSelector(MonthlyRadioSelector)).Click();
            Logger.Info($"selecting monthly trade history");
            Thread.Sleep(TimeSpan.FromSeconds(3));
            webDriver.FindElement(By.Name(TradeHistorySubmitName)).Click();
            Logger.Info($"monthly trade history click success");

            Thread.Sleep(TimeSpan.FromSeconds(10));
            webDriverWait.Until(d => d.FindElement(By.CssSelector(TradeHistoryTableSelector)));
            // need to test for no trades
            var tables = webDriver.FindElements(By.CssSelector(TradeHistoryTableSelector));

            foreach (var table in tables)
            {
                var tbody = table.FindElement(By.TagName(TableBodyTag));
                var rows  = tbody.FindElements(By.TagName(TableRowTag));

                var transactions      = (List <Transaction>)_transactionBuilder.Build(rows);
                var transactionResult = await _transactionProcessor.Process(transactions);
            }

            return(new TaskResult {
                IsSuccessful = true
            });
        }
        public List <ExcelProcessorMessage> ProcessExcelFile(ExcelFile ef)
        {
            var errorMessages = new List <ExcelProcessorMessage>();

            var       activeWorksheet = ef.Worksheets.ActiveWorksheet;
            string    output          = string.Empty;
            CellRange range           = activeWorksheet.GetUsedCellRange(true);
            var       rowProcessed    = 0;

            for (int j = range.FirstRowIndex; j <= range.LastRowIndex; j++)
            {
                var transaction = new TransactionInput();

                for (int i = range.FirstColumnIndex; i <= range.LastColumnIndex; i++)
                {
                    ExcelCell cell = range[j - range.FirstRowIndex, i - range.FirstColumnIndex];

                    string cellName   = CellRange.RowColumnToPosition(j, i);
                    string cellRow    = ExcelRowCollection.RowIndexToName(j);
                    string cellColumn = ExcelColumnCollection.ColumnIndexToName(i);
                    if (cellColumn == "A")
                    {
                        transaction.Account = (cell.Value == null)? string.Empty : cell.Value.ToString();
                    }
                    if (cellColumn == "B")
                    {
                        transaction.Description = (cell.Value == null) ? string.Empty : cell.Value.ToString();
                    }
                    if (cellColumn == "C")
                    {
                        transaction.CurrencyCode = (cell.Value == null) ? string.Empty : cell.Value.ToString();
                    }
                    if (cellColumn == "D")
                    {
                        transaction.Amount = (cell.Value == null) ? string.Empty : cell.Value.ToString();
                    }

                    output += string.Format("Cell name: {1}{0}Cell row: {2}{0}Cell column: {3}{0}Cell value: {4}{0}",
                                            Environment.NewLine, cellName, cellRow, cellColumn, (cell.Value) ?? "Empty");
                }

                rowProcessed++;
                //ignore first transaction as that it caption
                if (rowProcessed > 1)
                {
                    var transactionStatus = _transactionProcessor.Process(transaction);

                    if (transactionStatus.Error)
                    {
                        errorMessages.Add(
                            new ExcelProcessorMessage()
                        {
                            Key       = $"Record_{j}",
                            Message   = transactionStatus.ErrorMessage,
                            IsErrored = true
                        }
                            );
                    }
                    else
                    {
                        errorMessages.Add(
                            new ExcelProcessorMessage()
                        {
                            Key       = $"Record_{j}",
                            Message   = "Record successfully processed",
                            IsErrored = false
                        }
                            );
                    }
                }
            }
            return(errorMessages);
        }