Exemplo n.º 1
0
        private bool ValidModel(CsvTransactionsModel csvModel)
        {
            bool isValidModel = true;

            foreach (var item in csvModel.TransactionsCollection)
            {
                if (item.TransactionIdentifier == null || item.TransactionIdentifier == string.Empty || item.TransactionIdentifier.Length > 50)
                {
                    isValidModel = false;
                    _logger.LogError($"Validation Error for item ID - {item.TransactionIdentifier}. Field - {item.TransactionIdentifier}");
                }
                if (item.Amount == 0)
                {
                    isValidModel = false;
                    _logger.LogError($"Validation Error for item ID - {item.TransactionIdentifier}. Field - {item.Amount}");
                }
                if (!isCurrencyCode(item.CurrencyCode))
                {
                    isValidModel = false;
                    _logger.LogError($"Validation Error for item ID - {item.TransactionIdentifier}. Field - {item.CurrencyCode}");
                }
            }

            return(isValidModel);
        }
Exemplo n.º 2
0
        private CsvTransactionsModel ParseCsv(IFormFile file)
        {
            CsvTransactionsModel csvModel = new CsvTransactionsModel();

            try
            {
                using TextFieldParser parser    = new TextFieldParser(file.OpenReadStream());
                csvModel.TransactionsCollection = new List <TransactionCsv>();

                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                while (!parser.EndOfData)
                {
                    //process row
                    string[] fields = parser.ReadFields();
                    csvModel.TransactionsCollection.Add(new TransactionCsv()
                    {
                        TransactionIdentifier = fields[0],
                        Amount          = ToInt(fields[1]),
                        CurrencyCode    = fields[2],
                        TransactionDate = Convert.ToDateTime(fields[3]),
                        Status          = (CsvStatusEnum)Enum.Parse(typeof(CsvStatusEnum), fields[4])
                    });
                }
            }
            catch (Exception e)
            {
                //log
            }
            return(csvModel);
        }