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.

RegisterRepository.cs 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using Microsoft.AspNetCore.Authorization;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnivateProperties_API.Containers.Users;
  5. using UnivateProperties_API.Containers.Users.Simple;
  6. using UnivateProperties_API.Context;
  7. using UnivateProperties_API.Helpers;
  8. using UnivateProperties_API.Model.Users;
  9. namespace UnivateProperties_API.Repository.Users
  10. {
  11. public class RegisterRepository : IRegisterRepository
  12. {
  13. private readonly DataContext _dbContext;
  14. public RegisterRepository(DataContext dbContext)
  15. {
  16. _dbContext = dbContext;
  17. }
  18. public User Authenticate(string username, string password)
  19. {
  20. if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
  21. return null;
  22. var user = _dbContext.Users.SingleOrDefault(x => x.Username == username);
  23. // check if username exists
  24. if (user == null)
  25. throw new AppException("Username is incorrect");
  26. // check if password is correct
  27. if (!MyCommon.VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
  28. throw new AppException("Password is incorrect");
  29. // authentication successful
  30. return user;
  31. }
  32. public User Create(User user, string password, bool save)
  33. {
  34. // validation
  35. if (string.IsNullOrWhiteSpace(password))
  36. throw new AppException("Password is required");
  37. if (_dbContext.Users.Any(x => x.Username == user.Username))
  38. throw new AppException("Username \"" + user.Username + "\" is already taken");
  39. MyCommon.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
  40. user.PasswordHash = passwordHash;
  41. user.PasswordSalt = passwordSalt;
  42. user.Id = NewUserId();
  43. _dbContext.Users.Add(user);
  44. if (save)
  45. {
  46. _dbContext.SaveChanges();
  47. }
  48. return user;
  49. }
  50. public Agency CreateAgency(AgencyDto agency)
  51. {
  52. // validation
  53. if (string.IsNullOrWhiteSpace(agency.EaabeffcNumber))
  54. throw new AppException("eaabeffcNumber is required");
  55. if (_dbContext.Agencies.Any(x => x.EAABEFFCNumber == agency.EaabeffcNumber))
  56. throw new AppException("eaabeffcNumber \"" + agency.EaabeffcNumber + "\" already exists");
  57. Agency a = new Agency()
  58. {
  59. AgencyName = agency.Name,
  60. EAABEFFCNumber = agency.EaabeffcNumber,
  61. CompanyRegNumber = agency.RegNo
  62. };
  63. a.Id = NewAgencyId();
  64. _dbContext.Agencies.Add(a);
  65. CreatePerson(agency.User, PersonType.Agent, false, a);
  66. _dbContext.SaveChanges();
  67. return a;
  68. }
  69. public void CreatePerson(UserDto individual, PersonType personType, bool save, Agency agency)
  70. {
  71. // validation
  72. if (string.IsNullOrWhiteSpace(individual.Password))
  73. throw new AppException("Password is required");
  74. if (_dbContext.Users.Any(x => x.Username == individual.Username))
  75. throw new AppException("Individual \"" + individual.Username + "\" is already taken");
  76. MyCommon.CreatePasswordHash(individual.Password, out byte[] passwordHash, out byte[] passwordSalt);
  77. User createUser = new User(individual.Username, individual.Password);
  78. Create(createUser, individual.Password, false);
  79. if (personType == PersonType.Agent)
  80. {
  81. Agent agent = new Agent()
  82. {
  83. Name = individual.Name,
  84. Surname = individual.Surname,
  85. User = createUser,
  86. Email = individual.Email,
  87. CellNumber = individual.CellNumber,
  88. Telephone = individual.Telephone,
  89. Agency = agency
  90. };
  91. agent.Id = NewAgentId();
  92. _dbContext.Agents.Add(agent);
  93. }
  94. else if (personType == PersonType.Individual)
  95. {
  96. Individual i = new Individual()
  97. {
  98. Name = individual.Name,
  99. Surname = individual.Surname,
  100. User = createUser,
  101. Email = individual.Email,
  102. CellNumber = individual.CellNumber,
  103. Telephone = individual.Telephone
  104. };
  105. i.Id = NewIndividualId();
  106. _dbContext.Individuals.Add(i);
  107. }
  108. if (save)
  109. {
  110. Save();
  111. }
  112. }
  113. public void Update(User userParam, string password = null)
  114. {
  115. var user = _dbContext.Users.Find(userParam.Id);
  116. if (user == null)
  117. throw new AppException("User not found");
  118. if (userParam.Username != user.Username)
  119. {
  120. // username has changed so check if the new username is already taken
  121. if (_dbContext.Users.Any(x => x.Username == userParam.Username))
  122. throw new AppException("Username " + userParam.Username + " is already taken");
  123. }
  124. // update user properties
  125. user.Username = userParam.Username;
  126. // update password if it was entered
  127. if (!string.IsNullOrWhiteSpace(password))
  128. {
  129. MyCommon.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
  130. user.PasswordHash = passwordHash;
  131. user.PasswordSalt = passwordSalt;
  132. }
  133. _dbContext.Users.Update(user);
  134. _dbContext.SaveChanges();
  135. }
  136. [Authorize(Roles = Role.SuperAdmin)]
  137. public IEnumerable<User> GetAllUsers()
  138. {
  139. return _dbContext.Users;
  140. }
  141. [Authorize(Roles = Role.SuperAdmin)]
  142. public IEnumerable<Agency> GetAllAgencies()
  143. {
  144. return _dbContext.Agencies;
  145. }
  146. [Authorize(Roles = Role.SuperAdmin)]
  147. public IEnumerable<Individual> GetAllIndividuals()
  148. {
  149. return _dbContext.Individuals;
  150. }
  151. public User GetById(int id)
  152. {
  153. return _dbContext.Users.Find(id);
  154. }
  155. public Agency GetByAgencyId(int id)
  156. {
  157. return _dbContext.Agencies.Find(id);
  158. }
  159. public Individual GetByIndividualId(int id)
  160. {
  161. return _dbContext.Individuals.Find(id);
  162. }
  163. public void Delete(int id)
  164. {
  165. var user = _dbContext.Users.Find(id);
  166. if (user != null)
  167. {
  168. _dbContext.Users.Remove(user);
  169. _dbContext.SaveChanges();
  170. }
  171. }
  172. public void DeleteAgency(int id)
  173. {
  174. var agency = _dbContext.Agencies.Find(id);
  175. if (agency != null)
  176. {
  177. _dbContext.Remove(agency);
  178. _dbContext.SaveChanges();
  179. }
  180. }
  181. public void DeleteIndividual(int id)
  182. {
  183. var individual = _dbContext.Individuals.Find(id);
  184. if (individual != null)
  185. {
  186. _dbContext.Individuals.Remove(individual);
  187. _dbContext.SaveChanges();
  188. }
  189. }
  190. private void Save()
  191. {
  192. _dbContext.SaveChanges();
  193. }
  194. public int NewAgencyId()
  195. {
  196. int id = 0;
  197. if (_dbContext.Agencies.Count() > 0)
  198. {
  199. id = _dbContext.Agencies.Max(x => x.Id);
  200. }
  201. id += 1;
  202. return id;
  203. }
  204. public int NewAgentId()
  205. {
  206. int id = 0;
  207. if (_dbContext.Agents.Count() > 0)
  208. {
  209. id = _dbContext.Agents.Max(x => x.Id);
  210. }
  211. id += 1;
  212. return id;
  213. }
  214. public int NewIndividualId()
  215. {
  216. int id = 0;
  217. if (_dbContext.Individuals.Count() > 0)
  218. {
  219. id = _dbContext.Individuals.Max(x => x.Id);
  220. }
  221. id += 1;
  222. return id;
  223. }
  224. public int NewUserId()
  225. {
  226. int id = 0;
  227. if (_dbContext.Users.Count() > 0)
  228. {
  229. id = _dbContext.Users.Max(x => x.Id);
  230. }
  231. id += 1;
  232. return id;
  233. }
  234. public SimplePersonDto UserDetails(int userId)
  235. {
  236. var individual = _dbContext.Individuals.Where(i => i.UserId == userId).FirstOrDefault();
  237. if (individual == null)
  238. {
  239. var agent = _dbContext.Agents.Where(i => i.UserId == userId).FirstOrDefault();
  240. if (agent != null)
  241. {
  242. return new SimplePersonDto()
  243. {
  244. Name = agent.Name,
  245. Surname = agent.Surname,
  246. Email = agent.Email
  247. };
  248. }
  249. else
  250. {
  251. return new SimplePersonDto(); ;
  252. }
  253. }
  254. else
  255. {
  256. return new SimplePersonDto()
  257. {
  258. Name = individual.Name,
  259. Surname = individual.Surname,
  260. Email = individual.Email
  261. };
  262. }
  263. }
  264. }
  265. }