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

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