Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. using MeetUp.Data;
  2. using MeetUp.Data.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.Entity.Validation;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace MeetUp.Services
  12. {
  13. public class UserService
  14. {
  15. //private readonly MeetUpDbContext db;
  16.  
  17. public UserService()
  18. {
  19. //this.db = new MeetUpDbContext();
  20. }
  21.  
  22. public bool Create(string email, string password, string fullname, string description="")
  23. {
  24. using (var db = new MeetUpDbContext())
  25. {
  26. var mailExists = db.Users.Any(u => u.Email == email);
  27.  
  28. if(mailExists)
  29. {
  30. return false;
  31. }
  32.  
  33. var user = new User
  34. {
  35. Email = email,
  36. Password = this.HashPassword(password, email),
  37. FullName = fullname,
  38. Description = description,
  39. Sex = 1,
  40. CityId = 1,
  41. };
  42.  
  43. db.Users.Add(user);
  44.  
  45. try
  46. {
  47. db.SaveChanges();
  48. } catch(DbEntityValidationException ex)
  49. {
  50. foreach (var entityValidationErrors in ex.EntityValidationErrors)
  51. {
  52. foreach (var validationError in entityValidationErrors.ValidationErrors)
  53. {
  54. System.Diagnostics.Debug.WriteLine("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
  55. }
  56. }
  57. }
  58.  
  59.  
  60. return true;
  61. }
  62. }
  63.  
  64. public bool Login(string email, string password)
  65. {
  66. using (var db = new MeetUpDbContext())
  67. {
  68. var hashedPass = this.HashPassword(password, email);
  69.  
  70. var user = db.Users.Where(u => u.Email == email && u.Password == hashedPass);
  71.  
  72. if (user == null)
  73. {
  74. return false;
  75. }
  76.  
  77. return true;
  78. }
  79. }
  80.  
  81. public List<User> All()
  82. {
  83. using (var db = new MeetUpDbContext())
  84. {
  85. return db.Users.ToList();
  86. }
  87. }
  88.  
  89. public string HashPassword(string password, string salt)
  90. {
  91. using (var sha1 = new SHA1Managed())
  92. {
  93. return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(password + salt)));
  94. }
  95. }
  96.  
  97. public void DeleteUser(int id)
  98. {
  99. MeetUpDbContext ctx = new MeetUpDbContext();
  100. var user = new User { Id = id };
  101. ctx.Users.Attach(user);
  102. ctx.Users.Remove(user);
  103. ctx.SaveChanges();
  104.  
  105. }
  106.  
  107. public void EditUser()
  108. {
  109. MeetUpDbContext ctx = new MeetUpDbContext();
  110. User user = ctx.Users.Create();
  111. user.Id = 4;
  112. ctx.Users.Attach(user);
  113.  
  114. user.FullName = "darko";
  115.  
  116. ctx.SaveChanges();
  117. }
  118.  
  119. public List<User> getUsers()
  120. {
  121. List<User> allUsers = new List<User>();
  122.  
  123. using (var context = new MeetUpDbContext())
  124. {
  125. var users = from u in context.Users select u;
  126.  
  127. foreach (User u in users)
  128. {
  129. allUsers.Add(u);
  130. }
  131.  
  132. }
  133.  
  134. return allUsers;
  135. }
  136.  
  137. public User GetUserById(int id)
  138. {
  139. User user = new User();
  140.  
  141. using (var context = new MeetUpDbContext())
  142. {
  143. user = (from u in context.Users where u.Id == id select u).First();
  144.  
  145. }
  146.  
  147. return user;
  148. }
  149.  
  150. public void updateUser(int id, string fullName, string email, string description, string password)
  151. {
  152. using (var context = new MeetUpDbContext())
  153. {
  154. var user = context.Users.SingleOrDefault(u => u.Id == id);
  155. if(user != null)
  156. {
  157. user.FullName = fullName;
  158. user.Description = description;
  159. user.Email = email;
  160. user.Password = this.HashPassword(password, email);
  161.  
  162. context.SaveChanges();
  163. }
  164.  
  165. }
  166. }
  167.  
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement