-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAccessLayer.cpp
More file actions
348 lines (283 loc) · 11.6 KB
/
DataAccessLayer.cpp
File metadata and controls
348 lines (283 loc) · 11.6 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* DataAccessLayer.cpp
*
* Created on: Aug 1, 2012
* Author: adamtraub
*/
#include "Declarations.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include "DataAccessLayer.h"
#include <typeinfo>
using namespace DAL::Redis_Keys;
using namespace Account_Values;
using namespace Debt_Values;
using namespace Income_Values;
namespace DAL{
bool notInitialized = true;
/*Maps that relate enumerators to redis database fields*/
AcctValueMap accountKeys;
DebtValueMap debtKeys;
IncomeValueMap incomeKeys;
std::string database;
int port;
/*Necessary to initialize the DAL, only needs to be called once*/
void initialize(){
if(notInitialized){
accountKeys[name] = "name";
accountKeys[email] = "email";
accountKeys[phone] = "phone";
accountKeys[monthlyEmail] = "monthlyEmail";
accountKeys[emailOnUpdate] = "emailOnUpdate";
debtKeys[debt_source] = "debt_source";
debtKeys[balance] = "balance";
debtKeys[apr] = "apr";
debtKeys[minimum_payment] = "minimum_payment";
debtKeys[extra_payment] = "extra_payment";
debtKeys[due_date] = "due_date";
debtKeys[payment_scheduled] = "payment_scheduled";
debtKeys[payment_processed] = "payment_processed";
incomeKeys[income_source] = "income_source";
incomeKeys[amount] = "amount";
incomeKeys[savings] = "savings";
database = "localhost";
port = 6379;
notInitialized = false;
}
}
/**TODO Create a clean way to check the output of redisCommands for failure
* and implement error handling on all redisCommand calls*/
/* Function used for generating a generic ID.
* First checks to see if there's an unregistered ID
* If there isn't, it creates a new one.*/
std::string createID(std::string unregisteredList, std::string incrementingID){
redisReply *reply;
std::stringstream out;
redisContext *c = redisConnect(database.c_str(), port);
reply = redisCommand(c, "LLEN %s", unregisteredList.c_str());
if(reply->integer > 0){//if there is an unregistered ID
reply = redisCommand(c, "LPOP %s", unregisteredList.c_str());
out << reply -> str;
} else {//if there are no unregistered ID's
reply = redisCommand(c, "INCR %s",incrementingID.c_str()); //create a new ID from the id counter
out << reply -> integer;
}
freeReplyObject(reply);
redisFree(c);
return out.str();
}
/*Creates a unique ID for a given user account
*returns the newly created user's id*/
std::string createAccountID(){
return createID(unregisteredUserIDs(),lastUserID());
}
/*Creates a unique ID for a given source of income for a given user*/
std::string createIncomeID(std::string accountID){
return createID(unregisteredIncomeIDs(accountID),lastIncomeID(accountID));
}
/*Creates a unique ID for a given source of debt for a given user*/
std::string createDebtID(std::string accountID){
return createID(unregisteredDebtIDs(accountID),lastDebtID(accountID));
}
/*Creates a new User in redis*/
std::string createAccount(ActtValueMap &vals){
redisContext *c = redisConnect(database.c_str(), port);
if(redisCommand(c,"HGET %s %s",idLookupHash().c_str(),vals[email].c_str())->type != REDIS_REPLY_NIL)
return "-1"; //returns an invalid ID to signify an error, namely that the email already belongs to a registered user
std::string accountID = createAccountID();//accountID for this user.
for ( AcctValueMap::iterator it = vals.begin(); it != vals.end(); it++)
{
redisCommand(c, "HSET %s %s %s",accountInfo(accountID).c_str(), accountKeys[it->first].c_str(), vals[it->first].c_str());
}
//Add email and account info to id lookup table
redisCommand(c, "HSET %s %s %s",idLookupHash().c_str(), vals[email].c_str(), accountID.c_str());//table for getting accountID from email
redisFree(c);
return accountID;
}
/*Adds a source of debt to a given user in Redis*/
std::string addDebt(std::string accountID, DebtValueMap &vals){
std::string debtID = createDebtID(accountID);
redisContext *c = redisConnect(database.c_str(), port);
for ( DebtValueMap::iterator it = vals.begin(); it != vals.end(); it++)
{
redisCommand(c,"HSET %s %s %s",debtInfo(accountID, debtID).c_str(), debtKeys[it->first].c_str(), vals[it->first].c_str());
}
redisFree(c);
return debtID;
}
/*Adds a source of income to a given user in Redis*/
std::string addIncome(std::string accountID, IncomeValueMap &vals){
std::string incomeID = createIncomeID(accountID);
redisContext *c = redisConnect(database.c_str(), port);
for ( IncomeValueMap::iterator it = vals.begin(); it != vals.end(); it++)
{
redisCommand(c, "HSET %s %s %s", incomeInfo(accountID, incomeID).c_str(), incomeKeys[it->first].c_str(), vals[it->first].c_str());
}
redisFree(c);
return incomeID;
}
/*gets a user ID from an email address*/
std::string getIDFromEmail(std::string email){
redisContext *c = redisConnect(database.c_str(), port);
std::string id = hGetAndCorrect(c,idLookupHash(), email);
redisFree(c);
return id;
}
/* Deletes a user from redis including:
* his account information
* his debt information
* his income information*/
void deleteAccount(std::string accountID){
redisReply *mainReply, *emailReply ;
std::string item;
std::string email;
redisContext *c = redisConnect(database.c_str(), port);
mainReply = redisCommand(c, "KEYS account:%s:*",accountID.c_str());
bool userExists = mainReply->elements > 0;
if(userExists){
emailReply = redisCommand(c, "HGET %s email",accountInfo(accountID).c_str());
if(emailReply->type == REDIS_REPLY_STRING) {
email = emailReply ->str;
} else {
//TODO: LOG AN ERROR! The email wasn't in the lookup table!
}
//delete all user info
for(unsigned int i=0; i < mainReply->elements; i++){
item = mainReply->element[i] ->str;
redisCommand(c, "DEL %s",item.c_str());
}
//add ID to unregistered id list
redisCommand(c, "LPUSH %s %s",unregisteredUserIDs().c_str(), accountID.c_str());
//remove email from lookup table
redisCommand(c, "HDEL %s %S",idLookupHash().c_str(), email.c_str());
freeReplyObject(emailReply);
}
freeReplyObject(mainReply);
redisFree(c);
}
/* Deletes a given income source for a user and adds the incomeID
* to that user's unregistered income id list.*/
void deleteIncome(std::string accountID, std::string incomeID){
redisContext *c = redisConnect(database.c_str(), port);
redisCommand(c, "DEL %s",incomeInfo(accountID, incomeID).c_str());
redisCommand(c, "LPUSH %s %s",unregisteredIncomeIDs(accountID).c_str(), incomeID.c_str());
redisFree(c);
}
/* deletes a given debt source for a user and adds the debtID
* to that user's unregistered debt id list.*/
void deleteDebt(std::string accountID, std::string debtID){
redisContext *c = redisConnect(database.c_str(), port);
redisCommand(c, "DEL %s",debtInfo(accountID, debtID).c_str());
redisCommand(c, "LPUSH %s %s",unregisteredDebtIDs(accountID).c_str(), debtID.c_str());
redisFree(c);
}
/**
* Given an ID and a map of accountValue String pairs,
* this function updates a corresponding account*/
bool updateAccount(std::string accountID, AcctValueMap &vals){
bool isbadEmail = false;
redisContext *c = redisConnect(database.c_str(), port);
for ( AcctValueMap::iterator it = vals.begin(); it != vals.end(); it++)
{
if(it->first != email) { //if the value we're looking at is NOT email
redisCommand(c, "HSET %s %s %s",accountInfo(accountID).c_str(), accountKeys[it->first].c_str(), vals[it->first].c_str());
} else { //if we're trying to update the email
//if the new email isn't already in the lookup hash and the new email isn't blank
if((redisCommand(c,"HGET %s %s",idLookupHash().c_str() ,vals[email].c_str())->type == REDIS_REPLY_NIL) && (vals[email].compare("") != 0)){
//get the old email address
std::string oldEmail = redisCommand(c, "HGET %s email",accountInfo(accountID).c_str())->str;
//remove old email address from lookuphash
redisCommand(c,"HDEL %s %s",idLookupHash().c_str(), oldEmail.c_str());
//update the account's email
redisCommand(c, "HSET %s %s %s",accountInfo(accountID).c_str(), accountKeys[email].c_str(), vals[email].c_str());
//update the lookupHash's entry for the new email
redisCommand(c,"HSET %s %s %s",idLookupHash().c_str(), vals[email].c_str(), accountID.c_str());
} else {
isbadEmail = true;
}
}
}
redisFree(c);
return isbadEmail;
}
/*Updates debt info*/
void updateDebt(std::string accountID, std::string debtID, DebtValueMap &vals){
redisContext *c = redisConnect(database.c_str(), port);
for ( DebtValueMap::iterator it = vals.begin(); it != vals.end(); it++)
{
redisCommand(c,"HSET %s %s %s",debtInfo(accountID, debtID).c_str(), debtKeys[it->first].c_str(), vals[it->first].c_str());
}
redisFree(c);
}
/*Updates income Info*/
void updateIncome(std::string accountID, std::string incomeID, IncomeValueMap &vals){
redisContext *c = redisConnect(database.c_str(), port);
for ( IncomeValueMap::iterator it = vals.begin(); it != vals.end(); it++)
{
redisCommand(c, "HSET %s %s %s", incomeInfo(accountID, incomeID).c_str(), incomeKeys[it->first].c_str(), vals[it->first].c_str());
}
redisFree(c);
}
AcctValueMap getAccountInfo(std::string accountID){
redisContext *c = redisConnect(database.c_str(), port);
AcctValueMap accountData;
for ( AcctValueMap::iterator it = accountKeys.begin(); it != accountKeys.end(); it++){
accountData[it->first] = hGetAndCorrect(c, accountInfo(accountID), accountKeys[it->first]);
}
redisFree(c);
return accountData;
}
DebtValMapVec getDebtInfo(std::string accountID){
redisContext *c = redisConnect(database.c_str(), port);
DebtValMapVec debtData;
redisReply *debtNames;
debtNames = redisCommand(c, "KEYS %s", debtInfo(accountID,"*").c_str());
for(unsigned int i=0; i < debtNames->elements; i++){
DebtValueMap tempMap; //purposely re-define the variable at the loop scope each iteration
for ( std::map<DebtValues, std::string>::iterator it = debtKeys.begin(); it != debtKeys.end(); it++){
tempMap[it->first] = hGetAndCorrect(c, debtNames->element[i]->str, debtKeys[it->first]);
}
debtData.push_back(tempMap);
}
redisFree(c);
return debtData;
}
IncomeValMapVec getIncomeInfo(std::string accountID){
redisContext *c = redisConnect(database.c_str(), port);
IncomeValMapVec incomeData;
redisReply *incomeNames;
std::cout<<"CONNECTED TO REDIS"<<std::endl;
std::cout<< redisCommand(c, "ECHO HELLO FROM REDIS!")->str <<std::endl;
incomeNames = redisCommand(c, "KEYS %s", incomeInfo(accountID,"*").c_str());
for(unsigned int i=0; i < incomeNames->elements; i++){
<<<<<<< HEAD
std::cout<<DEBUG++<<std::endl;
IncomeValueMap tempMap; //purposely re-define the variable at the loop scope each iteration
for ( IncomeValueMap::iterator it = incomeKeys.begin(); it != incomeKeys.end(); it++){
=======
std::map<IncomeValues, std::string> tempMap; //purposely re-define the variable at the loop scope each iteration
for ( std::map<IncomeValues, std::string>::iterator it = incomeKeys.begin(); it != incomeKeys.end(); it++){
>>>>>>> aca81cad0da77a4dc723f6c85d8a0fe25278c5f9
tempMap[it->first] = hGetAndCorrect(c, incomeNames->element[i]->str, incomeKeys[it->first]);
}
incomeData.push_back(tempMap);
}
redisFree(c);
return incomeData;
}
std::string hGetAndCorrect(redisContext *c, std::string hash, std::string key){
redisReply *reply;
reply = redisCommand(c, "HGET %s %s",hash.c_str(), key.c_str());
std::string output;
if(reply->type == REDIS_REPLY_NIL){
output = "NA";
//TODO log an error! Missing value
redisCommand(c, "HSET %s %s %s",hash.c_str(), key.c_str(), output.c_str());
} else {
output = reply->str;
}
freeReplyObject(reply);
return output;
}
}