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

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. 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. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  224. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-Owner");
  225. if (template != null)
  226. {
  227. templateRepository.SendEmailTemplate(template, property.Owner, list);
  228. }
  229. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-User");
  230. if (template != null)
  231. {
  232. if (individual != null)
  233. {
  234. templateRepository.SendEmailTemplate(template, individual, list);
  235. }
  236. }
  237. }
  238. }
  239. _dbContext.Add(bid);
  240. Save();
  241. }
  242. }
  243. public void Insert(IEnumerable<BidItem> items)
  244. {
  245. foreach (var item in items)
  246. {
  247. _dbContext.Add(item);
  248. }
  249. Save();
  250. }
  251. public void Remove(BidItem item)
  252. {
  253. var i = _dbContext.BidItems.Find(item);
  254. _dbContext.BidItems.Remove(i);
  255. Save();
  256. }
  257. public void Remove(IEnumerable<BidItem> items)
  258. {
  259. foreach (var item in items)
  260. {
  261. var i = _dbContext.BidItems.Find(item);
  262. _dbContext.BidItems.Remove(i);
  263. }
  264. Save();
  265. }
  266. public void RemoveAtId(int item)
  267. {
  268. var i = _dbContext.BidItems.Find(item);
  269. _dbContext.BidItems.Remove(i);
  270. Save();
  271. }
  272. public void Save()
  273. {
  274. _dbContext.SaveChanges();
  275. }
  276. public void Update(BidItem item)
  277. {
  278. _dbContext.Entry(item).State = EntityState.Modified;
  279. Save();
  280. }
  281. public BidItemDisplay AcceptBid(int id)
  282. {
  283. var item = _dbContext.BidItems
  284. .Include("Property")
  285. .Include("TimeshareWeek")
  286. .Include("BidMaker")
  287. .Include("Status")
  288. .Where(x => x.Id == id).FirstOrDefault();
  289. var status = (from s in _dbContext.Status
  290. where s.Code == "E2"
  291. select s).FirstOrDefault();
  292. if (status != null)
  293. {
  294. item.StatusId = status.Id;
  295. }
  296. _dbContext.Entry(item).State = EntityState.Modified;
  297. Save();
  298. List<BidItem> bids = new List<BidItem>() { item };
  299. return LoadDisplay(bids).Find(x => x.Id == item.Id);
  300. }
  301. public BidItemDisplay DecineBid(BitItemDecline item)
  302. {
  303. var bid = _dbContext.BidItems
  304. .Include("Property")
  305. .Include("TimeshareWeek")
  306. .Include("BidMaker")
  307. .Include("Status")
  308. .Where(x => x.Id == item.Id).FirstOrDefault();
  309. var status = (from s in _dbContext.Status
  310. where s.Code == "E3"
  311. select s).FirstOrDefault();
  312. if (status != null)
  313. {
  314. bid.StatusId = status.Id;
  315. }
  316. bid.DeclinedReason = item.Comment;
  317. _dbContext.Entry(bid).State = EntityState.Modified;
  318. Save();
  319. List<BidItem> bids = new List<BidItem>() { bid };
  320. return LoadDisplay(bids).Find(x => x.Id == bid.Id);
  321. }
  322. public int NewId()
  323. {
  324. // Not sure if properties need it
  325. return 0;
  326. }
  327. public BidItemDisplay GetDispaly(int id)
  328. {
  329. var bid = _dbContext.BidItems
  330. .Include("Property")
  331. .Include("TimeshareWeek")
  332. .Include("BidMaker")
  333. .Include("Status")
  334. .Where(x => x.Id == id).FirstOrDefault();
  335. return BidItemDisplay(bid);
  336. }
  337. private BidItemDisplay BidItemDisplay(BidItem item)
  338. {
  339. if (item == null)
  340. return null;
  341. BidItemDisplay bid = new BidItemDisplay()
  342. {
  343. Id = item.Id,
  344. Offer = (decimal)item.Amount,
  345. Comment = item.Comment,
  346. DeclineReason = item.DeclinedReason
  347. };
  348. if (item.PropertyId != null)
  349. {
  350. bid.Type = "Property";
  351. bid.ShortDescription = item.Property.ShortDescription;
  352. bid.Description = item.Property.Description;
  353. bid.Price = item.Property.Price;
  354. }
  355. if (item.TimeshareWeekId != null)
  356. {
  357. bid.Type = "Timeshare";
  358. bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
  359. bid.SellPrice = (decimal)item.TimeshareWeek.SellPrice;
  360. bid.Resort = item.TimeshareWeek.ResortName;
  361. bid.UnitNumber = item.TimeshareWeek.UnitNumber;
  362. bid.WeekNumber = item.TimeshareWeek.Module;
  363. }
  364. if (item.Status != null)
  365. {
  366. bid.StatusCode = item.Status.Code;
  367. bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
  368. }
  369. if (item.BidMaker != null)
  370. bid.MadeBy = (item.BidMaker.Name + " " + item.BidMaker.Surname).Trim();
  371. bid.Date = item.Created;
  372. return bid;
  373. }
  374. }
  375. }