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.3KB

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