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.

AgentRepository.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Context;
  6. using UnivateProperties_API.Model.Users;
  7. namespace UnivateProperties_API.Repository.Users
  8. {
  9. public class AgentRepository : IRepository<Agent>
  10. {
  11. private readonly DataContext _dbContext;
  12. public AgentRepository(DataContext dbContext)
  13. {
  14. _dbContext = dbContext;
  15. }
  16. public List<Agent> Get(Func<Agent, bool> where)
  17. {
  18. return _dbContext.Agents.Where(where).ToList();
  19. }
  20. public List<Agent> GetAll()
  21. {
  22. return _dbContext.Agents.Include("User").Where(x => x.IsDeleted == false).ToList();
  23. }
  24. public Agent GetDetailed(Func<Agent, bool> first)
  25. {
  26. var item = _dbContext.Agents.FirstOrDefault(first);
  27. //AgentRepository account = new AgentRepository(_dbContext);
  28. //item = GetDetailedObject(item, account);
  29. return item;
  30. }
  31. public void Insert(Agent item)
  32. {
  33. //item.Id = NewId();
  34. //item.User.Role = Role.Agent;
  35. /*if (_dbContext.Agents.Any(a => a.AgencyId == null))
  36. {
  37. item.AgencyId = 10;
  38. }*/
  39. var agent = _dbContext.Agents.Where(x => x.UserId == item.User.Id).FirstOrDefault();
  40. var individual = _dbContext.Individuals.Where(x => x.UserId == item.User.Id).FirstOrDefault();
  41. var user = _dbContext.Users.Where(x => x.Id == item.User.Id).FirstOrDefault();
  42. if (item.User.Role == "Agent" || item.User.Role == "Agency" || item.User.Role == "Managing Agent")
  43. {
  44. if (agent == null)
  45. {
  46. if (individual != null)
  47. {
  48. individual.IsDeleted = true;
  49. _dbContext.Individuals.Update(individual);
  50. }
  51. item.Id = 0;
  52. _dbContext.Agents.Add(item);
  53. user.Role = item.User.Role;
  54. _dbContext.Users.Update(user);
  55. }
  56. else
  57. {
  58. if (agent.Name != item.Name)
  59. {
  60. agent.Name = item.Name;
  61. }
  62. if (agent.Surname != item.Surname)
  63. {
  64. agent.Surname = item.Surname;
  65. }
  66. if (agent.Email != item.Email)
  67. {
  68. agent.Email = item.Email;
  69. }
  70. if (agent.Telephone != item.Telephone)
  71. {
  72. agent.Telephone = item.Telephone;
  73. }
  74. if (agent.CellNumber != item.CellNumber)
  75. {
  76. agent.CellNumber = item.CellNumber;
  77. }
  78. if (agent.AgencyId != item.AgencyId)
  79. {
  80. agent.AgencyId = item.AgencyId;
  81. }
  82. agent.IsDeleted = false;
  83. if (individual != null)
  84. {
  85. individual.IsDeleted = true;
  86. _dbContext.Individuals.Update(individual);
  87. }
  88. _dbContext.Agents.Update(agent);
  89. if (item.User.Role != user.Role)
  90. {
  91. user.Role = item.User.Role;
  92. }
  93. _dbContext.Users.Update(user);
  94. }
  95. }
  96. else
  97. {
  98. agent.IsDeleted = true;
  99. individual.IsDeleted = false;
  100. user.Role = item.User.Role;
  101. if (individual.Name != item.Name)
  102. {
  103. individual.Name = item.Name;
  104. }
  105. if (individual.Surname != item.Surname)
  106. {
  107. individual.Surname = item.Surname;
  108. }
  109. if (individual.Email != item.Email)
  110. {
  111. individual.Email = item.Email;
  112. }
  113. if (individual.Telephone != item.Telephone)
  114. {
  115. individual.Telephone = item.Telephone;
  116. }
  117. if (individual.CellNumber != item.CellNumber)
  118. {
  119. individual.CellNumber = item.CellNumber;
  120. }
  121. _dbContext.Agents.Update(agent);
  122. _dbContext.Users.Update(user);
  123. _dbContext.Individuals.Update(individual);
  124. }
  125. Save();
  126. }
  127. public void Insert(IEnumerable<Agent> item)
  128. {
  129. int id = NewId();
  130. foreach (var i in item)
  131. {
  132. i.Id = id;
  133. _dbContext.Add(i);
  134. id += 1;
  135. }
  136. Save();
  137. }
  138. public void Remove(Agent item)
  139. {
  140. var i = _dbContext.Agents.Find(item);
  141. _dbContext.Agents.Remove(i);
  142. Save();
  143. }
  144. public void Remove(IEnumerable<Agent> items)
  145. {
  146. foreach (var item in items)
  147. {
  148. Agent i = _dbContext.Agents.Find(item);
  149. _dbContext.Agents.Remove(i);
  150. }
  151. Save();
  152. }
  153. public void RemoveAtId(int item)
  154. {
  155. var i = _dbContext.Agents.Find(item);
  156. _dbContext.Agents.Remove(i);
  157. Save();
  158. }
  159. public void Update(Agent item)
  160. {
  161. //var savedAgent = _dbContext.Agents.Where(x => x.Id == item.Id).FirstOrDefault();
  162. _dbContext.Entry(item).State = EntityState.Modified;
  163. _dbContext.Entry(item.User).State = EntityState.Modified;
  164. Save();
  165. }
  166. public void Save()
  167. {
  168. _dbContext.SaveChanges();
  169. }
  170. public List<Agent> GetDetailedAll()
  171. {
  172. // TODO: GetDetailed
  173. throw new NotImplementedException();
  174. }
  175. public int NewId()
  176. {
  177. int id = 0;
  178. if (_dbContext.Agents.Count() > 0)
  179. {
  180. id = _dbContext.Agents.Max(x => x.Id);
  181. }
  182. id += 1;
  183. return id;
  184. }
  185. }
  186. }