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.

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. foreach (PropertyImage image in images)
  139. {
  140. image.PropertyId = item.Id;
  141. dBContext.PropertyImages.Add(image);
  142. Save();
  143. }
  144. }
  145. if (fields != null)
  146. {
  147. var lastID = (from p in dBContext.PropertyUserFields
  148. orderby p.Id descending
  149. select p.Id).FirstOrDefault();
  150. foreach (PropertyUserField field in fields)
  151. {
  152. lastID++;
  153. field.Id = lastID;
  154. field.PropertyId = item.Id;
  155. dBContext.PropertyUserFields.Add(field);
  156. Save();
  157. }
  158. }
  159. }
  160. public void Insert(IEnumerable<Property> items)
  161. {
  162. foreach (var item in items)
  163. {
  164. dBContext.Properties.Add(item);
  165. }
  166. Save();
  167. }
  168. public void Remove(Property item)
  169. {
  170. dBContext.Properties.Remove(item);
  171. Save();
  172. }
  173. public void Remove(IEnumerable<Property> items)
  174. {
  175. foreach (var item in items)
  176. {
  177. dBContext.Properties.Remove(item);
  178. }
  179. Save();
  180. }
  181. public void RemoveAtId(int item)
  182. {
  183. var property = Get(x => x.Id == item).FirstOrDefault();
  184. if (property != null)
  185. {
  186. dBContext.Properties.Remove(property);
  187. Save();
  188. }
  189. }
  190. public void Save()
  191. {
  192. dBContext.SaveChanges();
  193. }
  194. public void Update(Property item)
  195. {
  196. dBContext.Entry(item).State = EntityState.Modified;
  197. Save();
  198. }
  199. public List<PropertyDisplay> GetDisplay()
  200. {
  201. List<Property> props = GetAll();
  202. return GetDisplayDetails(props);
  203. }
  204. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  205. {
  206. List<Property> props;
  207. if (where != null)
  208. props = Get(where);
  209. else
  210. props = GetAll();
  211. return GetDisplayDetails(props);
  212. }
  213. public List<PropertyDisplay> GetDisplay(PropertySearch search)
  214. {
  215. //Save to Log
  216. dBContext.SearchLogs.Add(new Model.Logging.SearchLog()
  217. {
  218. Type = "Property",
  219. Search = JsonConvert.SerializeObject(search)
  220. });
  221. Save();
  222. if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL")
  223. {
  224. string keyword = search.Keyword.ToLower();
  225. List<Property> props = (from p in dBContext.Properties
  226. join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
  227. join c in dBContext.Cities on p.CityId equals c.Id
  228. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  229. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  230. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
  231. || EF.Functions.Like(pr.Description.ToLower(), $"%{keyword}%")
  232. || EF.Functions.Like(c.Description.ToLower(), $"%{keyword}%")
  233. || EF.Functions.Like(s.Description.ToLower(), $"%{keyword}%")
  234. || EF.Functions.Like(pt.Description.ToLower(), $"%{keyword}%")
  235. select p).ToList();
  236. return GetDisplayDetails(props);
  237. }
  238. else
  239. {
  240. List<Property> props;
  241. PropertyUsageType uType = PropertyUsageType.Both;
  242. if (!string.IsNullOrEmpty(search.PropertyUsageType) && search.PropertyUsageType != "undefined")
  243. {
  244. if (search.PropertyUsageType.ToUpper() == "COMMERCIAL")
  245. uType = PropertyUsageType.Commercial;
  246. else
  247. uType = PropertyUsageType.Residential;
  248. }
  249. props = (from p in dBContext.Properties
  250. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  251. where pt.UsageType == uType
  252. select p).ToList();
  253. if (!string.IsNullOrEmpty(search.SalesType) && search.SalesType != "undefined" && search.SalesType.ToUpper() != "ALL")
  254. {
  255. if (search.SalesType.ToUpper() == "SALE")
  256. props = props.Where(p => p.IsSale).ToList();
  257. else
  258. props = props.Where(p => !p.IsSale).ToList();
  259. }
  260. if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
  261. {
  262. props = (from p in props
  263. join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
  264. where pp.Description.ToUpper() == search.Province.ToUpper()
  265. select p).ToList();
  266. }
  267. if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
  268. {
  269. props = (from p in props
  270. join c in dBContext.Cities on p.CityId equals c.Id
  271. where c.Description.ToUpper() == search.City.ToUpper()
  272. select p).ToList();
  273. }
  274. if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
  275. {
  276. props = (from p in props
  277. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  278. where s.Description.ToUpper() == search.Suburb.ToUpper()
  279. select p).ToList();
  280. }
  281. if (!string.IsNullOrEmpty(search.PropertyType) && search.PropertyType != "Undefined" && search.PropertyType.ToUpper() != "ALL")
  282. {
  283. var pType = dBContext.PropertyTypes.Where(t => t.Description == search.PropertyType).FirstOrDefault();
  284. if (pType != null)
  285. {
  286. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  287. }
  288. }
  289. return GetDisplayDetails(props);
  290. }
  291. }
  292. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  293. {
  294. var properties = new List<PropertyDisplay>();
  295. foreach (var item in props)
  296. {
  297. PropertyDisplay display = new PropertyDisplay
  298. {
  299. Id = item.Id,
  300. ShortDescription = item.ShortDescription,
  301. IsSale = item.IsSale,
  302. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  303. DisplayImage = (from i in dBContext.PropertyImages
  304. where i.PropertyId == item.Id
  305. && i.IsDefault
  306. select i.Image).FirstOrDefault(),
  307. Area = (from u in dBContext.PropertyUserFields
  308. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  309. where u.PropertyId == item.Id
  310. && f.FieldName == "Floor Size"
  311. select u.Value).FirstOrDefault(),
  312. Beds = (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 == "Bedrooms"
  316. select u.Value).FirstOrDefault(),
  317. Baths = (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 == "Bathrooms"
  321. select u.Value).FirstOrDefault(),
  322. Garages = (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 == "Garages"
  326. select u.Value).FirstOrDefault(),
  327. Province = (from p in dBContext.Provinces
  328. where p.Id == item.ProvinceId
  329. select p.Description).FirstOrDefault(),
  330. City = (from c in dBContext.Cities
  331. where c.Id == item.CityId
  332. select c.Description).FirstOrDefault(),
  333. Suburb = (from s in dBContext.Suburbs
  334. where s.Id == item.SuburbId
  335. select s.Description).FirstOrDefault(),
  336. Price = item.Price,
  337. DateCreated = item.Created
  338. };
  339. if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
  340. {
  341. display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
  342. }
  343. properties.Add(display);
  344. }
  345. return properties;
  346. }
  347. public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
  348. {
  349. List<PropertyType> list;
  350. if (where != null)
  351. list = dBContext.PropertyTypes.Where(where).ToList();
  352. else
  353. list = dBContext.PropertyTypes.ToList();
  354. return list;
  355. }
  356. public List<PropertyDisplay> GetLatestDisplay()
  357. {
  358. List<Property> props = GetAll().OrderBy(x => x.Created).Take(3).ToList();
  359. return GetDisplayDetails(props);
  360. }
  361. public List<PropertyList> GetPropertyList(Func<Property, bool> where)
  362. {
  363. var properties = Get(where);
  364. List<PropertyList> list = new List<PropertyList>();
  365. foreach (Property p in properties)
  366. {
  367. var prop = new PropertyList()
  368. {
  369. Id = p.Id,
  370. Name = string.IsNullOrEmpty(p.PropertyName) ? p.ShortDescription : p.PropertyName,
  371. Price = string.Format("R {0:n}", p.Price),
  372. Publish = p.Published.ToString(),
  373. Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description
  374. };
  375. prop.Size = (from u in dBContext.PropertyUserFields
  376. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  377. where u.PropertyId == p.Id
  378. && f.FieldName == "Floor Size"
  379. select u.Value).FirstOrDefault();
  380. if (!string.IsNullOrEmpty(prop.Size) && prop.Size.EndsWith("2"))
  381. {
  382. prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
  383. }
  384. list.Add(prop);
  385. }
  386. return list;
  387. }
  388. public int NewId()
  389. {
  390. // Not sure if properties need it
  391. return 0;
  392. }
  393. }
  394. }