API
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AgentRepository.cs 4.4KB

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