-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserManager.cs
215 lines (199 loc) · 7.96 KB
/
UserManager.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
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace DataAccessLayer
{
public class UserManager
{
static string dbFile = "mfdata.db";
static public string GetDataFileFullName()
{
string sCurrentDir = AppDomain.CurrentDomain.BaseDirectory;
string sFile = System.IO.Path.Combine(sCurrentDir, dbFile);
//string sFilePath = Path.GetFullPath(sFile);
string sFilePath = Path.GetPathRoot(sFile) + @"ProdDatabase\" + dbFile;
return sFilePath;
}
/// <summary>
/// metho that return rootfolder\ProdDatabase\
/// </summary>
/// <returns></returns>
static public string GetDataFolder()
{
string sCurrentDir = AppDomain.CurrentDomain.BaseDirectory;
string sFile = System.IO.Path.Combine(sCurrentDir, dbFile);
//string sFilePath = Path.GetFullPath(sFile);
string sDataFolder = Path.GetPathRoot(sFile) + @"ProdDatabase\StockDataFiles\";
return sDataFolder;
}
public SQLiteConnection CreateConnection()
{
SQLiteConnection sqlite_conn = null;
//string sCurrentDir = AppDomain.CurrentDomain.BaseDirectory;
//string sFile = System.IO.Path.Combine(sCurrentDir, dbFile);
////string sFilePath = Path.GetFullPath(sFile);
//string sFilePath = Path.GetPathRoot(sFile) + @"ProdDatabase\" + dbFile;
string sFilePath = UserManager.GetDataFileFullName();
// Create a new database connection:
//sqlite_conn = new SQLiteConnection(@"Data Source= E:\MSFT_SampleWork\Analytics\portfolio\MFData\mfdata.db; " +
// " Version = 3; FailIfMissing=True; Foreign Keys=True; New = True; Compress = True; ");
sqlite_conn = new SQLiteConnection("Data Source=" + sFilePath +
"; Version = 3; FailIfMissing=True; Foreign Keys=True; New = True; Compress = True; PRAGMA synchronous=OFF;");
// Open the connection:
try
{
sqlite_conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw (ex);
}
return sqlite_conn;
}
/// <summary>
/// Method to check if userid exists in USERMASTER or NOT
/// </summary>
/// <param name="userid">userid to check</param>
/// <param name="sqlite_cmd"></param>
/// <returns>ROWID of the matching userid else 0</returns>
public long CheckUserExists(string userid, string password = null, SQLiteCommand sqlite_cmd = null)
{
long usermaster_rowid = 0;
SQLiteConnection sqlite_conn = null;
SQLiteDataReader sqlite_datareader = null;
SQLiteTransaction transaction = null;
try
{
if (sqlite_cmd == null)
{
sqlite_conn = CreateConnection();
sqlite_cmd = sqlite_conn.CreateCommand();
transaction = sqlite_conn.BeginTransaction();
}
if (password == null)
{
sqlite_cmd.CommandText = "SELECT ROWID FROM USERMASTER WHERE USERID = '" + userid + "'";
}
else
{
sqlite_cmd.CommandText = "SELECT ROWID FROM USERMASTER WHERE USERID = '" + userid + "' AND PASSWORD = '" + password + "'";
}
try
{
sqlite_datareader = sqlite_cmd.ExecuteReader();
if (sqlite_datareader.HasRows)
{
if (sqlite_datareader.Read())
{
//sqlite_datareader.Read();
usermaster_rowid = Int64.Parse(sqlite_datareader["ROWID"].ToString());
}
}
}
catch (SQLiteException exSQL)
{
Console.WriteLine("getPortfoliId: [" + userid + "]" + exSQL.Message);
}
}
catch (Exception ex)
{
Console.WriteLine("getPortfoliId: [" + userid + "]" + ex.Message);
}
finally
{
if (sqlite_datareader != null)
{
sqlite_datareader.Close();
sqlite_datareader = null;
}
if (sqlite_conn != null)
{
if (transaction != null)
{
transaction.Commit();
transaction.Dispose();
}
if (sqlite_cmd != null)
{
sqlite_cmd.Dispose();
}
sqlite_conn.Close();
sqlite_conn.Dispose();
transaction = null;
sqlite_cmd = null;
sqlite_conn = null;
}
sqlite_datareader = null; ;
}
return usermaster_rowid;
}
/// <summary>
/// Method to register a user in the system. It will allow new user registration only if that userid does not exists in USERMASTER
/// </summary>
/// <param name="userid">user id to be registered</param>
/// <param name="password">encrypted password</param>
/// <returns>ROWID from USERMASTER for the row that was created else 0</returns>
public long RegisterUser(string userid, string password)
{
long usermaster_id = 0;
SQLiteConnection sqlite_conn = null;
SQLiteCommand sqlite_cmd = null;
try
{
sqlite_conn = CreateConnection();
sqlite_cmd = sqlite_conn.CreateCommand();
var transaction = sqlite_conn.BeginTransaction();
try
{
//first check if userid exists
if (CheckUserExists(userid, password, sqlite_cmd) <= 0)
{
sqlite_cmd.CommandText = "INSERT OR IGNORE INTO USERMASTER(USERID, PASSWORD) VALUES (@USERID, @PASSWORD)";
sqlite_cmd.Prepare();
sqlite_cmd.Parameters.AddWithValue("@USERID", userid);
sqlite_cmd.Parameters.AddWithValue("@PASSWORD", password);
if (sqlite_cmd.ExecuteNonQuery() > 0)
{
usermaster_id = CheckUserExists(userid, password, sqlite_cmd);
}
}
}
catch (SQLiteException exSQL)
{
Console.WriteLine("CreateNewPortfolio: " + userid + exSQL.Message);
usermaster_id = -1;
}
transaction.Commit();
transaction.Dispose();
transaction = null;
}
catch
{
usermaster_id = -1;
}
finally
{
if (sqlite_cmd != null)
{
sqlite_cmd.Dispose();
}
if (sqlite_conn != null)
{
sqlite_conn.Close();
sqlite_conn.Dispose();
}
sqlite_cmd = null;
sqlite_conn = null;
}
return usermaster_id;
}
}
}