This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceSession.cpp
132 lines (116 loc) · 3.71 KB
/
ServiceSession.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "ServiceSession.h"
#include "..\common\StringHelper.h"
#include "FileHelper.h"
//#include "..\common\model\User.h"
//#include "..\common\model\Trader.h"
//#include "..\common\model\UserDetails.h"
//#include "..\common\model\Portfolio.h"
//#include "..\common\model\Transaction.h"
//#include "..\common\model\Stock.h"
#include "..\common\model\Models.h"
#include "CSVParser\CSVParser.h"
#include <Wt/WDateTime>
#include <Wt/Dbo/FixedSqlConnectionPool>
#include <Wt/Dbo/backend/Sqlite3>
#define ADMIN_USERNAME "admin"
#define ADMIN_PASSWORD "admin123"
ServiceSession::ServiceSession(dbo::SqlConnectionPool& connectionPool)
: _connectionPool(connectionPool),
_userDataAccess(*this),
_stockDataAccess(_userDataAccess, *this)
{
setConnectionPool(_connectionPool);
_connectionPool.getConnection();
mapClass<User>("users");
mapClass<UserDetails>("userdetails");
mapClass<Portfolio>("portfolio");
mapClass<Trader>("trader");
mapClass<Transaction>("transaction");
mapClass<Stock>("stock");
try
{
// Create the tables, if not exist
dbo::Transaction t(*this);
createTables();
t.commit();
}
catch (std::exception& e)
{
//std::cerr << e.what() << std::endl;
std::cerr << "Using existing database" << std::endl;
return;
}
// Try to create a user
try
{
// Create the default user
_userDataAccess.createNewUser(ADMIN_USERNAME, copmuteMd5(computeSHA1(ADMIN_PASSWORD)), User::Admin);
// Create the user details
_userDataAccess.createUserDetails(ADMIN_USERNAME, copmuteMd5(computeSHA1(ADMIN_PASSWORD)),
"Admin", "", "admin@cs.com", "Brazil", "Sao Paulo", 30);
}
catch (std::exception& e)
{
//std::cerr << e.what() << std::endl;
std::cerr << "Impossible to create an user";
}
// try to create the stocks
try
{
std::string fileName = "output.txt";
if (fileExist(fileName))
{
CSVParser* csvParser = new CSVParser(fileName);
csvParser->parseCsv(';');
// Naviagete into the vector matrix to get all the csv data
for (int i = 0;i < csvParser->getCsvContent().size();i++)
{
Wt::WDateTime lastUpdate = Wt::WDateTime(toPosixDate(csvParser->getCsvContent()[i][7], "%m/%d/%y"));
try
{
_stockDataAccess.insertNewStock(
ADMIN_USERNAME,
copmuteMd5(computeSHA1(ADMIN_PASSWORD)),
csvParser->getCsvContent()[i][0], // Stock code
csvParser->getCsvContent()[i][1], // Company name
toDouble(csvParser->getCsvContent()[i][2]), // Last value
toDouble(csvParser->getCsvContent()[i][3]), // Value change
toDouble(csvParser->getCsvContent()[i][4]), // Value change percent
toDouble(csvParser->getCsvContent()[i][5]), // Volume
toDouble(csvParser->getCsvContent()[i][6]), // Price volume
lastUpdate); // Last update
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
std::cerr << "Fail to insert stock register" << std::endl;
}
}
delete csvParser;
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
std::cerr << "Impossible to create the stocks";
}
}
ServiceSession::~ServiceSession()
{
}
// Return the instance of UserDataAccess
UserDataAccess& ServiceSession::getUserDataAccess()
{
return _userDataAccess;
}
StockDataAccess& ServiceSession::getStockDataAccess()
{
return _stockDataAccess;
}
// Create the pool of connections
dbo::SqlConnectionPool* ServiceSession::createConnectionPool(const std::string& sqliteDb)
{
dbo::backend::Sqlite3* connection = new dbo::backend::Sqlite3(sqliteDb);
connection->setDateTimeStorage(Wt::Dbo::SqlDateTime, Wt::Dbo::backend::Sqlite3::PseudoISO8601AsText);
return new dbo::FixedSqlConnectionPool(connection, 30);
}