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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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<TimeshareWeek> GetDetailedAll()
  43. {
  44. var list = GetAll();
  45. List<TimeshareWeek> weeklist = new List<TimeshareWeek>();
  46. foreach(var item in list)
  47. {
  48. weeklist.Add(GetDetailedWeek(item));
  49. }
  50. return weeklist;
  51. }
  52. private TimeshareWeek GetDetailedWeek(TimeshareWeek week)
  53. {
  54. if(week.AgencyId != null)
  55. {
  56. AgencyRepository agency = new AgencyRepository(_dbContext);
  57. week.Agency = agency.Get(x => x.Id == week.AgencyId).FirstOrDefault();
  58. }
  59. if(week.AgentId != null)
  60. {
  61. AgentRepository agent = new AgentRepository(_dbContext);
  62. week.Agent = agent.Get(x => x.Id == week.AgentId).FirstOrDefault();
  63. }
  64. if (week.StatusId != 0)
  65. {
  66. StatusRepository status = new StatusRepository(_dbContext);
  67. week.Status = status.Get(x => x.Id == week.StatusId).FirstOrDefault();
  68. }
  69. if (week.RegionId != 0)
  70. {
  71. ProvinceRepository province = new ProvinceRepository(_dbContext);
  72. week.Region = province.Get(x => x.Id == week.RegionId).FirstOrDefault();
  73. }
  74. if (week.OwnerId != 0)
  75. {
  76. IndividualRepository individual = new IndividualRepository(_dbContext);
  77. week.Owner = individual.Get(x => x.Id == week.OwnerId).FirstOrDefault();
  78. }
  79. return week;
  80. }
  81. public void Insert(TimeshareWeek item)
  82. {
  83. item = GetDetailedWeek(item);
  84. item.Id = _dbContext.Weeks.Max(x => x.Id) + 1;
  85. if (item.Owner != null && item.Owner.Id == 0)
  86. {
  87. item.Owner.Id = _dbContext.Individuals.Max(x => x.Id) + 1;
  88. }
  89. // Set starting Status of A1
  90. StatusRepository repo = new StatusRepository(_dbContext);
  91. item.Status = repo.GetDetailed(s => s.Code == "A1");
  92. if(item.Status != null)
  93. {
  94. //Create initial
  95. item.Status = new Status()
  96. {
  97. Code = "A1",
  98. Description = "Pending verification",
  99. StatusType = StatusType.Timeshare,
  100. ModifiedBy = "KobusB"
  101. };
  102. }
  103. _dbContext.Add(item);
  104. Save();
  105. }
  106. public void Insert(IEnumerable<TimeshareWeek> items)
  107. {
  108. foreach (var item in items)
  109. {
  110. _dbContext.Add(item);
  111. }
  112. Save();
  113. }
  114. public void Remove(TimeshareWeek item)
  115. {
  116. var i = _dbContext.Weeks.Find(item);
  117. _dbContext.Weeks.Remove(i);
  118. Save();
  119. }
  120. public void Remove(IEnumerable<TimeshareWeek> items)
  121. {
  122. foreach (var item in items)
  123. {
  124. var i = _dbContext.Weeks.Find(item);
  125. _dbContext.Weeks.Remove(i);
  126. }
  127. Save();
  128. }
  129. public void RemoveAtId(int item)
  130. {
  131. var i = _dbContext.Weeks.Find(item);
  132. _dbContext.Weeks.Remove(i);
  133. Save();
  134. }
  135. public void Save()
  136. {
  137. _dbContext.SaveChanges();
  138. }
  139. public void Update(TimeshareWeek item)
  140. {
  141. _dbContext.Entry(item).State = EntityState.Modified;
  142. Save();
  143. }
  144. public List<TimeshareWeek> GetBy(WeekFilterDto week)
  145. {
  146. List<TimeshareWeek> item = GetDetailedAll();
  147. if (!string.IsNullOrEmpty(week.RegionCode))
  148. {
  149. item = item.Where(x => x.Region != null && x.Region.Code?.ToLower() == week.RegionCode.ToLower()).ToList();
  150. }
  151. if (!string.IsNullOrEmpty(week.ResortCode))
  152. {
  153. item = item.Where(x => x.ResortCode?.ToLower() == week.ResortCode).ToList();
  154. }
  155. if (week.Date != null)
  156. {
  157. item = item.Where(x =>
  158. x.ArrivalDate >= week.Date.Value.AddDays(-7)
  159. && x.DepartureDate <= week.Date.Value.AddDays(7)).ToList();
  160. }
  161. if (week.MinAmount != null && week.MinAmount != 0)
  162. {
  163. item = item.Where(x => x.SellPrice >= week.MinAmount).ToList();
  164. }
  165. if (week.MaxAmount != null && week.MaxAmount != 0)
  166. {
  167. item.Where(x => x.SellPrice <= week.MaxAmount).ToList();
  168. }
  169. return item;
  170. }
  171. }
  172. }