API

PropertyRepository.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using Microsoft.EntityFrameworkCore;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnivateProperties_API.Containers.Property;
  7. using UnivateProperties_API.Context;
  8. using UnivateProperties_API.Model.Properties;
  9. namespace UnivateProperties_API.Repository.Properties
  10. {
  11. public class PropertyRepository : IPropertyRepository
  12. {
  13. private readonly DataContext dBContext;
  14. public PropertyRepository(DataContext _dBContext)
  15. {
  16. dBContext = _dBContext;
  17. }
  18. public List<Property> Get(Func<Property, bool> where)
  19. {
  20. return dBContext.Properties
  21. .Include("PropertyType")
  22. .Include("Province")
  23. .Include("City")
  24. .Include("Suburb")
  25. .Where(where).ToList();
  26. }
  27. public List<Property> GetAll()
  28. {
  29. var properties = dBContext.Properties.ToList();
  30. return properties;
  31. }
  32. public Property GetDetailed(Func<Property, bool> first)
  33. {
  34. var property = dBContext.Properties.Include("Status").FirstOrDefault(first);
  35. if (property != null)
  36. {
  37. GetDetail(ref property);
  38. }
  39. return property;
  40. }
  41. public List<Property> GetDetailedAll()
  42. {
  43. var properties = dBContext.Properties.ToList();
  44. return properties;
  45. }
  46. private void GetDetail(ref Property property)
  47. {
  48. int propID = property.Id;
  49. var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
  50. property.Province = dBContext.Provinces.Find(property.ProvinceId);
  51. property.City = dBContext.Cities.Find(property.CityId);
  52. property.Suburb = dBContext.Suburbs.Find(property.SuburbId);
  53. property.DisplayData = new List<PropertyDetailGroup>();
  54. var groups = (from g in dBContext.UserDefinedGroups
  55. where g.UsageType == propertyType.UsageType
  56. || g.UsageType == PropertyUsageType.Both
  57. orderby g.Rank
  58. select g).ToList();
  59. foreach (UserDefinedGroup uGroup in groups)
  60. {
  61. var groupFields = (from f in dBContext.PropertyUserFields
  62. join uf in dBContext.UserDefinedFields on f.UserDefinedFieldId equals uf.Id
  63. join g in dBContext.UserDefinedGroups on uf.GroupId equals g.Id
  64. where f.PropertyId == propID
  65. && g.Id == uGroup.Id
  66. orderby g.Rank, uf.Rank
  67. select new { uf.FieldName, f.Value, f.Description }).ToList();
  68. if (groupFields.Count > 0)
  69. {
  70. PropertyDetailGroup detailGroup = new PropertyDetailGroup()
  71. {
  72. GroupName = uGroup.Description,
  73. Values = new List<PropertyDetail>()
  74. };
  75. if (uGroup.Description == "Property Overview")
  76. {
  77. detailGroup.Values.Add(new PropertyDetail()
  78. {
  79. Name = "Property Type",
  80. Value = property.PropertyType.Description
  81. });
  82. }
  83. foreach (var val in groupFields)
  84. {
  85. var irem = new PropertyDetail()
  86. {
  87. Name = val.FieldName,
  88. Description = val.Description
  89. };
  90. detailGroup.Values.Add(irem);
  91. if ((val.FieldName == "Erf Size" || val.FieldName == "Floor Size") && val.Value.EndsWith("2"))
  92. {
  93. irem.Value = val.Value.Substring(0, val.Value.Length - 1) + "<sup>" + val.Value.Last() + "</sup>";
  94. }
  95. else
  96. irem.Value = val.Value;
  97. }
  98. property.DisplayData.Add(detailGroup);
  99. }
  100. }
  101. }
  102. public void Insert(Property item)
  103. {
  104. PropertyType pt = dBContext.PropertyTypes.Find(item.PropertyTypeId);
  105. if (pt != null)
  106. {
  107. if (pt.UsageType == PropertyUsageType.Residential)
  108. {
  109. string type = dBContext.PropertyTypes.Find(item.PropertyTypeId).Description;
  110. if (item.PropertyUserFields.Count > 0)
  111. {
  112. string shortDesc = "{0} {1} {2}";
  113. UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
  114. var udValue = item.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
  115. if (udValue != null)
  116. item.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
  117. else
  118. item.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
  119. }
  120. else
  121. {
  122. item.ShortDescription = type;
  123. }
  124. }
  125. else
  126. {
  127. item.ShortDescription = pt.Description;
  128. }
  129. }
  130. var images = item.PropertyImages;
  131. var fields = item.PropertyUserFields;
  132. item.PropertyImages = null;
  133. item.PropertyUserFields = null;
  134. dBContext.Properties.Add(item);
  135. Save();
  136. if (images != null)
  137. {
  138. var lastID = (from p in dBContext.PropertyImages
  139. orderby p.Id descending
  140. select p.Id).FirstOrDefault();
  141. foreach (PropertyImage image in images)
  142. {
  143. lastID++;
  144. image.Id = lastID;
  145. image.PropertyId = item.Id;
  146. dBContext.PropertyImages.Add(image);
  147. Save();
  148. }
  149. }
  150. if (fields != null)
  151. {
  152. var lastID = (from p in dBContext.PropertyUserFields
  153. orderby p.Id descending
  154. select p.Id).FirstOrDefault();
  155. foreach (PropertyUserField field in fields)
  156. {
  157. lastID++;
  158. field.Id = lastID;
  159. field.PropertyId = item.Id;
  160. dBContext.PropertyUserFields.Add(field);
  161. Save();
  162. }
  163. }
  164. }
  165. public void Insert(IEnumerable<Property> items)
  166. {
  167. foreach (var item in items)
  168. {
  169. dBContext.Properties.Add(item);
  170. }
  171. Save();
  172. }
  173. public void Remove(Property item)
  174. {
  175. dBContext.Properties.Remove(item);
  176. Save();
  177. }
  178. public void Remove(IEnumerable<Property> items)
  179. {
  180. foreach (var item in items)
  181. {
  182. dBContext.Properties.Remove(item);
  183. }
  184. Save();
  185. }
  186. public void RemoveAtId(int item)
  187. {
  188. var property = Get(x => x.Id == item).FirstOrDefault();
  189. if (property != null)
  190. {
  191. dBContext.Properties.Remove(property);
  192. Save();
  193. }
  194. }
  195. public void Save()
  196. {
  197. dBContext.SaveChanges();
  198. }
  199. public void Update(Property item)
  200. {
  201. dBContext.Entry(item).State = EntityState.Modified;
  202. Save();
  203. }
  204. public List<PropertyDisplay> GetDisplay()
  205. {
  206. List<Property> props = GetAll();
  207. return GetDisplayDetails(props);
  208. }
  209. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  210. {
  211. List<Property> props;
  212. if (where != null)
  213. props = Get(where);
  214. else
  215. props = GetAll();
  216. return GetDisplayDetails(props);
  217. }
  218. public List<PropertyDisplay> GetDisplay(PropertySearch search)
  219. {
  220. //Save to Log
  221. dBContext.SearchLogs.Add(new Model.Logging.SearchLog()
  222. {
  223. Type = "Property",
  224. Search = JsonConvert.SerializeObject(search)
  225. });
  226. Save();
  227. if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL")
  228. {
  229. string keyword = search.Keyword.ToLower();
  230. List<Property> props = (from p in dBContext.Properties
  231. join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
  232. join c in dBContext.Cities on p.CityId equals c.Id
  233. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  234. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  235. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
  236. || EF.Functions.Like(pr.Description.ToLower(), $"%{keyword}%")
  237. || EF.Functions.Like(c.Description.ToLower(), $"%{keyword}%")
  238. || EF.Functions.Like(s.Description.ToLower(), $"%{keyword}%")
  239. || EF.Functions.Like(pt.Description.ToLower(), $"%{keyword}%")
  240. select p).ToList();
  241. return GetDisplayDetails(props);
  242. }
  243. else
  244. {
  245. List<Property> props;
  246. PropertyUsageType uType = PropertyUsageType.Both;
  247. if (!string.IsNullOrEmpty(search.PropertyUsageType) && search.PropertyUsageType != "undefined")
  248. {
  249. if (search.PropertyUsageType.ToUpper() == "COMMERCIAL")
  250. uType = PropertyUsageType.Commercial;
  251. else
  252. uType = PropertyUsageType.Residential;
  253. }
  254. props = (from p in dBContext.Properties
  255. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  256. where pt.UsageType == uType
  257. select p).ToList();
  258. if (!string.IsNullOrEmpty(search.SalesType) && search.SalesType != "undefined" && search.SalesType.ToUpper() != "ALL")
  259. {
  260. if (search.SalesType.ToUpper() == "SALE")
  261. props = props.Where(p => p.IsSale).ToList();
  262. else
  263. props = props.Where(p => !p.IsSale).ToList();
  264. }
  265. if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
  266. {
  267. props = (from p in props
  268. join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
  269. where pp.Description.ToUpper() == search.Province.ToUpper()
  270. select p).ToList();
  271. }
  272. if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
  273. {
  274. props = (from p in props
  275. join c in dBContext.Cities on p.CityId equals c.Id
  276. where c.Description.ToUpper() == search.City.ToUpper()
  277. select p).ToList();
  278. }
  279. if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
  280. {
  281. props = (from p in props
  282. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  283. where s.Description.ToUpper() == search.Suburb.ToUpper()
  284. select p).ToList();
  285. }
  286. if (!string.IsNullOrEmpty(search.PropertyType) && search.PropertyType != "Undefined" && search.PropertyType.ToUpper() != "ALL")
  287. {
  288. var pType = dBContext.PropertyTypes.Where(t => t.Description == search.PropertyType).FirstOrDefault();
  289. if (pType != null)
  290. {
  291. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  292. }
  293. }
  294. return GetDisplayDetails(props);
  295. }
  296. }
  297. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  298. {
  299. var properties = new List<PropertyDisplay>();
  300. foreach (var item in props)
  301. {
  302. PropertyDisplay display = new PropertyDisplay
  303. {
  304. Id = item.Id,
  305. ShortDescription = item.ShortDescription,
  306. IsSale = item.IsSale,
  307. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  308. DisplayImage = (from i in dBContext.PropertyImages
  309. where i.PropertyId == item.Id
  310. && i.IsDefault
  311. select i.Image).FirstOrDefault(),
  312. Area = (from u in dBContext.PropertyUserFields
  313. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  314. where u.PropertyId == item.Id
  315. && f.FieldName == "Floor Size"
  316. select u.Value).FirstOrDefault(),
  317. Beds = (from u in dBContext.PropertyUserFields
  318. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  319. where u.PropertyId == item.Id
  320. && f.FieldName == "Bedrooms"
  321. select u.Value).FirstOrDefault(),
  322. Baths = (from u in dBContext.PropertyUserFields
  323. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  324. where u.PropertyId == item.Id
  325. && f.FieldName == "Bathrooms"
  326. select u.Value).FirstOrDefault(),
  327. Garages = (from u in dBContext.PropertyUserFields
  328. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  329. where u.PropertyId == item.Id
  330. && f.FieldName == "Garages"
  331. select u.Value).FirstOrDefault(),
  332. Province = (from p in dBContext.Provinces
  333. where p.Id == item.ProvinceId
  334. select p.Description).FirstOrDefault(),
  335. City = (from c in dBContext.Cities
  336. where c.Id == item.CityId
  337. select c.Description).FirstOrDefault(),
  338. Suburb = (from s in dBContext.Suburbs
  339. where s.Id == item.SuburbId
  340. select s.Description).FirstOrDefault(),
  341. Price = item.Price,
  342. DateCreated = item.Created
  343. };
  344. if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
  345. {
  346. display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
  347. }
  348. properties.Add(display);
  349. }
  350. return properties;
  351. }
  352. public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
  353. {
  354. List<PropertyType> list;
  355. if (where != null)
  356. list = dBContext.PropertyTypes.Where(where).ToList();
  357. else
  358. list = dBContext.PropertyTypes.ToList();
  359. return list;
  360. }
  361. public List<PropertyDisplay> GetLatestDisplay()
  362. {
  363. List<Property> props = GetAll().OrderBy(x => x.Created).Take(3).ToList();
  364. return GetDisplayDetails(props);
  365. }
  366. public List<PropertyList> GetPropertyList(Func<Property, bool> where)
  367. {
  368. var properties = Get(where);
  369. List<PropertyList> list = new List<PropertyList>();
  370. foreach (Property p in properties)
  371. {
  372. var prop = new PropertyList()
  373. {
  374. Id = p.Id,
  375. Name = string.IsNullOrEmpty(p.PropertyName) ? p.ShortDescription : p.PropertyName,
  376. Price = string.Format("R {0:n}", p.Price),
  377. Publish = p.Published.ToString(),
  378. Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description
  379. };
  380. prop.Size = (from u in dBContext.PropertyUserFields
  381. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  382. where u.PropertyId == p.Id
  383. && f.FieldName == "Floor Size"
  384. select u.Value).FirstOrDefault();
  385. if (!string.IsNullOrEmpty(prop.Size) && prop.Size.EndsWith("2"))
  386. {
  387. prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
  388. }
  389. list.Add(prop);
  390. }
  391. return list;
  392. }
  393. }
  394. }