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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using UnivateProperties_API.Containers.Timeshare;
  10. using UnivateProperties_API.Containers.Users;
  11. using UnivateProperties_API.Context;
  12. using UnivateProperties_API.Helpers;
  13. using UnivateProperties_API.Model.Region;
  14. using UnivateProperties_API.Model.Timeshare;
  15. using UnivateProperties_API.Repository.Region;
  16. using UnivateProperties_API.Repository.Users;
  17. namespace UnivateProperties_API.Repository.Timeshare
  18. {
  19. public class WeekRepository : IRepository<TimeshareWeek>
  20. {
  21. private readonly DataContext _dbContext;
  22. public WeekRepository(DataContext dbContext)
  23. {
  24. _dbContext = dbContext;
  25. }
  26. public List<TimeshareWeek> Get(Func<TimeshareWeek, bool> where)
  27. {
  28. return _dbContext.Weeks.Where(where).ToList();
  29. }
  30. public List<TimeshareWeek> GetAll()
  31. {
  32. return _dbContext.Weeks.ToList();
  33. }
  34. public TimeshareWeek GetDetailed(Func<TimeshareWeek, bool> first)
  35. {
  36. var item = _dbContext.Weeks.FirstOrDefault(first);
  37. item = GetDetailedWeek(item);
  38. return item;
  39. }
  40. public List<WeekDto> GetDtoListAll()
  41. {
  42. List<WeekDto> list = new List<WeekDto>();
  43. foreach (var item in GetDetailedAll())
  44. {
  45. list.Add(new WeekDto(item));
  46. }
  47. foreach (var item in GetTenderWeeks())
  48. {
  49. list.Add(item);
  50. }
  51. return list;
  52. }
  53. public List<WeekDto> GetMyWeeks(int userId)
  54. {
  55. List<WeekDto> list = new List<WeekDto>();
  56. UserRepository userRepository = new UserRepository(_dbContext);
  57. var user = userRepository.Get(x => x.Id == userId).FirstOrDefault();
  58. if (user.IsUserInRole(Role.SuperAdmin))
  59. {
  60. foreach (var item in GetDetailedAll())
  61. {
  62. list.Add(new WeekDto(item));
  63. }
  64. }
  65. else if (user.IsUserInRole(Role.Agency))
  66. {
  67. foreach (var item in GetDetailedAll().Where(x => x.AgencyId == userId))
  68. {
  69. list.Add(new WeekDto(item));
  70. }
  71. }
  72. else if (user.IsUserInRole(Role.Agent) || user.IsUserInRole(Role.ManagingAgent))
  73. {
  74. foreach (var item in GetDetailedAll().Where(x => x.AgentId == userId))
  75. {
  76. list.Add(new WeekDto(item));
  77. }
  78. }
  79. else
  80. {
  81. foreach (var item in GetDetailedAll().Where(x => x.OwnerId == userId))
  82. {
  83. list.Add(new WeekDto(item));
  84. }
  85. }
  86. return list;
  87. }
  88. public List<RegionDto> GetAvailResort()
  89. {
  90. List<RegionDto> list = new List<RegionDto>();
  91. var allItems = GetDetailedAll();
  92. foreach (var item in allItems)
  93. {
  94. if (item.Region != null)
  95. {
  96. if (!list.Any(x => x.RegionCode == item.Region.Code))
  97. {
  98. list.Add(new RegionDto(item.Region.Code, item.Region.Description));
  99. }
  100. foreach (var i in list.Where(x => x.RegionCode == item.Region.Code))
  101. {
  102. i.TryAddResort(item.ResortCode, item.ResortName);
  103. }
  104. }
  105. }
  106. list = list.OrderBy(x => x.RegionName).ToList();
  107. foreach (var region in list)
  108. {
  109. region.OrderResorts();
  110. foreach (var resort in region.Resorts)
  111. {
  112. resort.Available = allItems.Count(x => x.ResortCode == resort.ResortCode);
  113. }
  114. }
  115. return list;
  116. }
  117. public List<TimeshareWeek> GetDetailedAll()
  118. {
  119. var list = GetAll();
  120. List<TimeshareWeek> weeklist = new List<TimeshareWeek>();
  121. foreach (var item in list)
  122. {
  123. weeklist.Add(GetDetailedWeek(item));
  124. }
  125. return weeklist;
  126. }
  127. private TimeshareWeek GetDetailedWeek(TimeshareWeek week)
  128. {
  129. if (week.AgencyId != null)
  130. {
  131. AgencyRepository agency = new AgencyRepository(_dbContext);
  132. week.Agency = agency.Get(x => x.Id == week.AgencyId).FirstOrDefault();
  133. }
  134. if (week.AgentId != null)
  135. {
  136. AgentRepository agent = new AgentRepository(_dbContext);
  137. week.Agent = agent.Get(x => x.Id == week.AgentId).FirstOrDefault();
  138. }
  139. if (week.StatusId != 0)
  140. {
  141. StatusRepository status = new StatusRepository(_dbContext);
  142. week.Status = status.Get(x => x.Id == week.StatusId).FirstOrDefault();
  143. }
  144. else
  145. {
  146. StatusRepository repo = new StatusRepository(_dbContext);
  147. week.Status = repo.GetDetailed(s => s.Code == "A1");
  148. }
  149. if (week.RegionId != 0)
  150. {
  151. ProvinceRepository province = new ProvinceRepository(_dbContext);
  152. week.Region = province.Get(x => x.Id == week.RegionId).FirstOrDefault();
  153. }
  154. if (week.OwnerId != 0)
  155. {
  156. IndividualRepository individual = new IndividualRepository(_dbContext);
  157. week.Owner = individual.Get(x => x.Id == week.OwnerId).FirstOrDefault();
  158. }
  159. return week;
  160. }
  161. public void Insert(TimeshareWeek item)
  162. {
  163. item = GetDetailedWeek(item);
  164. item.Id = _dbContext.Weeks.Max(x => x.Id) + 1;
  165. if (item.Owner != null && item.Owner.Id == 0)
  166. {
  167. item.Owner.Id = _dbContext.Individuals.Max(x => x.Id) + 1;
  168. }
  169. // Set starting Status of A1
  170. StatusRepository repo = new StatusRepository(_dbContext);
  171. item.Status = repo.GetDetailed(s => s.Code == "A1");
  172. if (item.Status != null)
  173. {
  174. //Create initial
  175. item.Status = new Status()
  176. {
  177. Code = "A1",
  178. Description = "Pending verification",
  179. StatusType = StatusType.Timeshare,
  180. ModifiedBy = "KobusB"
  181. };
  182. }
  183. item.Id = NewId();
  184. _dbContext.Add(item);
  185. Save();
  186. }
  187. public void Insert(IEnumerable<TimeshareWeek> items)
  188. {
  189. int id = NewId();
  190. foreach (var item in items)
  191. {
  192. item.Id = id;
  193. _dbContext.Add(item);
  194. id += 1;
  195. }
  196. Save();
  197. }
  198. public void Remove(TimeshareWeek item)
  199. {
  200. var i = _dbContext.Weeks.Find(item);
  201. _dbContext.Weeks.Remove(i);
  202. Save();
  203. }
  204. public void Remove(IEnumerable<TimeshareWeek> items)
  205. {
  206. foreach (var item in items)
  207. {
  208. var i = _dbContext.Weeks.Find(item);
  209. _dbContext.Weeks.Remove(i);
  210. }
  211. Save();
  212. }
  213. public void RemoveAtId(int item)
  214. {
  215. var i = _dbContext.Weeks.Find(item);
  216. _dbContext.Weeks.Remove(i);
  217. Save();
  218. }
  219. public void Save()
  220. {
  221. _dbContext.SaveChanges();
  222. }
  223. public void Update(TimeshareWeek item)
  224. {
  225. _dbContext.Entry(item).State = EntityState.Modified;
  226. Save();
  227. }
  228. public List<TimeshareWeek> GetBy(WeekFilterDto week)
  229. {
  230. List<TimeshareWeek> item = GetDetailedAll();
  231. if (!string.IsNullOrEmpty(week.RegionCode))
  232. {
  233. item = item.Where(x => x.Region != null && x.Region.Code?.ToLower() == week.RegionCode.ToLower()).ToList();
  234. }
  235. if (!string.IsNullOrEmpty(week.ResortCode))
  236. {
  237. item = item.Where(x => x.ResortCode?.ToLower() == week.ResortCode).ToList();
  238. }
  239. if (week.Date != null)
  240. {
  241. item = item.Where(x =>
  242. x.ArrivalDate >= week.Date.Value.AddDays(-7)
  243. && x.DepartureDate <= week.Date.Value.AddDays(7)).ToList();
  244. }
  245. if (week.MinAmount != null && week.MinAmount != 0)
  246. {
  247. item = item.Where(x => x.SellPrice >= week.MinAmount).ToList();
  248. }
  249. if (week.MaxAmount != null && week.MaxAmount != 0)
  250. {
  251. item.Where(x => x.SellPrice <= week.MaxAmount).ToList();
  252. }
  253. return item;
  254. }
  255. public int NewId()
  256. {
  257. int id = 0;
  258. if (_dbContext.Weeks.Count() > 0)
  259. {
  260. id = _dbContext.Weeks.Max(x => x.Id);
  261. }
  262. id += 1;
  263. return id;
  264. }
  265. private List<WeekDto> GetTenderWeeks()
  266. {
  267. List<WeekDto> list = new List<WeekDto>();
  268. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(MyCommon.TenderUrl);
  269. request.Method = "GET";
  270. WebResponse response = request.GetResponse();
  271. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  272. {
  273. string result = reader.ReadToEnd();
  274. if (result.Length > 0)
  275. {
  276. string cleanLine;
  277. string[] lines = result.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
  278. foreach (string line in lines)
  279. {
  280. cleanLine = line.Replace("<br>", "");
  281. cleanLine = line.Replace("<br/>", "");
  282. list.Add(new WeekDto(cleanLine));
  283. }
  284. }
  285. }
  286. // Check that all regions are same as other
  287. list
  288. .Where(x => x.Region != null && x.Region.RegionCode == "FN")
  289. .ToList()
  290. .ForEach(x => x.Region.RegionCode = ChangeRegion(x.Region.RegionCode));
  291. list = GetRegion(list);
  292. return list;
  293. }
  294. private List<WeekDto> GetRegion(List<WeekDto> list)
  295. {
  296. ProvinceRepository province = new ProvinceRepository(_dbContext);
  297. Province prov = null;
  298. foreach(var item in list)
  299. {
  300. prov = province.GetDetailed(x => x.Code == item.Region.RegionCode);
  301. item.Region = new RegionDto(prov.Id, prov.Code, prov.Description);
  302. }
  303. return list;
  304. }
  305. private string ChangeRegion(string value)
  306. {
  307. switch(value)
  308. {
  309. case "FN":
  310. return "KZN";
  311. default:
  312. return value;
  313. }
  314. }
  315. }
  316. }