Guest User

Untitled

a guest
Dec 15th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.35 KB | None | 0 0
  1. private string GenerateArandomPass()
  2. {
  3. const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  4. StringBuilder res = new StringBuilder();
  5. Random rnd = new Random();
  6. int length = 7;
  7. while (0 < length--)
  8. {
  9. res.Append(valid[rnd.Next(valid.Length)]);
  10. }
  11. return res.ToString();
  12.  
  13. }
  14.  
  15. public void AdminEmail(string email, string randomPass)
  16. {
  17.  
  18.  
  19. MailMessage message = new MailMessage();
  20.  
  21. // Reciever's Email
  22. message.To.Add(email);
  23.  
  24. // Email Subject
  25. message.Subject = "Welcome To Our Humble Fitness Application ";
  26.  
  27. // Sender's Email
  28. message.From = new MailAddress("fitness.weightlossapp@gmail.com", "Fitness App");
  29.  
  30. // Email Body
  31. message.IsBodyHtml = true;
  32. string htmlBody = "<h3>Hi</h3><br><h3>Welcome&nbsp;to <strong><u>FitnessApp</u>,</strong></h3><br>" +
  33. "<ul><li>your password :'" + randomPass + "' </li><br><li>Change it as early as possible </li><br><li>Point 3</li><br></ul><br><p>Please contact us</p><br>";
  34. message.Body = htmlBody;
  35.  
  36.  
  37. SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
  38. smtp.EnableSsl = true;
  39. smtp.Credentials = new System.Net.NetworkCredential("fitness.weightlossapp@gmail.com", "m3leshyFitness21");
  40. smtp.Send(message);
  41. }
  42.  
  43.  
  44. public void newAdminInsertaion(string email, string password)
  45. {
  46. int adminId = 0;
  47. string type = "Admin*";
  48. string query = "Select min(PK_AdminID) from Admin ";
  49. Connection.Open();
  50. SqlCommand cmd = new SqlCommand(query, Connection);
  51. adminId = (int)cmd.ExecuteScalar();
  52. Connection.Close();
  53. string query2 = "insert into Account Values(@adminId , @email, @password , @type);";
  54. Connection.Open();
  55. SqlCommand cmd2 = new SqlCommand(query2, Connection);
  56. cmd2.Parameters.AddWithValue("@adminId", adminId);
  57. cmd2.Parameters.AddWithValue("@email", email);
  58. cmd2.Parameters.AddWithValue("@password", password);
  59. cmd2.Parameters.AddWithValue("@type", type);
  60. cmd2.ExecuteReader();
  61. Connection.Close();
  62. }
  63.  
  64. public string AdminType(int accountID )
  65. {
  66. string query = "select Type from Account where AccountID=@accountID ";
  67. Connection.Open();
  68. SqlCommand cmd = new SqlCommand(query, Connection);
  69. cmd.Parameters.AddWithValue("@accountID", accountID);
  70. SqlDataReader dr = cmd.ExecuteReader();
  71. dr.Read();
  72. string type = (string)dr["Type"];
  73. Connection.Close();
  74. return type;
  75.  
  76.  
  77. }
  78. public void AddNewAdmin(string email, string firstName , string lastName)
  79. {
  80.  
  81. string query = "insert into Admin(FirstName,LastName) values (@firstName,@lastName)";
  82. Connection.Open();
  83. SqlCommand cmd = new SqlCommand(query, Connection);
  84. cmd.Parameters.AddWithValue("@firstName", firstName);
  85. cmd.Parameters.AddWithValue("@lastName", lastName);
  86. cmd.ExecuteReader();
  87. Connection.Close();
  88. string password = GenerateArandomPass();
  89. string encryptedPassword = EncryptPassword(password);
  90. AdminEmail(email, password);
  91. newAdminInsertaion(email, encryptedPassword);
  92. }
  93.  
  94. public bool checkOldPass(int accountID, string oldPass)
  95. {
  96. string OldPassword = EncryptPassword(oldPass);
  97. string password = "";
  98. Connection.Open();
  99. string query = "Select Password from Account where AccountID=@accountID ;";
  100. SqlCommand cmd = new SqlCommand(query, Connection);
  101. cmd.Parameters.AddWithValue("@accountID", accountID);
  102. password = cmd.ExecuteScalar().ToString();
  103. Connection.Close();
  104. if (password == OldPassword)
  105. {
  106. return true;
  107.  
  108. }
  109. else
  110. {
  111. return false;
  112. }
  113. }
  114.  
  115. public void ModifyAdminPassword(int accountID, string newPass)
  116. {
  117. string newPassword = EncryptPassword(newPass);
  118. string type = "Admin";
  119. string query = "Update Account Set Password=@newPassword , Type =@type where AccountID = @accountID ";
  120. Connection.Open();
  121. SqlCommand cmd = new SqlCommand(query, Connection);
  122. cmd.Parameters.AddWithValue("@newPassword", newPassword);
  123. cmd.Parameters.AddWithValue("@type", type);
  124. cmd.Parameters.AddWithValue("@accountID", accountID);
  125. cmd.ExecuteNonQuery();
  126. Connection.Close();
  127.  
  128. }
  129.  
  130. public string NumberOFUsers()
  131. {
  132. string total = "";
  133. Connection.Open();
  134. string query = "select Count(PK_UserID) from [User];";
  135. SqlCommand cmd = new SqlCommand(query, Connection);
  136. total = cmd.ExecuteScalar().ToString();
  137. Connection.Close();
  138. return total;
  139. }
  140.  
  141.  
  142. /////////////////////////////////////////unTested //////////////////////////////
  143.  
  144. public void DeleteAfeedBack(int accountID)
  145. {
  146. string query = "delete from Feedback where FK_Feedback_UserID=@accountID";
  147. Connection.Open();
  148. SqlCommand cmd = new SqlCommand(query, Connection);
  149. cmd.Parameters.AddWithValue("@accountID", accountID);
  150. cmd.ExecuteNonQuery();
  151. Connection.Close();
  152. }
  153. // /// /// /// /// /// //
  154.  
  155. public void DeleteUser(int accountID)
  156. {
  157.  
  158.  
  159. string feedbackDelete = "delete from Feedback where FK_Feedback_UserID=@accountID;";
  160. Connection.Open();
  161. SqlCommand cmd = new SqlCommand(feedbackDelete, Connection);
  162. cmd.Parameters.AddWithValue("@accountID", accountID);
  163. SqlDataReader dr = cmd.ExecuteReader();
  164. dr.Close();
  165. Connection.Close();
  166.  
  167. string userWorkoutDelete = "delete from UserWorkout where FK_UserWorkout_UserID=@accountID;";
  168. Connection.Open();
  169. SqlCommand cmd2 = new SqlCommand(userWorkoutDelete, Connection);
  170. cmd2.Parameters.AddWithValue("@accountID", accountID);
  171. dr = cmd2.ExecuteReader();
  172. dr.Close();
  173. Connection.Close();
  174.  
  175. string accountDelete = "delete from Account where AccountID=@accountID;";
  176. Connection.Open();
  177. SqlCommand cmd3 = new SqlCommand(accountDelete, Connection);
  178. cmd3.Parameters.AddWithValue("@accountID", accountID);
  179. dr = cmd3.ExecuteReader();
  180. dr.Close();
  181. Connection.Close();
  182.  
  183. string challengeDelete = "delete from UserChallenge where FK_UserChallenge_UserID=@accountID;";
  184. Connection.Open();
  185. SqlCommand cmd4 = new SqlCommand(challengeDelete, Connection);
  186. cmd4.Parameters.AddWithValue("@accountID", accountID);
  187. dr = cmd4.ExecuteReader();
  188. dr.Close();
  189. Connection.Close();
  190.  
  191. string planDelete = "delete from UserPlanDay where FK_UserPlanDay_UserID=@accountID;";
  192. Connection.Open();
  193. SqlCommand cmd5 = new SqlCommand(planDelete, Connection);
  194. cmd5.Parameters.AddWithValue("@accountID", accountID);
  195. dr = cmd5.ExecuteReader();
  196. dr.Close();
  197. Connection.Close();
  198.  
  199. string weightDelete = "delete from UserWeight where FK_UserWeight_UserID=@accountID;";
  200. Connection.Open();
  201. SqlCommand cmd6 = new SqlCommand(weightDelete, Connection);
  202. cmd6.Parameters.AddWithValue("@accountID", accountID);
  203. dr = cmd6.ExecuteReader();
  204. dr.Close();
  205. Connection.Close();
  206.  
  207. string foodDelete = "delete from UserFood where FK_UserFood_UserID=@accountID;";
  208. Connection.Open();
  209. SqlCommand cmd7 = new SqlCommand(foodDelete, Connection);
  210. cmd7.Parameters.AddWithValue("@accountID", accountID);
  211. dr = cmd7.ExecuteReader();
  212. dr.Close();
  213. Connection.Close();
  214.  
  215. string userDelete = "delete from [User] where PK_UserID=@accountID;";
  216. Connection.Open();
  217. SqlCommand cmd8 = new SqlCommand(userDelete, Connection);
  218. cmd8.Parameters.AddWithValue("@accountID", accountID);
  219. dr = cmd8.ExecuteReader();
  220. dr.Close();
  221. Connection.Close();
  222.  
  223. }
  224. // /// /// /// /// /// //
  225.  
  226. public List<int> Rating()
  227. {
  228. Connection.Open();
  229. var rateList = new List<int>();
  230. for (int i = 1; i <= 5; i++)
  231. {
  232. int rate = 0;
  233. string query = "select count(FK_Feedback_UserID) from Feedback where Rating = @i;";
  234.  
  235. SqlCommand cmd = new SqlCommand(query, Connection);
  236. rate = (int)cmd.ExecuteScalar();
  237.  
  238. rateList.Add(rate);
  239.  
  240. }
  241. Connection.Close();
  242. return rateList;
  243. }
  244.  
  245.  
  246. // don't forget to put the image to bute array function on clicking the button before accessing this function
  247. public void AddNewChallenge(byte[] image, string name, string description, int targetMinutes, string reward, string dueDate, int workoutID)
  248. {
  249. Connection.Open();
  250.  
  251. SqlCommand cmd = new SqlCommand("AddChallenge", Connection);
  252. cmd.CommandType = CommandType.StoredProcedure;
  253. cmd.Parameters.AddWithValue("@Image", image);
  254. cmd.Parameters.AddWithValue("@Name", name);
  255. cmd.Parameters.AddWithValue("@Description", description);
  256. cmd.Parameters.AddWithValue("@TargetMinutes", targetMinutes);
  257. cmd.Parameters.AddWithValue("@Rewards", reward);
  258. cmd.Parameters.AddWithValue("@DueDate", dueDate);
  259. cmd.Parameters.AddWithValue("@Fk_Challenge_WorkoutID", workoutID);
  260. cmd.ExecuteNonQuery();
  261.  
  262. Connection.Close();
  263.  
  264. }
  265.  
  266.  
  267.  
  268.  
  269. // UNFINISHED//
  270. public List<UserModel> LoadAUser(string search)
  271. {
  272. UserModel currentUser = new UserModel();
  273.  
  274. string firstName = "";
  275. string lastName = "";
  276. string email = "";
  277. string searching = search + '%';
  278.  
  279.  
  280. string query = "select photo , FirstName , LastName , Email from [User] inner join Account on PK_UserID = AccountID where FirstName like @search;";
  281. SqlCommand cmd = new SqlCommand(query, Connection);
  282. cmd.Parameters.AddWithValue(" @search", searching);
  283. SqlDataReader dr = cmd.ExecuteReader();
  284. while (dr.Read())
  285. {
  286. if (dr.HasRows == true)
  287. {
  288. if (dr["Photo"] != DBNull.Value)
  289. currentUser.ProfilePhoto.ByteArray = (byte[])dr["Photo"];
  290.  
  291. firstName = dr["FirstName"].ToString();
  292. lastName = dr["LastName"].ToString();
  293. email = dr["Email"].ToString();
  294.  
  295. }
  296.  
  297. }
  298.  
  299.  
  300. }
Add Comment
Please, Sign In to add comment