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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.Communication;
  10. using UnivateProperties_API.Model.ProcessFlow;
  11. using UnivateProperties_API.Repository.Communication;
  12. using UnivateProperties_API.Repository.Timeshare;
  13. namespace UnivateProperties_API.Repository.ProccessFlow
  14. {
  15. public class BidRepository : IBidRepository
  16. {
  17. private readonly DataContext _dbContext;
  18. public BidRepository(DataContext dbContext)
  19. {
  20. _dbContext = dbContext;
  21. }
  22. public List<BidItem> Get(Func<BidItem, bool> where)
  23. {
  24. return _dbContext.BidItems.Where(where).ToList();
  25. }
  26. public List<BidItem> GetAll()
  27. {
  28. return _dbContext.BidItems.ToList();
  29. }
  30. public BidItem GetDetailed(Func<BidItem, bool> first)
  31. {
  32. var item = _dbContext.BidItems.FirstOrDefault(first);
  33. return item;
  34. }
  35. public List<BidItem> GetDetailedAll()
  36. {
  37. return GetAll();
  38. }
  39. public List<BidItemDisplay> GetAllBid()
  40. {
  41. List<BidItem> bids = _dbContext.BidItems
  42. .Include("Property")
  43. .Include("TimeshareWeek")
  44. .Include("BidMaker")
  45. .Include("Status")
  46. .Include("Property.Owner")
  47. .Include("Property.Agent")
  48. .ToList();
  49. return LoadDisplay(bids);
  50. }
  51. public List<BidItemDisplay> GetMyBid(Func<BidItem, bool> where)
  52. {
  53. List<BidItem> bids = _dbContext.BidItems
  54. .Include("Property")
  55. .Include("TimeshareWeek")
  56. .Include("BidMaker")
  57. .Include("Status")
  58. .Where(where)
  59. .ToList();
  60. return LoadDisplay(bids);
  61. }
  62. private List<BidItemDisplay> LoadDisplay(List<BidItem> bids)
  63. {
  64. List<BidItemDisplay> list = new List<BidItemDisplay>();
  65. foreach (BidItem item in bids)
  66. {
  67. BidItemDisplay bid = new BidItemDisplay()
  68. {
  69. Id = item.Id,
  70. Offer = (decimal)item.Amount,
  71. Comment = item.Comment,
  72. DeclineReason = item.DeclinedReason
  73. };
  74. if (item.PropertyId != null)
  75. {
  76. bid.Type = "Property";
  77. bid.ShortDescription = item.Property.ShortDescription;
  78. bid.Description = item.Property.Description;
  79. bid.Price = item.Property.Price;
  80. }
  81. if (item.TimeshareWeekId != null)
  82. {
  83. bid.Type = "Timeshare";
  84. bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
  85. bid.Price = (decimal)item.TimeshareWeek.SellPrice;
  86. bid.Resort = item.TimeshareWeek.ResortName;
  87. bid.Unit = item.TimeshareWeek.UnitNumber;
  88. bid.Module = item.TimeshareWeek.Module;
  89. }
  90. if (item.Status != null)
  91. {
  92. bid.StatusCode = item.Status.Code;
  93. bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
  94. }
  95. if (item.BidMaker != null)
  96. bid.MadeBy = item.BidMaker.Name + " " + item.BidMaker.Surname;
  97. bid.MadeBy = "Bob";
  98. list.Add(bid);
  99. }
  100. return list;
  101. }
  102. public void Insert(BidItem item)
  103. {
  104. var week = _dbContext.Weeks.Find(item.TimeshareWeekId);
  105. if (!(item.TimeshareWeek == null && week == null))
  106. {
  107. if (week == null && item.TimeshareWeek != null)
  108. {
  109. MyCommon.PostToConsoft(item.TimeshareWeek);
  110. WeekRepository weekRepository = new WeekRepository(_dbContext);
  111. weekRepository.Insert(item.TimeshareWeek);
  112. }
  113. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  114. if (status != null)
  115. {
  116. List<BaseEntity> list = new List<BaseEntity>() { item, week, week.Owner };
  117. item.StatusId = status.Id;
  118. if (item.TimeshareWeekId != null)
  119. {
  120. if (week != null)
  121. {
  122. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  123. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  124. if (template != null)
  125. {
  126. templateRepository.SendEmailTemplate(template, week.Owner, list);
  127. }
  128. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  129. if (template != null)
  130. {
  131. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == item.BidMakerId);
  132. if (bidMaker != null)
  133. {
  134. templateRepository.SendEmailTemplate(template, bidMaker, list);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. _dbContext.Add(item);
  141. Save();
  142. }
  143. }
  144. public void Insert(IEnumerable<BidItem> items)
  145. {
  146. foreach (var item in items)
  147. {
  148. _dbContext.Add(item);
  149. }
  150. Save();
  151. }
  152. public void Remove(BidItem item)
  153. {
  154. var i = _dbContext.BidItems.Find(item);
  155. _dbContext.BidItems.Remove(i);
  156. Save();
  157. }
  158. public void Remove(IEnumerable<BidItem> items)
  159. {
  160. foreach (var item in items)
  161. {
  162. var i = _dbContext.BidItems.Find(item);
  163. _dbContext.BidItems.Remove(i);
  164. }
  165. Save();
  166. }
  167. public void RemoveAtId(int item)
  168. {
  169. var i = _dbContext.BidItems.Find(item);
  170. _dbContext.BidItems.Remove(i);
  171. Save();
  172. }
  173. public void Save()
  174. {
  175. _dbContext.SaveChanges();
  176. }
  177. public void Update(BidItem item)
  178. {
  179. _dbContext.Entry(item).State = EntityState.Modified;
  180. Save();
  181. }
  182. public BidItemDisplay AcceptBid(int id)
  183. {
  184. var item = _dbContext.BidItems
  185. .Include("Property")
  186. .Include("TimeshareWeek")
  187. .Include("BidMaker")
  188. .Include("Status")
  189. .Where(x => x.Id == id).FirstOrDefault();
  190. var status = (from s in _dbContext.Status
  191. where s.Code == "E2"
  192. select s).FirstOrDefault();
  193. if (status != null)
  194. {
  195. item.StatusId = status.Id;
  196. }
  197. _dbContext.Entry(item).State = EntityState.Modified;
  198. Save();
  199. List<BidItem> bids = new List<BidItem>() { item };
  200. return LoadDisplay(bids).Find(x => x.Id == item.Id);
  201. }
  202. public BidItemDisplay DecineBid(BitItemDecline item)
  203. {
  204. var bid = _dbContext.BidItems
  205. .Include("Property")
  206. .Include("TimeshareWeek")
  207. .Include("BidMaker")
  208. .Include("Status")
  209. .Where(x => x.Id == item.Id).FirstOrDefault();
  210. var status = (from s in _dbContext.Status
  211. where s.Code == "E3"
  212. select s).FirstOrDefault();
  213. if (status != null)
  214. {
  215. bid.StatusId = status.Id;
  216. }
  217. bid.DeclinedReason = item.Comment;
  218. _dbContext.Entry(bid).State = EntityState.Modified;
  219. Save();
  220. List<BidItem> bids = new List<BidItem>() { bid };
  221. return LoadDisplay(bids).Find(x => x.Id == bid.Id);
  222. }
  223. public int NewId()
  224. {
  225. // Not sure if properties need it
  226. return 0;
  227. }
  228. }
  229. }