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

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