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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using UnivateProperties_API.Containers.ProcessFlow;
  7. using UnivateProperties_API.Context;
  8. using UnivateProperties_API.Model.ProcessFlow;
  9. namespace UnivateProperties_API.Repository.ProccessFlow
  10. {
  11. public class BidRepository : IBidRepository
  12. {
  13. private readonly DataContext _dbContext;
  14. public BidRepository(DataContext dbContext)
  15. {
  16. _dbContext = dbContext;
  17. }
  18. public List<BidItem> Get(Func<BidItem, bool> where)
  19. {
  20. return _dbContext.BidItems.Where(where).ToList();
  21. }
  22. public List<BidItem> GetAll()
  23. {
  24. return _dbContext.BidItems.ToList();
  25. }
  26. public BidItem GetDetailed(Func<BidItem, bool> first)
  27. {
  28. var item = _dbContext.BidItems.FirstOrDefault(first);
  29. return item;
  30. }
  31. public List<BidItem> GetDetailedAll()
  32. {
  33. return GetAll();
  34. }
  35. public List<BidItemDisplay> GetAllBid()
  36. {
  37. List<BidItem> bids = _dbContext.BidItems
  38. .Include("Property")
  39. .Include("TimeshareWeek")
  40. .Include("BidMaker")
  41. .Include("Status")
  42. .Include("Property.Owner")
  43. .Include("Property.Agent")
  44. .ToList();
  45. return LoadDisplay(bids);
  46. }
  47. public List<BidItemDisplay> GetMyBid(Func<BidItem, bool> where)
  48. {
  49. List<BidItem> bids = _dbContext.BidItems
  50. .Include("Property")
  51. .Include("TimeshareWeek")
  52. .Include("BidMaker")
  53. .Include("Status")
  54. .Where(where)
  55. .ToList();
  56. return LoadDisplay(bids);
  57. }
  58. private List<BidItemDisplay> LoadDisplay(List<BidItem> bids)
  59. {
  60. List<BidItemDisplay> list = new List<BidItemDisplay>();
  61. foreach (BidItem item in bids)
  62. {
  63. BidItemDisplay bid = new BidItemDisplay()
  64. {
  65. Id = item.Id,
  66. Offer = (decimal)item.Amount,
  67. Comment = item.Comment,
  68. DeclineReason = item.DeclinedReason
  69. };
  70. if (item.PropertyId != null)
  71. {
  72. bid.Type = "Property";
  73. bid.ShortDescription = item.Property.ShortDescription;
  74. bid.Description = item.Property.Description;
  75. bid.Price = item.Property.Price;
  76. }
  77. if (item.TimeshareWeekId != null)
  78. {
  79. bid.Type = "Timeshare";
  80. bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
  81. bid.Price = (decimal)item.TimeshareWeek.SellPrice;
  82. bid.Resort = item.TimeshareWeek.ResortName;
  83. bid.Unit = item.TimeshareWeek.UnitNumber;
  84. bid.Module = item.TimeshareWeek.Module;
  85. }
  86. if (item.Status != null)
  87. {
  88. bid.StatusCode = item.Status.Code;
  89. bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
  90. }
  91. if (item.BidMaker != null)
  92. bid.MadeBy = item.BidMaker.Name + " " + item.BidMaker.Surname;
  93. bid.MadeBy = "Bob";
  94. list.Add(bid);
  95. }
  96. return list;
  97. }
  98. public void Insert(BidItem item)
  99. {
  100. var status = _dbContext.Status.Where(x => x.Code == "E1" && x.StatusType == StatusType.Bid).FirstOrDefault();
  101. if (status != null)
  102. item.StatusId = status.Id;
  103. _dbContext.Add(item);
  104. Save();
  105. }
  106. public void Insert(IEnumerable<BidItem> items)
  107. {
  108. foreach (var item in items)
  109. {
  110. _dbContext.Add(item);
  111. }
  112. Save();
  113. }
  114. public void Remove(BidItem item)
  115. {
  116. var i = _dbContext.BidItems.Find(item);
  117. _dbContext.BidItems.Remove(i);
  118. Save();
  119. }
  120. public void Remove(IEnumerable<BidItem> items)
  121. {
  122. foreach (var item in items)
  123. {
  124. var i = _dbContext.BidItems.Find(item);
  125. _dbContext.BidItems.Remove(i);
  126. }
  127. Save();
  128. }
  129. public void RemoveAtId(int item)
  130. {
  131. var i = _dbContext.BidItems.Find(item);
  132. _dbContext.BidItems.Remove(i);
  133. Save();
  134. }
  135. public void Save()
  136. {
  137. _dbContext.SaveChanges();
  138. }
  139. public void Update(BidItem item)
  140. {
  141. _dbContext.Entry(item).State = EntityState.Modified;
  142. Save();
  143. }
  144. public BidItemDisplay AcceptBid(int id)
  145. {
  146. var item = _dbContext.BidItems
  147. .Include("Property")
  148. .Include("TimeshareWeek")
  149. .Include("BidMaker")
  150. .Include("Status")
  151. .Where(x => x.Id == id).FirstOrDefault();
  152. var status = (from s in _dbContext.Status
  153. where s.Code == "E2"
  154. && s.StatusType == StatusType.Bid
  155. select s).FirstOrDefault();
  156. if (status != null)
  157. {
  158. item.StatusId = status.Id;
  159. }
  160. _dbContext.Entry(item).State = EntityState.Modified;
  161. Save();
  162. List<BidItem> bids = new List<BidItem>() { item };
  163. return LoadDisplay(bids).Find(x => x.Id == item.Id);
  164. }
  165. public BidItemDisplay DecineBid(BitItemDecline item)
  166. {
  167. var bid = _dbContext.BidItems
  168. .Include("Property")
  169. .Include("TimeshareWeek")
  170. .Include("BidMaker")
  171. .Include("Status")
  172. .Where(x => x.Id == item.Id).FirstOrDefault();
  173. var status = (from s in _dbContext.Status
  174. where s.Code == "E3"
  175. && s.StatusType == StatusType.Bid
  176. select s).FirstOrDefault();
  177. if (status != null)
  178. {
  179. bid.StatusId = status.Id;
  180. }
  181. bid.DeclinedReason = item.Comment;
  182. _dbContext.Entry(bid).State = EntityState.Modified;
  183. Save();
  184. List<BidItem> bids = new List<BidItem>() { bid };
  185. return LoadDisplay(bids).Find(x => x.Id == bid.Id);
  186. }
  187. }
  188. }