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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. bid.Id = _dbContext.GetMaxID("BidItems") + 1;
  173. foreach (string prop in bid.GetAllProperties())
  174. {
  175. if (prop != "Item" && prop != "Display")
  176. bid[prop] = item[prop];
  177. }
  178. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  179. if (status != null)
  180. bid.StatusId = status.Id;
  181. var individual = _dbContext.Individuals.Where(i => i.UserId == item.UserId).FirstOrDefault();
  182. if (individual != null)
  183. bid.BidMakerId = individual.Id;
  184. if (!(bid.TimeshareWeek == null && week == null))
  185. {
  186. if (week == null && bid.TimeshareWeek != null)
  187. {
  188. MyCommon.PostToConsoft(bid.TimeshareWeek);
  189. WeekRepository weekRepository = new WeekRepository(_dbContext);
  190. weekRepository.Insert(bid.TimeshareWeek);
  191. }
  192. List<BaseEntity> list = new List<BaseEntity>() { bid, week, week.Owner };
  193. if (bid.TimeshareWeekId != null)
  194. {
  195. if (week != null)
  196. {
  197. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  198. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  199. if (template != null)
  200. {
  201. templateRepository.SendEmailTemplate(template, week.Owner, list);
  202. }
  203. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  204. if (template != null)
  205. {
  206. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == bid.BidMakerId);
  207. if (bidMaker != null)
  208. {
  209. templateRepository.SendEmailTemplate(template, bidMaker, list);
  210. }
  211. }
  212. }
  213. }
  214. _dbContext.Add(bid);
  215. Save();
  216. }
  217. else
  218. {
  219. List<BaseEntity> list = new List<BaseEntity>() { bid, property, property.Owner };
  220. if (bid.PropertyId != null)
  221. {
  222. if (property != null)
  223. {
  224. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  225. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-Owner");
  226. if (template != null)
  227. {
  228. templateRepository.SendEmailTemplate(template, property.Owner, list);
  229. }
  230. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-User");
  231. if (template != null)
  232. {
  233. if (individual != null)
  234. {
  235. templateRepository.SendEmailTemplate(template, individual, list);
  236. }
  237. }
  238. }
  239. }
  240. _dbContext.Add(bid);
  241. Save();
  242. }
  243. item.Id = bid.Id;
  244. }
  245. public void Insert(IEnumerable<BidItem> items)
  246. {
  247. foreach (var item in items)
  248. {
  249. _dbContext.Add(item);
  250. }
  251. Save();
  252. }
  253. public void Remove(BidItem item)
  254. {
  255. var i = _dbContext.BidItems.Find(item);
  256. _dbContext.BidItems.Remove(i);
  257. Save();
  258. }
  259. public void Remove(IEnumerable<BidItem> items)
  260. {
  261. foreach (var item in items)
  262. {
  263. var i = _dbContext.BidItems.Find(item);
  264. _dbContext.BidItems.Remove(i);
  265. }
  266. Save();
  267. }
  268. public void RemoveAtId(int item)
  269. {
  270. var i = _dbContext.BidItems.Find(item);
  271. _dbContext.BidItems.Remove(i);
  272. Save();
  273. }
  274. public void Save()
  275. {
  276. _dbContext.SaveChanges();
  277. }
  278. public void Update(BidItem item)
  279. {
  280. _dbContext.Entry(item).State = EntityState.Modified;
  281. Save();
  282. }
  283. public BidItemDisplay AcceptBid(int id)
  284. {
  285. var item = _dbContext.BidItems
  286. .Include("Property")
  287. .Include("TimeshareWeek")
  288. .Include("BidMaker")
  289. .Include("Status")
  290. .Where(x => x.Id == id).FirstOrDefault();
  291. var status = (from s in _dbContext.Status
  292. where s.Code == "E2"
  293. select s).FirstOrDefault();
  294. if (status != null)
  295. {
  296. item.StatusId = status.Id;
  297. }
  298. _dbContext.Entry(item).State = EntityState.Modified;
  299. Save();
  300. List<BidItem> bids = new List<BidItem>() { item };
  301. return LoadDisplay(bids).Find(x => x.Id == item.Id);
  302. }
  303. public BidItemDisplay DecineBid(BitItemDecline item)
  304. {
  305. var bid = _dbContext.BidItems
  306. .Include("Property")
  307. .Include("TimeshareWeek")
  308. .Include("BidMaker")
  309. .Include("Status")
  310. .Where(x => x.Id == item.Id).FirstOrDefault();
  311. var status = (from s in _dbContext.Status
  312. where s.Code == "E3"
  313. select s).FirstOrDefault();
  314. if (status != null)
  315. {
  316. bid.StatusId = status.Id;
  317. }
  318. bid.DeclinedReason = item.Comment;
  319. _dbContext.Entry(bid).State = EntityState.Modified;
  320. Save();
  321. List<BidItem> bids = new List<BidItem>() { bid };
  322. return LoadDisplay(bids).Find(x => x.Id == bid.Id);
  323. }
  324. public int NewId()
  325. {
  326. // Not sure if properties need it
  327. return 0;
  328. }
  329. public BidItemDisplay GetDispaly(int id)
  330. {
  331. var bid = _dbContext.BidItems
  332. .Include("Property")
  333. .Include("TimeshareWeek")
  334. .Include("BidMaker")
  335. .Include("Status")
  336. .Where(x => x.Id == id).FirstOrDefault();
  337. return BidItemDisplay(bid);
  338. }
  339. private BidItemDisplay BidItemDisplay(BidItem item)
  340. {
  341. if (item == null)
  342. return null;
  343. BidItemDisplay bid = new BidItemDisplay()
  344. {
  345. Id = item.Id,
  346. Offer = (decimal)item.Amount,
  347. Comment = item.Comment,
  348. DeclineReason = item.DeclinedReason
  349. };
  350. if (item.PropertyId != null)
  351. {
  352. bid.Type = "Property";
  353. bid.ShortDescription = item.Property.ShortDescription;
  354. bid.Description = item.Property.Description;
  355. bid.Price = item.Property.Price;
  356. }
  357. if (item.TimeshareWeekId != null)
  358. {
  359. bid.Type = "Timeshare";
  360. bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
  361. bid.SellPrice = (decimal)item.TimeshareWeek.SellPrice;
  362. bid.Resort = item.TimeshareWeek.ResortName;
  363. bid.UnitNumber = item.TimeshareWeek.UnitNumber;
  364. bid.WeekNumber = item.TimeshareWeek.Module;
  365. }
  366. if (item.Status != null)
  367. {
  368. bid.StatusCode = item.Status.Code;
  369. bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
  370. }
  371. if (item.BidMaker != null)
  372. bid.MadeBy = (item.BidMaker.Name + " " + item.BidMaker.Surname).Trim();
  373. bid.Date = item.Created;
  374. return bid;
  375. }
  376. }
  377. }