Esempio n. 1
0
        public ActionResult ImportTransactions(ImportTransactionsViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var accountId = model.AccountId;
                    using (var reader = new StreamReader(model.Transactions.OpenReadStream()))
                        using (var csv = new CsvReader(reader))
                        {
                            csv.Configuration.Delimiter       = ",";
                            csv.Configuration.HasHeaderRecord = true;
                            var good        = new List <RabobankTransactions>();
                            var bad         = new List <string>();
                            var isRecordBad = false;
                            csv.Configuration.BadDataFound = context =>
                            {
                                isRecordBad = true;
                                bad.Add(context.RawRecord);
                            };
                            while (csv.Read())
                            {
                                var record = csv.GetRecord <RabobankTransactions>();
                                if (!isRecordBad)
                                {
                                    record.TimeOfTransaction.ToString("yyyy-MM-dd");
                                    good.Add(record);
                                }

                                isRecordBad = false;
                            }
                            _transactionLogic.ImportTransactions(good, accountId);
                            good.Clear();
                            bad.Clear();
                            _accountLogic.RefreshAccountBalance(accountId);
                        }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            return(RedirectToAction("Index", "Transaction", new { id = model.AccountId }));
        }