forked from petrmac/NTResultsImport
/
InstrumentsRepository.cs
70 lines (63 loc) · 2.21 KB
/
InstrumentsRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace PetrMacek.ResultsImport
{
public class CannotLoadInstrumentsException : Exception
{
public CannotLoadInstrumentsException(string message)
: base(message)
{
}
}
public class InstrumentsRepository
{
private Dictionary<string, Instrument> instrumentsMap;
public InstrumentsRepository()
{
instrumentsMap = new Dictionary<string, Instrument>();
List<Instrument> list = loadInstruments();
foreach (Instrument i in list)
{
instrumentsMap.Add(i.Code, i);
}
}
public Instrument GetInstrument(string code)
{
if (code == null)
{
throw new ArgumentException("Instrument code cannot be empty");
}
if (code.IndexOf(' ') > 0)
{
code = code.Split(' ')[0];
}
if (!instrumentsMap.Keys.Contains(code))
{
throw new ArgumentException(string.Format("The instrument you are trying to load ({0}) is not available. Please modify the instrument-list.txt accordingly.", code));
}
return instrumentsMap[code];
}
public List<Instrument> loadInstruments()
{
List<Instrument> instruments;
try
{
using (FileStream fStream = File.Open("instrument-list.txt", FileMode.OpenOrCreate))
{
var csv = new CsvHelper.CsvHelper(fStream);
csv.Configuration.Delimiter = ';';
csv.Configuration.UseInvariantCulture = true;
instruments = csv.Reader.GetRecords<Instrument>().ToList();
}
return instruments;
}
catch (Exception e)
{
throw new CannotLoadInstrumentsException(string.Format("There is some problem starting application. Cannot load the instrumet list: {0}", e.Message));
}
}
}
}