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.

BidRepository.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Containers.ProcessFlow;
  6. using UnivateProperties_API.Context;
  7. using UnivateProperties_API.Helpers;
  8. using UnivateProperties_API.Model;
  9. using UnivateProperties_API.Model.ProcessFlow;
  10. using UnivateProperties_API.Repository.Communication;
  11. using UnivateProperties_API.Repository.Timeshare;
  12. namespace UnivateProperties_API.Repository.ProccessFlow
  13. {
  14. public class BidRepository : IBidRepository
  15. {
  16. private readonly DataContext _dbContext;
  17. public BidRepository(DataContext dbContext)
  18. {
  19. _dbContext = dbContext;
  20. }
  21. public List<BidItem> Get(Func<BidItem, bool> where)
  22. {
  23. return _dbContext.BidItems.Where(where).ToList();
  24. }
  25. public List<BidItem> GetAll()
  26. {
  27. return _dbContext.BidItems.ToList();
  28. }
  29. public BidItem GetDetailed(Func<BidItem, bool> first)
  30. {
  31. var item = _dbContext.BidItems.FirstOrDefault(first);
  32. return item;
  33. }
  34. public List<BidItem> GetDetailedAll()
  35. {
  36. return GetAll();
  37. }
  38. public List<BidItemDisplay> GetAllBid()
  39. {
  40. List<BidItem> bids = _dbContext.BidItems
  41. .Include("Property")
  42. .Include("TimeshareWeek")
  43. .Include("BidMaker")
  44. .Include("Status")
  45. .Include("Property.Owner")
  46. .Include("Property.Agent")
  47. .ToList();
  48. return LoadDisplay(bids);
  49. }
  50. public List<BidItemDisplay> GetMyBid(Func<BidItem, bool> where)
  51. {
  52. List<BidItem> bids = _dbContext.BidItems
  53. .Include("Property")
  54. .Include("TimeshareWeek")
  55. .Include("BidMaker")
  56. .Include("Status")
  57. .Where(where)
  58. .ToList();
  59. return LoadDisplay(bids);
  60. }
  61. public List<BidItemDisplay> GetAllBidBy(string name)
  62. {
  63. /*
  64. * Agency - can see all its agents' listings
  65. * Agent - only see own listings
  66. * Private User - only see own listings
  67. * Super Admin - can see all
  68. */
  69. var user = _dbContext.Users.Where(u => u.Username == name).FirstOrDefault();
  70. if (user != null)
  71. {
  72. if (user.Role.ToUpper() == "AGENCY" || user.Role.ToUpper() == "AGENT")
  73. {
  74. var agent = _dbContext.Agents.Where(a => a.UserId == user.Id).FirstOrDefault();
  75. if (user.Role.ToUpper() == "AGENCY")
  76. {
  77. List<BidItem> bids = _dbContext.BidItems
  78. .Include("Property")
  79. .Include("TimeshareWeek")
  80. .Include("BidMaker")
  81. .Include("Status")
  82. .Where(x => x.TimeshareWeek.AgencyId == agent.AgencyId || x.Property.AgencyId == agent.AgencyId)
  83. .ToList();
  84. return LoadDisplay(bids);
  85. }
  86. else
  87. {
  88. List<BidItem> bids = _dbContext.BidItems
  89. .Include("Property")
  90. .Include("TimeshareWeek")
  91. .Include("BidMaker")
  92. .Include("Status")
  93. .Where(x => x.TimeshareWeek.AgentId == agent.Id || x.Property.AgentId == agent.Id)
  94. .ToList();
  95. return LoadDisplay(bids);
  96. }
  97. }
  98. if (user.Role.ToUpper() == "PRIVATE USER")
  99. {
  100. var individual = _dbContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
  101. List<BidItem> bids = _dbContext.BidItems
  102. .Include("Property")
  103. .Include("TimeshareWeek")
  104. .Include("BidMaker")
  105. .Include("Status")
  106. .Where(x => x.TimeshareWeek.OwnerId == individual.Id || x.Property.OwnerId == individual.Id)
  107. .ToList();
  108. return LoadDisplay(bids);
  109. }
  110. if (user.Role.ToUpper() == "SUPER ADMIN")
  111. return GetAllBid();
  112. }
  113. return null;
  114. }
  115. private List<BidItemDisplay> LoadDisplay(List<BidItem> bids)
  116. {
  117. List<BidItemDisplay> list = new List<BidItemDisplay>();
  118. foreach (BidItem item in bids)
  119. {
  120. BidItemDisplay bid = BidItemDisplay(item);
  121. list.Add(bid);
  122. }
  123. return list;
  124. }
  125. public void Insert(BidItem item)
  126. {
  127. var week = _dbContext.Weeks.Find(item.TimeshareWeekId);
  128. if (!(item.TimeshareWeek == null && week == null))
  129. {
  130. if (week == null && item.TimeshareWeek != null)
  131. {
  132. MyCommon.PostToConsoft(item.TimeshareWeek);
  133. WeekRepository weekRepository = new WeekRepository(_dbContext);
  134. weekRepository.Insert(item.TimeshareWeek);
  135. }
  136. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  137. if (status != null)
  138. {
  139. List<BaseEntity> list = new List<BaseEntity>() { item, week, week.Owner };
  140. item.StatusId = status.Id;
  141. if (item.TimeshareWeekId != null)
  142. {
  143. if (week != null)
  144. {
  145. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  146. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  147. if (template != null)
  148. {
  149. templateRepository.SendEmailTemplate(template, week.Owner, list);
  150. }
  151. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  152. if (template != null)
  153. {
  154. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == item.BidMakerId);
  155. if (bidMaker != null)
  156. {
  157. templateRepository.SendEmailTemplate(template, bidMaker, list);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. _dbContext.Add(item);
  164. Save();
  165. }
  166. }
  167. public void InsertNew(BidItemNew item)
  168. {
  169. var week = _dbContext.Weeks.Include("Owner").Where(x => x.Id == item.TimeshareWeekId).FirstOrDefault();
  170. var property = _dbContext.Properties.Include("Owner").Where(x => x.Id == item.PropertyId).FirstOrDefault();
  171. BidItem bid = new BidItem();
  172. foreach (string prop in bid.GetAllProperties())
  173. {
  174. if (prop != "Item" && prop != "Display")
  175. bid[prop] = item[prop];
  176. }
  177. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  178. if (status != null)
  179. bid.StatusId = status.Id;
  180. var individual = _dbContext.Individuals.Where(i => i.UserId == item.UserId).FirstOrDefault();
  181. if (individual != null)
  182. bid.BidMakerId = individual.Id;
  183. if (!(bid.TimeshareWeek == null && week == null))
  184. {
  185. if (week == null && bid.TimeshareWeek != null)
  186. {
  187. MyCommon.PostToConsoft(bid.TimeshareWeek);
  188. WeekRepository weekRepository = new WeekRepository(_dbContext);
  189. weekRepository.Insert(bid.TimeshareWeek);
  190. }
  191. List<BaseEntity> list = new List<BaseEntity>() { bid, week, week.Owner };
  192. if (bid.TimeshareWeekId != null)
  193. {
  194. if (week != null)
  195. {
  196. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  197. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  198. if (template != null)
  199. {
  200. templateRepository.SendEmailTemplate(template, week.Owner, list);
  201. }
  202. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  203. if (template != null)
  204. {
  205. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == bid.BidMakerId);
  206. if (bidMaker != null)
  207. {
  208. templateRepository.SendEmailTemplate(template, bidMaker, list);
  209. }
  210. }
  211. }
  212. }
  213. _dbContext.Add(bid);
  214. Save();
  215. }
  216. else
  217. {
  218. List<BaseEntity> list = new List<BaseEntity>() { bid, property, property.Owner };
  219. if (bid.PropertyId != null)
  220. {
  221. if (property != null)
  222. {
  223. var propertyStatus = _dbContext.Status.Where(s => s.Code == "P4").FirstOrDefault();
  224. if (propertyStatus != null)
  225. {
  226. property.StatusId = propertyStatus.Id;
  227. _dbContext.Update(property);
  228. }
  229. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  230. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-Owner");
  231. if (template != null)
  232. {
  233. templateRepository.SendEmailTemplate(template, property.Owner, list);
  234. }
  235. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-User");
  236. if (template != null)
  237. {
  238. if (individual != null)
  239. {
  240. templateRepository.SendEmailTemplate(template, individual, list);
  241. }
  242. }
  243. }
  244. }
  245. _dbContext.Add(bid);
  246. Save();
  247. }
  248. item.Id = bid.Id;
  249. }
  250. public void Insert(IEnumerable<BidItem> items)
  251. {
  252. foreach (var item in items)
  253. {
  254. _dbContext.Add(item);
  255. }
  256. Save();
  257. }
  258. public void Remove(BidItem item)
  259. {
  260. var i = _dbContext.BidItems.Find(item);
  261. _dbContext.BidItems.Remove(i);
  262. Save();
  263. }
  264. public void Remove(IEnumerable<BidItem> items)
  265. {
  266. foreach (var item in items)
  267. {
  268. var i = _dbContext.BidItems.Find(item);
  269. _dbContext.BidItems.Remove(i);
  270. }
  271. Save();
  272. }
  273. public void RemoveAtId(int item)
  274. {
  275. var i = _dbContext.BidItems.Find(item);
  276. _dbContext.BidItems.Remove(i);
  277. Save();
  278. }
  279. public void Save()
  280. {
  281. _dbContext.SaveChanges();
  282. }
  283. public void Update(BidItem item)
  284. {
  285. _dbContext.Entry(item).State = EntityState.Modified;
  286. Save();
  287. }
  288. public BidItemDisplay AcceptBid(int id)
  289. {
  290. var item = _dbContext.BidItems
  291. .Include("Property")
  292. .Include("TimeshareWeek")
  293. .Include("BidMaker")
  294. .Include("Status")
  295. .Where(x => x.Id == id).FirstOrDefault();
  296. var status = (from s in _dbContext.Status
  297. where s.Code == "E2"
  298. select s).FirstOrDefault();
  299. if (status != null)
  300. {
  301. item.StatusId = status.Id;
  302. }
  303. if (item.Property != null)
  304. {
  305. var propertyStatus = _dbContext.Status.Where(p => p.Code == "P5").FirstOrDefault();
  306. if (propertyStatus != null)
  307. {
  308. item.Property.StatusId = propertyStatus.Id;
  309. item.Property.Published = false;
  310. }
  311. }
  312. _dbContext.Entry(item).State = EntityState.Modified;
  313. Save();
  314. List<BidItem> bids = new List<BidItem>() { item };
  315. return LoadDisplay(bids).Find(x => x.Id == item.Id);
  316. }
  317. public BidItemDisplay DecineBid(BitItemDecline item)
  318. {
  319. var bid = _dbContext.BidItems
  320. .Include("Property")
  321. .Include("TimeshareWeek")
  322. .Include("BidMaker")
  323. .Include("Status")
  324. .Where(x => x.Id == item.Id).FirstOrDefault();
  325. var status = (from s in _dbContext.Status
  326. where s.Code == "E3"
  327. select s).FirstOrDefault();
  328. if (status != null)
  329. {
  330. bid.StatusId = status.Id;
  331. }
  332. bid.DeclinedReason = item.Comment;
  333. if (bid.Property != null)
  334. {
  335. var propertyStatus = _dbContext.Status.Where(p => p.Code == "P3").FirstOrDefault();
  336. if (propertyStatus != null)
  337. {
  338. bid.Property.StatusId = propertyStatus.Id;
  339. }
  340. }
  341. _dbContext.Entry(bid).State = EntityState.Modified;
  342. Save();
  343. List<BidItem> bids = new List<BidItem>() { bid };
  344. return LoadDisplay(bids).Find(x => x.Id == bid.Id);
  345. }
  346. public int NewId()
  347. {
  348. // Not sure if properties need it
  349. return 0;
  350. }
  351. public BidItemDisplay GetDispaly(int id)
  352. {
  353. var bid = _dbContext.BidItems
  354. .Include("Property")
  355. .Include("TimeshareWeek")
  356. .Include("BidMaker")
  357. .Include("Status")
  358. .Where(x => x.Id == id).FirstOrDefault();
  359. return BidItemDisplay(bid);
  360. }
  361. private BidItemDisplay BidItemDisplay(BidItem item)
  362. {
  363. if (item == null)
  364. return null;
  365. BidItemDisplay bid = new BidItemDisplay()
  366. {
  367. Id = item.Id,
  368. Offer = (decimal)item.Amount,
  369. Comment = item.Comment,
  370. DeclineReason = item.DeclinedReason
  371. };
  372. if (item.PropertyId != null)
  373. {
  374. bid.Type = "Property";
  375. bid.ShortDescription = item.Property.ShortDescription;
  376. bid.Description = item.Property.Description;
  377. bid.Price = item.Property.Price;
  378. }
  379. if (item.TimeshareWeekId != null)
  380. {
  381. bid.Type = "Timeshare";
  382. bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
  383. bid.SellPrice = (decimal)item.TimeshareWeek.SellPrice;
  384. bid.Resort = item.TimeshareWeek.ResortName;
  385. bid.UnitNumber = item.TimeshareWeek.UnitNumber;
  386. bid.WeekNumber = item.TimeshareWeek.Module;
  387. }
  388. if (item.Status != null)
  389. {
  390. bid.StatusCode = item.Status.Code;
  391. bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
  392. }
  393. if (item.BidMaker != null)
  394. bid.MadeBy = (item.BidMaker.Name + " " + item.BidMaker.Surname).Trim();
  395. bid.Date = item.Created;
  396. return bid;
  397. }
  398. }
  399. }