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.

WeekRepository.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Containers.Timeshare;
  6. using UnivateProperties_API.Context;
  7. using UnivateProperties_API.Model.Timeshare;
  8. using UnivateProperties_API.Repository.Region;
  9. using UnivateProperties_API.Repository.Users;
  10. namespace UnivateProperties_API.Repository.Timeshare
  11. {
  12. public class WeekRepository : IRepository<TimeshareWeek>
  13. {
  14. private readonly DataContext _dbContext;
  15. public WeekRepository(DataContext dbContext)
  16. {
  17. _dbContext = dbContext;
  18. }
  19. public List<TimeshareWeek> Get(Func<TimeshareWeek, bool> where)
  20. {
  21. return _dbContext.Weeks.Where(where).ToList();
  22. }
  23. public List<TimeshareWeek> GetAll()
  24. {
  25. return _dbContext.Weeks.ToList();
  26. }
  27. public TimeshareWeek GetDetailed(Func<TimeshareWeek, bool> first)
  28. {
  29. var item = _dbContext.Weeks.FirstOrDefault(first);
  30. item = GetDetailedWeek(item);
  31. return item;
  32. }
  33. public List<WeekDto> GetDtoListAll()
  34. {
  35. List<WeekDto> list = new List<WeekDto>();
  36. foreach(var item in GetDetailedAll())
  37. {
  38. list.Add(new WeekDto(item));
  39. }
  40. return list;
  41. }
  42. public List<RegionDto> GetAvailResort()
  43. {
  44. List<RegionDto> list = new List<RegionDto>();
  45. foreach (var item in GetDetailedAll())
  46. {
  47. if (item.Region != null)
  48. {
  49. if (!list.Any(x => x.RegionCode == item.Region.Code))
  50. {
  51. list.Add(new RegionDto(item.Region.Code, item.Region.Description));
  52. }
  53. foreach (var i in list.Where(x => x.RegionCode == item.Region.Code))
  54. {
  55. i.TryAddResort(item.ResortCode, item.ResortName);
  56. }
  57. }
  58. }
  59. return list;
  60. }
  61. public List<TimeshareWeek> GetDetailedAll()
  62. {
  63. var list = GetAll();
  64. List<TimeshareWeek> weeklist = new List<TimeshareWeek>();
  65. foreach(var item in list)
  66. {
  67. weeklist.Add(GetDetailedWeek(item));
  68. }
  69. return weeklist;
  70. }
  71. private TimeshareWeek GetDetailedWeek(TimeshareWeek week)
  72. {
  73. if(week.AgencyId != null)
  74. {
  75. AgencyRepository agency = new AgencyRepository(_dbContext);
  76. week.Agency = agency.Get(x => x.Id == week.AgencyId).FirstOrDefault();
  77. }
  78. if(week.AgentId != null)
  79. {
  80. AgentRepository agent = new AgentRepository(_dbContext);
  81. week.Agent = agent.Get(x => x.Id == week.AgentId).FirstOrDefault();
  82. }
  83. if (week.StatusId != 0)
  84. {
  85. StatusRepository status = new StatusRepository(_dbContext);
  86. week.Status = status.Get(x => x.Id == week.StatusId).FirstOrDefault();
  87. }
  88. else
  89. {
  90. StatusRepository repo = new StatusRepository(_dbContext);
  91. week.Status = repo.GetDetailed(s => s.Code == "A1");
  92. }
  93. if (week.RegionId != 0)
  94. {
  95. ProvinceRepository province = new ProvinceRepository(_dbContext);
  96. week.Region = province.Get(x => x.Id == week.RegionId).FirstOrDefault();
  97. }
  98. if (week.OwnerId != 0)
  99. {
  100. IndividualRepository individual = new IndividualRepository(_dbContext);
  101. week.Owner = individual.Get(x => x.Id == week.OwnerId).FirstOrDefault();
  102. }
  103. return week;
  104. }
  105. public void Insert(TimeshareWeek item)
  106. {
  107. item = GetDetailedWeek(item);
  108. item.Id = _dbContext.Weeks.Max(x => x.Id) + 1;
  109. if (item.Owner != null && item.Owner.Id == 0)
  110. {
  111. item.Owner.Id = _dbContext.Individuals.Max(x => x.Id) + 1;
  112. }
  113. // Set starting Status of A1
  114. StatusRepository repo = new StatusRepository(_dbContext);
  115. item.Status = repo.GetDetailed(s => s.Code == "A1");
  116. if(item.Status != null)
  117. {
  118. //Create initial
  119. item.Status = new Status()
  120. {
  121. Code = "A1",
  122. Description = "Pending verification",
  123. StatusType = StatusType.Timeshare,
  124. ModifiedBy = "KobusB"
  125. };
  126. }
  127. item.Id = NewId();
  128. _dbContext.Add(item);
  129. Save();
  130. }
  131. public void Insert(IEnumerable<TimeshareWeek> items)
  132. {
  133. int id = NewId();
  134. foreach (var item in items)
  135. {
  136. item.Id = id;
  137. _dbContext.Add(item);
  138. id += 1;
  139. }
  140. Save();
  141. }
  142. public void Remove(TimeshareWeek item)
  143. {
  144. var i = _dbContext.Weeks.Find(item);
  145. _dbContext.Weeks.Remove(i);
  146. Save();
  147. }
  148. public void Remove(IEnumerable<TimeshareWeek> items)
  149. {
  150. foreach (var item in items)
  151. {
  152. var i = _dbContext.Weeks.Find(item);
  153. _dbContext.Weeks.Remove(i);
  154. }
  155. Save();
  156. }
  157. public void RemoveAtId(int item)
  158. {
  159. var i = _dbContext.Weeks.Find(item);
  160. _dbContext.Weeks.Remove(i);
  161. Save();
  162. }
  163. public void Save()
  164. {
  165. _dbContext.SaveChanges();
  166. }
  167. public void Update(TimeshareWeek item)
  168. {
  169. _dbContext.Entry(item).State = EntityState.Modified;
  170. Save();
  171. }
  172. public List<TimeshareWeek> GetBy(WeekFilterDto week)
  173. {
  174. List<TimeshareWeek> item = GetDetailedAll();
  175. if (!string.IsNullOrEmpty(week.RegionCode))
  176. {
  177. item = item.Where(x => x.Region != null && x.Region.Code?.ToLower() == week.RegionCode.ToLower()).ToList();
  178. }
  179. if (!string.IsNullOrEmpty(week.ResortCode))
  180. {
  181. item = item.Where(x => x.ResortCode?.ToLower() == week.ResortCode).ToList();
  182. }
  183. if (week.Date != null)
  184. {
  185. item = item.Where(x =>
  186. x.ArrivalDate >= week.Date.Value.AddDays(-7)
  187. && x.DepartureDate <= week.Date.Value.AddDays(7)).ToList();
  188. }
  189. if (week.MinAmount != null && week.MinAmount != 0)
  190. {
  191. item = item.Where(x => x.SellPrice >= week.MinAmount).ToList();
  192. }
  193. if (week.MaxAmount != null && week.MaxAmount != 0)
  194. {
  195. item.Where(x => x.SellPrice <= week.MaxAmount).ToList();
  196. }
  197. return item;
  198. }
  199. public int NewId()
  200. {
  201. int id = 0;
  202. if (_dbContext.Weeks.Count() > 0)
  203. {
  204. id = _dbContext.Weeks.Max(x => x.Id);
  205. }
  206. id += 1;
  207. return id;
  208. }
  209. }
  210. }