Advertisement
Guest User

Untitled

a guest
May 14th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.64 KB | None | 0 0
  1. using ParkingApp.Business.Constants;
  2. using ParkingApp.Business.Entities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7.  
  8. namespace ParkingApp.Data.DBOperations
  9. {
  10.  
  11. public class UserDBOperations
  12. {
  13. // swap DBs around here
  14.  
  15. private static String connectionString = Keys.connectionString;
  16. private SqlDataAdapter dataAdapter = new SqlDataAdapter();
  17. private String query;
  18. private int result;
  19. private static UserDBOperations instance;
  20. private UserDBOperations() { }
  21. public static UserDBOperations SharedInstance
  22. {
  23. get
  24. {
  25. if (instance == null)
  26. {
  27. instance = new UserDBOperations();
  28. }
  29. return instance;
  30. }
  31. }
  32.  
  33. public bool login(String email, String password)
  34. {
  35.  
  36. SqlConnection connection = new SqlConnection();
  37. SqlCommand command = new SqlCommand();
  38. DataTable usersTable = new DataTable();
  39. try
  40. {
  41.  
  42. connection.ConnectionString = connectionString;
  43. query = "SELECT * FROM [ParkingAppDB].[dbo].[User] WHERE EMAIL = @email AND PASSWORD = @password";
  44. command.Parameters.AddWithValue("@email", email);
  45. command.Parameters.AddWithValue("@password", password);
  46. connection.Open();
  47. command.Connection = connection;
  48. command.CommandText = query;
  49. dataAdapter.SelectCommand = command;
  50. dataAdapter.Fill(usersTable);
  51. connection.Close();
  52. if (usersTable.Rows.Count > 0)
  53. {
  54. dataAdapter.Dispose();
  55. return true;
  56. }
  57.  
  58. }
  59. catch (Exception ex)
  60. {
  61. Console.WriteLine(ex.Message);
  62. }
  63. finally
  64. {
  65. dataAdapter.Dispose();
  66.  
  67. }
  68. return false;
  69. }
  70.  
  71. public bool create(String email, String password, String role, String phoneNb, bool isBlocked, int bookings)
  72. {
  73. SqlConnection connection = new SqlConnection();
  74. SqlCommand command = new SqlCommand();
  75. connection.ConnectionString = connectionString;
  76. int isBlk = 0;
  77. if (isBlocked) {
  78. isBlk = 1;
  79. }
  80. try
  81. {
  82. connection.Open();
  83. query = "INSERT INTO [ParkingAppDB].[dbo].[User] (EMAIL,PASSWORD,ROLE,PHONENB,ISBLOCKED,BOOKINGS) VALUES(@email,@password,@role,@phoneNb,@isBlocked,@bookings)";
  84. command.Parameters.AddWithValue("@email", email);
  85. command.Parameters.AddWithValue("@password", password);
  86. command.Parameters.AddWithValue("@role", role);
  87. command.Parameters.AddWithValue("@phoneNb", phoneNb);
  88. command.Parameters.AddWithValue("@isBlocked", isBlk);
  89. command.Parameters.AddWithValue("@bookings", bookings);
  90. command.Connection = connection;
  91. command.CommandText = query;
  92. result = command.ExecuteNonQuery();
  93. if (result > 0)
  94. {
  95. Console.WriteLine("User created successfully!");
  96. return true;
  97. }
  98. else
  99. {
  100. Console.WriteLine("err");
  101. }
  102. connection.Close();
  103. }
  104. catch (Exception ex)
  105. {
  106. Console.WriteLine(ex.Message);
  107. }
  108. return false;
  109. }
  110.  
  111. public void update(User user)
  112. {
  113. SqlConnection connection = new SqlConnection();
  114. SqlCommand command = new SqlCommand();
  115. connection.ConnectionString = connectionString;
  116. String userRole = "";
  117. int isBlocked = 0;
  118. if (user.IsBlocked)
  119. {
  120. isBlocked = 1;
  121. }
  122. switch (user.Role) {
  123. case Business.Constants.Roles.admin:
  124. {
  125. userRole = "admin";
  126. break;
  127. }
  128. case Business.Constants.Roles.powerUser:
  129. {
  130. userRole = "powerUser";
  131. break;
  132. }
  133. case Business.Constants.Roles.user:
  134. {
  135. userRole = "user";
  136. break;
  137. }
  138. }
  139. try
  140. {
  141.  
  142. connection.Open();
  143. query = "UPDATE [ParkingAppDB].[dbo].[User] SET EMAIL= @email, PASSWORD = @password, ROLE = @role, PHONENB = @phoneNb, ISBLOCKED = @isBlocked, BOOKINGS = @bookings WHERE ID = @id";
  144. command.Parameters.AddWithValue("@email", user.EmailAddress);
  145. command.Parameters.AddWithValue("@password", user.Password);
  146. command.Parameters.AddWithValue("@id", user.Id);
  147. command.Parameters.AddWithValue("@role", userRole);
  148. command.Parameters.AddWithValue("@phoneNb", user.PhoneNumber);
  149. command.Parameters.AddWithValue("@isBlocked", isBlocked);
  150. command.Parameters.AddWithValue("@bookings", user.Bookings);
  151. command.Connection = connection;
  152. command.CommandText = query;
  153. result = command.ExecuteNonQuery();
  154. if (result > 0)
  155. {
  156. Console.WriteLine("updated");
  157. }
  158. else
  159. {
  160. Console.WriteLine("err");
  161. }
  162. connection.Close();
  163. }
  164. catch (Exception ex)
  165. {
  166. Console.WriteLine(ex.Message);
  167. }
  168.  
  169. }
  170.  
  171. public List<User> readAll()
  172. {
  173. SqlConnection connection = new SqlConnection();
  174. SqlCommand command = new SqlCommand();
  175. DataTable usersTable = new DataTable();
  176. List<User> users = new List<User>();
  177. try
  178. {
  179.  
  180. connection.ConnectionString = connectionString;
  181. query = "SELECT ID as 'Id',EMAIL as 'Email',PASSWORD as 'Password', ROLE as 'Role', PHONENB as 'PhoneNb', ISBLOCKED as 'IsBlocked', BOOKINGS as 'Bookings' FROM [ParkingAppDB].[dbo].[User]";
  182. connection.Open();
  183. command.Connection = connection;
  184. command.CommandText = query;
  185. dataAdapter.SelectCommand = command;
  186. dataAdapter.Fill(usersTable);
  187. connection.Close();
  188. foreach (DataRow row in usersTable.Rows)
  189. {
  190. int id = (int)row["Id"];
  191. string email = row["Email"].ToString();
  192. string password = row["Password"].ToString();
  193. string roleStr = row["Role"].ToString();
  194. string phoneNb = row["PhoneNb"].ToString();
  195. int isBlocked = (int)row["IsBlocked"];
  196. int bookings = (int)row["Bookings"];
  197. bool isBlk = false;
  198. if (isBlocked != 0)
  199. {
  200. isBlk = true;
  201. }
  202. Roles role = new Roles();
  203. if (roleStr == "admin")
  204. {
  205. role = Roles.admin;
  206. }
  207. if (roleStr == "powerUser")
  208. {
  209. role = Roles.powerUser;
  210. }
  211. if (roleStr == "user")
  212. {
  213. role = Roles.user;
  214. }
  215. User userRow = new User(id, role, email, password, phoneNb, isBlk, bookings);
  216. users.Add(userRow);
  217. }
  218. return users;
  219. }
  220. catch (Exception ex)
  221. {
  222. Console.WriteLine(ex.Message);
  223. return users;
  224. }
  225. finally
  226. {
  227. dataAdapter.Dispose();
  228.  
  229. }
  230. }
  231.  
  232. public User readById(Nullable<int> id)
  233. {
  234. User foundUser = new User();
  235. SqlConnection connection = new SqlConnection();
  236. SqlCommand command = new SqlCommand();
  237. DataTable usersTable = new DataTable();
  238. List<User> resultUser = new List<User>();
  239. try
  240. {
  241.  
  242. connection.ConnectionString = connectionString;
  243. query = "SELECT ID as 'Id',EMAIL as 'Email',PASSWORD as 'Password', ROLE as 'Role', PHONENB as 'PhoneNb', ISBLOCKED as 'IsBlocked', BOOKINGS as 'Bookings', EMAIL as 'Email' FROM [ParkingAppDB].[dbo].[User] WHERE ID = @id";
  244. command.Parameters.AddWithValue("@id", id);
  245. connection.Open();
  246. command.Connection = connection;
  247. command.CommandText = query;
  248. dataAdapter.SelectCommand = command;
  249. dataAdapter.Fill(usersTable);
  250. connection.Close();
  251. foreach (DataRow row in usersTable.Rows)
  252. {
  253. string email = row["Email"].ToString();
  254. string password = row["Password"].ToString();
  255. string roleStr = row["Role"].ToString();
  256. string phoneNb = row["PhoneNb"].ToString();
  257. int isBlocked = (int)row["IsBlocked"];
  258. int bookings = (int)row["Bookings"];
  259. bool isBlk = false;
  260. if (isBlocked != 0)
  261. {
  262. isBlk = true;
  263. }
  264. Roles role = new Roles();
  265. switch (roleStr)
  266. {
  267. case "admin":
  268. {
  269. role = Roles.admin;
  270. break;
  271. }
  272. case "powerUser":
  273. {
  274. role = Roles.powerUser;
  275. break;
  276. }
  277. default:
  278. {
  279. role = Roles.user;
  280. break;
  281. }
  282. }
  283. int idStrong = id ?? default(int);
  284. foundUser = new User(idStrong, role, email, password, phoneNb, isBlk, bookings);
  285.  
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. Console.WriteLine(ex.Message);
  291. }
  292. finally
  293. {
  294. dataAdapter.Dispose();
  295. }
  296. return foundUser;
  297. }
  298.  
  299. public List<User> searchByEmail(String email)
  300. {
  301. SqlConnection connection = new SqlConnection();
  302. SqlCommand command = new SqlCommand();
  303. DataTable usersTable = new DataTable();
  304. List<User> resultUser = new List<User>();
  305. try
  306. {
  307.  
  308. connection.ConnectionString = connectionString;
  309. query = "SELECT ID as 'Id',EMAIL as 'Email',PASSWORD as 'Password', ROLE as 'Role', PHONENB as 'PhoneNb', ISBLOCKED as 'IsBlocked', BOOKINGS as 'Bookings' FROM [ParkingAppDB].[dbo].[User] WHERE EMAIL = @email";
  310. command.Parameters.AddWithValue("@email", email);
  311. connection.Open();
  312. command.Connection = connection;
  313. command.CommandText = query;
  314. dataAdapter.SelectCommand = command;
  315. dataAdapter.Fill(usersTable);
  316. connection.Close();
  317. foreach (DataRow row in usersTable.Rows)
  318. {
  319. int id = (int)row["Id"];
  320. string password = row["Password"].ToString();
  321. string roleStr = row["Role"].ToString();
  322. string phoneNb = row["PhoneNb"].ToString();
  323. int isBlocked = (int)row["IsBlocked"];
  324. int bookings = (int)row["Bookings"];
  325. bool isBlk = false;
  326. if (isBlocked != 0)
  327. {
  328. isBlk = true;
  329. }
  330. Roles role = new Roles();
  331. switch (roleStr)
  332. {
  333. case "admin":
  334. {
  335. role = Roles.admin;
  336. break;
  337. }
  338. case "powerUser":
  339. {
  340. role = Roles.powerUser;
  341. break;
  342. }
  343. default:
  344. {
  345. role = Roles.user;
  346. break;
  347. }
  348. }
  349. User userRow = new User(id, role, email, password, phoneNb, isBlk, bookings);
  350. resultUser.Add(userRow);
  351. }
  352. return resultUser;
  353. }
  354. catch (Exception ex)
  355. {
  356. Console.WriteLine(ex.Message);
  357. return resultUser;
  358. }
  359. finally
  360. {
  361. dataAdapter.Dispose();
  362. }
  363. }
  364.  
  365. public bool delete(User user)
  366.  
  367. {
  368. SqlConnection connection = new SqlConnection();
  369. SqlCommand command = new SqlCommand();
  370. try
  371. {
  372. connection.ConnectionString = connectionString;
  373. query = "DELETE FROM [ParkingAppDB].[dbo].[User] WHERE ID = @id";
  374. connection.Open();
  375. command.Connection = connection;
  376. command.CommandText = query;
  377. command.Parameters.AddWithValue("@id", user.Id);
  378. result = command.ExecuteNonQuery();
  379. if (result > 0)
  380. {
  381. Console.WriteLine("deleted id: " + user.Id.ToString());
  382. return true;
  383. }
  384. else
  385. {
  386. Console.WriteLine("not deleted");
  387.  
  388. }
  389. connection.Close();
  390. }
  391. catch (Exception e)
  392. {
  393. Console.WriteLine(e.Message);
  394. }
  395. return false;
  396. }
  397.  
  398. public DataTable ConvertToDatatable(List<User> list)
  399. {
  400. DataTable dt = new DataTable();
  401. dt.Columns.Add("Id");
  402. dt.Columns.Add("Email");
  403. dt.Columns.Add("Phone No");
  404. dt.Columns.Add("Role");
  405. dt.Columns.Add("Blocked");
  406.  
  407. foreach (var item in list)
  408. {
  409. var row = dt.NewRow();
  410. row["Id"] = item.Id;
  411. row["Email"] = item.EmailAddress;
  412. row["Phone No"] = item.PhoneNumber;
  413. row["Role"] = item.Role.ToString();
  414. row["Blocked"] = item.IsBlocked;
  415. dt.Rows.Add(row);
  416. }
  417.  
  418. return dt;
  419. }
  420.  
  421. }
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement