API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserRepository.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnivateProperties_API.Containers.Users;
  5. using UnivateProperties_API.Context;
  6. using UnivateProperties_API.Helpers;
  7. using UnivateProperties_API.Model.Users;
  8. namespace UnivateProperties_API.Repository.Users
  9. {
  10. public class UserRepository : IUserRepository
  11. {
  12. private readonly DataContext _dbContext;
  13. public UserRepository(DataContext dbContext)
  14. {
  15. _dbContext = dbContext;
  16. }
  17. public List<User> Get(Func<User, bool> where)
  18. {
  19. return _dbContext.Users.Where(where).ToList();
  20. }
  21. public List<User> GetAll()
  22. {
  23. return _dbContext.Users.ToList();
  24. }
  25. public User GetDetailed(Func<User, bool> first)
  26. {
  27. var item = _dbContext.Users.FirstOrDefault(first);
  28. UserRepository account = new UserRepository(_dbContext);
  29. item = GetDetailedObject(item, account);
  30. return item;
  31. }
  32. private User GetDetailedObject(User item, UserRepository repo)
  33. {
  34. item = repo.GetDetailed(x => x.Id == item.Id);
  35. return item;
  36. }
  37. public void Insert(User item)
  38. {
  39. //item.Id = NewId();
  40. _dbContext.Add(item);
  41. Save();
  42. }
  43. public void Insert(IEnumerable<User> item)
  44. {
  45. //int id = NewId();
  46. //foreach (var i in item)
  47. //{
  48. // i.Id = id;
  49. _dbContext.Add(item);
  50. // id += 1;
  51. //}
  52. Save();
  53. }
  54. public void Remove(User item)
  55. {
  56. var i = _dbContext.Users.Find(item);
  57. _dbContext.Users.Remove(i);
  58. Save();
  59. }
  60. public void Remove(IEnumerable<User> items)
  61. {
  62. foreach (var item in items)
  63. {
  64. User i = _dbContext.Users.Find(item);
  65. _dbContext.Users.Remove(i);
  66. }
  67. Save();
  68. }
  69. public void RemoveAtId(int item)
  70. {
  71. var i = _dbContext.Users.Find(item);
  72. _dbContext.Users.Remove(i);
  73. Save();
  74. }
  75. public void Update(UserDto userParam)
  76. {
  77. var user = _dbContext.Users.Find(userParam.Id);
  78. if (user == null)
  79. throw new AppException("User not found");
  80. if (userParam.Username != user.Username)
  81. {
  82. // username has changed so check if the new username is already taken
  83. if (_dbContext.Users.Any(x => x.Username == userParam.Username))
  84. throw new AppException("Username " + userParam.Username + " is already taken");
  85. }
  86. // update user properties
  87. user.Username = userParam.Username;
  88. // update password if it was entered
  89. if (!string.IsNullOrWhiteSpace(userParam.Password))
  90. {
  91. MyCommon.CreatePasswordHash(userParam.Password, out byte[] passwordHash, out byte[] passwordSalt);
  92. user.PasswordHash = passwordHash;
  93. user.PasswordSalt = passwordSalt;
  94. }
  95. if (userParam.LoginPasswordChange)
  96. user.LoginPasswordChange = false;
  97. _dbContext.Users.Update(user);
  98. _dbContext.SaveChanges();
  99. }
  100. private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
  101. {
  102. if (password == null) throw new ArgumentNullException("password");
  103. if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
  104. using (var hmac = new System.Security.Cryptography.HMACSHA512())
  105. {
  106. passwordSalt = hmac.Key;
  107. passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
  108. }
  109. }
  110. public void Save()
  111. {
  112. _dbContext.SaveChanges();
  113. }
  114. public List<User> GetDetailedAll()
  115. {
  116. throw new NotImplementedException();
  117. }
  118. //public int NewId()
  119. //{
  120. // int id = 0;
  121. // if (_dbContext.Users.Count() > 0)
  122. // {
  123. // id = _dbContext.Users.Max(x => x.Id);
  124. // }
  125. // id += 1;
  126. // return id;
  127. //}
  128. }
  129. }