Skip to content
Snippets Groups Projects
Commit 81f4f342 authored by Radek Puš's avatar Radek Puš
Browse files

import - parsing char ';'

fix - out of range exception for account parsing
parent 9db382e9
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,5 @@ namespace Core.BusinessLogic.ImportCSVLogic
public interface IModelImporter
{
int Import(string filePath, string fileName, long userID);
int SaveRecords(long userID, string fileName, IEnumerable<DocumentLayout> records);
}
}
\ No newline at end of file
......@@ -24,24 +24,54 @@ namespace Core.BusinessLogic.ImportCSVLogic
 
public int Import(string filePath, string fileName, long userID)
{
string delimiter = GetDelimiter(filePath);
if (string.IsNullOrEmpty(delimiter))
return -1;
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader))
using (var csv = MakeReader(reader,delimiter))
{
csv.Configuration.BadDataFound = null;
csv.Configuration.HeaderValidated = null;
csv.Configuration.Delimiter = ",";
csv.Configuration.PrepareHeaderForMatch = (string header, int index) => header.ToLower();
csv.Configuration.MissingFieldFound = (headerNames, index, context) =>
{
Console.WriteLine($"Field with names ['{string.Join("', '", headerNames)}'] at index '{index}' was not found.");
};
ModelImporter importer = new ModelImporter(model);
return importer.SaveRecords(userID, fileName, csv.GetRecords<DocumentLayout>());
}
}
 
public int SaveRecords(long userID, string fileName, IEnumerable<DocumentLayout> records)
#region csvReader settings
private string GetDelimiter(string filePath)
{
string[] delimiters = new string[] { ",", ";" };
foreach (string delimiter in delimiters)
{
using (var reader = new StreamReader(filePath))
using (var csv = MakeReader(reader,delimiter))
{
csv.Read();
csv.ReadHeader();
if (csv.Context.HeaderRecord.Count() > 3)
return delimiter;
}
}
return "";
}
private CsvReader MakeReader(StreamReader stream, string delimiter)
{
var csvReader = new CsvReader(stream);
csvReader.Configuration.BadDataFound = null;
csvReader.Configuration.HeaderValidated = null;
csvReader.Configuration.Delimiter = delimiter;
csvReader.Configuration.PrepareHeaderForMatch = (string header, int index) => header.ToLower();
csvReader.Configuration.MissingFieldFound = (headerNames, index, context) =>
{
Console.WriteLine($"Field with names ['{string.Join("', '", headerNames)}'] at index '{index}' was not found.");
};
return csvReader;
}
#endregion
internal int SaveRecords(long userID, string fileName, IEnumerable<DocumentLayout> records)
{
User user = model.Users
.Include(u => u.Files)
......@@ -55,10 +85,10 @@ namespace Core.BusinessLogic.ImportCSVLogic
 
try
{
if (!records.Any()) //throw reading exception for unsopported data
if (!records.Any()) //throw reading exception for unsupported data
throw new ArgumentException("file not supported");
}
catch (Exception) { return 0; } //no members are mapped exception
catch (Exception) { return -1; } //no members are mapped exception
 
 
Models.File file = new Models.File() { Name = fileName, Transactions = new List<Transaction>() };
......
......@@ -70,7 +70,7 @@ namespace Core.Controllers
 
int parsedCount = await SaveAndImport(formFile, filePath, userID); // process uploaded files
 
if (parsedCount == 0)
if (parsedCount < 1)
return BadRequest("File could not be parsed");
 
return Ok(new { count = parsedCount, formFile.FileName });
......
......@@ -34,6 +34,9 @@ namespace Core.Models
string[] splitedString = accountNumber.Split(new char[] { '-', '/' });
if (splitedString.Length < 2) //does not have account number or bank code
return null;
if (!int.TryParse(splitedString.Last(), out int parsedBankCode))
return null;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment