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

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