Exemplo n.º 1
0
        public async Task <ImportResultViewModel> ProcessFile(Stream stream)
        {
            var result = new ImportResultViewModel
            {
                LinesImported = 0,
                LinesIgnored  = new List <string>()
            };

            var linesProcessed = 1;

            using (var reader = new CSVTransactionReader(stream))
            {
                var tran = new TransactionViewModel();
                while (await reader.ReadTransactionAsync(tran))
                {
                    TransactionValidation.Validate(tran);
                    if (tran.IsValid)
                    {
                        result.LinesImported++;
                        _repository.Add(tran.ToTransaction());
                    }
                    else
                    {
                        result.LinesIgnored.Add($"{linesProcessed} ignored, Reason: {tran.ValidationMessage}.");
                    }
                    linesProcessed++;
                }

                await _repository.SaveChangesAsync();

                reader.Close();
            }

            return(result);
        }
Exemplo n.º 2
0
 public static Transaction ToTransaction(this TransactionViewModel model)
 {
     TransactionValidation.Validate(model);
     return(new Transaction
     {
         Account = model.Account,
         Amount = model.IsValid ? decimal.Parse(model.Amount) : 0,
         CurrencyCode = model.CurrencyCode,
         Description = model.Description,
         ID = model.ID
     });
 }
Exemplo n.º 3
0
        public async Task <ActionResult> EditTransaction(TransactionViewModel model)
        {
            TransactionValidation.Validate(model);
            if (model.IsValid)
            {
                await _transactionService.SaveTransaction(model);

                return(RedirectToAction("Transactions"));
            }

            ModelState.AddModelError("", model.ValidationMessage);
            return(View(model));
        }
Exemplo n.º 4
0
        void ValidateTransationObject(Transaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException();
            }

            var validator = new TransactionValidation();
            var results   = validator.Validate(transaction);

            if (!results.IsValid)
            {
                throw new ArgumentException();
            }
        }