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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.Property;
  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.Where(where).ToList();
  20. }
  21. public List<Property> GetAll()
  22. {
  23. var properties = dBContext.Properties.ToList();
  24. return properties;
  25. }
  26. public Property GetDetailed(Func<Property, bool> first)
  27. {
  28. var property = dBContext.Properties.FirstOrDefault(first);
  29. if (property != null)
  30. {
  31. GetDetail(ref property);
  32. }
  33. return property;
  34. }
  35. public List<Property> GetDetailedAll()
  36. {
  37. var properties = dBContext.Properties.ToList();
  38. return properties;
  39. }
  40. private void GetDetail(ref Property property)
  41. {
  42. int propID = property.Id;
  43. var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
  44. property.Province = dBContext.Provinces.Find(property.ProvinceId);
  45. property.City = dBContext.Cities.Find(property.CityId);
  46. property.Suburb = dBContext.Suburbs.Find(property.SuburbId);
  47. property.DisplayData = new List<PropertyDetailGroup>();
  48. var groups = (from g in dBContext.UserDefinedGroups
  49. where g.UsageType == propertyType.UsageType
  50. || g.UsageType == PropertyUsageType.Both
  51. orderby g.Rank
  52. select g).ToList();
  53. foreach (UserDefinedGroup uGroup in groups)
  54. {
  55. var groupFields = (from f in dBContext.PropertyUserFields
  56. join uf in dBContext.UserDefinedFields on f.UserDefinedFieldId equals uf.Id
  57. join g in dBContext.UserDefinedGroups on uf.GroupId equals g.Id
  58. where f.PropertyId == propID
  59. && g.Id == uGroup.Id
  60. orderby g.Rank, uf.Rank
  61. select new { uf.FieldName, f.Value, f.Description }).ToList();
  62. if (groupFields.Count > 0)
  63. {
  64. PropertyDetailGroup detailGroup = new PropertyDetailGroup()
  65. {
  66. GroupName = uGroup.Description,
  67. Values = new List<PropertyDetail>()
  68. };
  69. if (uGroup.Description == "Property Overview")
  70. {
  71. detailGroup.Values.Add(new PropertyDetail()
  72. {
  73. Name = "Property Type",
  74. Value = property.PropertyType.Description
  75. });
  76. }
  77. foreach (var val in groupFields)
  78. {
  79. var irem = new PropertyDetail()
  80. {
  81. Name = val.FieldName,
  82. Description = val.Description
  83. };
  84. detailGroup.Values.Add(irem);
  85. if ((val.FieldName == "Erf Size" || val.FieldName == "Floor Size") && val.Value.EndsWith("2"))
  86. {
  87. irem.Value = val.Value.Substring(0, val.Value.Length - 1) + "<sup>" + val.Value.Last() + "</sup>";
  88. }
  89. else
  90. irem.Value = val.Value;
  91. }
  92. property.DisplayData.Add(detailGroup);
  93. }
  94. }
  95. }
  96. public void Insert(Property item)
  97. {
  98. PropertyType pt = dBContext.PropertyTypes.Find(item.PropertyTypeId);
  99. if (pt != null)
  100. {
  101. if (pt.UsageType == PropertyUsageType.Residential)
  102. {
  103. if (item.PropertyUserFields.Count > 0)
  104. {
  105. string shortDesc = "{0} {1} {2}";
  106. string type = dBContext.PropertyTypes.Find(item.PropertyTypeId).Description;
  107. UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
  108. var udValue = item.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
  109. if (udValue != null)
  110. item.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
  111. else
  112. item.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
  113. }
  114. }
  115. else
  116. {
  117. item.ShortDescription = pt.Description;
  118. }
  119. }
  120. dBContext.Properties.Add(item);
  121. Save();
  122. }
  123. public void Insert(IEnumerable<Property> items)
  124. {
  125. foreach (var item in items)
  126. {
  127. dBContext.Properties.Add(item);
  128. }
  129. Save();
  130. }
  131. public void Remove(Property item)
  132. {
  133. dBContext.Properties.Remove(item);
  134. Save();
  135. }
  136. public void Remove(IEnumerable<Property> items)
  137. {
  138. foreach (var item in items)
  139. {
  140. dBContext.Properties.Remove(item);
  141. }
  142. Save();
  143. }
  144. public void RemoveAtId(int item)
  145. {
  146. var property = Get(x => x.Id == item).FirstOrDefault();
  147. if (property != null)
  148. {
  149. dBContext.Properties.Remove(property);
  150. Save();
  151. }
  152. }
  153. public void Save()
  154. {
  155. dBContext.SaveChanges();
  156. }
  157. public void Update(Property item)
  158. {
  159. dBContext.Entry(item).State = EntityState.Modified;
  160. Save();
  161. }
  162. public List<PropertyDisplay> GetDisplay()
  163. {
  164. List<Property> props = GetAll();
  165. return GetDisplayDetails(props);
  166. }
  167. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  168. {
  169. List<Property> props;
  170. if (where != null)
  171. props = Get(where);
  172. else
  173. props = GetAll();
  174. return GetDisplayDetails(props);
  175. }
  176. public List<PropertyDisplay> GetDisplay(string Keyword)
  177. {
  178. Keyword = Keyword.ToLower();
  179. List<Property> props = (from p in dBContext.Properties
  180. join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
  181. join c in dBContext.Cities on p.CityId equals c.Id
  182. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  183. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  184. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{Keyword}%")
  185. || EF.Functions.Like(pr.Description.ToLower(), $"%{Keyword}%")
  186. || EF.Functions.Like(c.Description.ToLower(), $"%{Keyword}%")
  187. || EF.Functions.Like(s.Description.ToLower(), $"%{Keyword}%")
  188. || EF.Functions.Like(pt.Description.ToLower(), $"%{Keyword}%")
  189. select p).ToList();
  190. return GetDisplayDetails(props);
  191. }
  192. public List<PropertyDisplay> GetDisplay(string type, string propertyType, string province, string city, string suburb, string propType)
  193. {
  194. List<Property> props;
  195. PropertyUsageType uType = PropertyUsageType.Both;
  196. if (propertyType != "" && propertyType != "undefined")
  197. {
  198. if (propertyType.ToUpper() == "COMMERCIAL")
  199. uType = PropertyUsageType.Commercial;
  200. else
  201. uType = PropertyUsageType.Residential;
  202. }
  203. props = (from p in dBContext.Properties
  204. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  205. where pt.UsageType == uType
  206. select p).ToList();
  207. if (type != "" && type != "undefined")
  208. {
  209. if (type.ToUpper() == "SALE")
  210. props = props.Where(p => p.IsSale).ToList();
  211. else
  212. props = props.Where(p => !p.IsSale).ToList();
  213. }
  214. if (province != "" && province != "undefined" && province.ToUpper() != "ALL")
  215. {
  216. props = (from p in props
  217. join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
  218. where pp.Description.ToUpper() == province.ToUpper()
  219. select p).ToList();
  220. }
  221. if (city != "" && city != "undefined" && city.ToUpper() != "ALL")
  222. {
  223. props = (from p in props
  224. join c in dBContext.Cities on p.CityId equals c.Id
  225. where c.Description.ToUpper() == city.ToUpper()
  226. select p).ToList();
  227. }
  228. if (suburb != "" && suburb != "undefined" && suburb.ToUpper() != "ALL")
  229. {
  230. props = (from p in props
  231. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  232. where s.Description.ToUpper() == suburb.ToUpper()
  233. select p).ToList();
  234. }
  235. if (propType != "" && propType != "Undefined" && propType.ToUpper() != "ALL")
  236. {
  237. var pType = dBContext.PropertyTypes.Where(t => t.Description == propType).FirstOrDefault();
  238. if (pType != null)
  239. {
  240. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  241. }
  242. }
  243. return GetDisplayDetails(props);
  244. }
  245. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  246. {
  247. var properties = new List<PropertyDisplay>();
  248. foreach (var item in props)
  249. {
  250. PropertyDisplay display = new PropertyDisplay
  251. {
  252. Id = item.Id,
  253. ShortDescription = item.ShortDescription,
  254. IsSale = item.IsSale,
  255. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  256. DisplayImage = (from i in dBContext.PropertyImages
  257. where i.PropertyId == item.Id
  258. && i.IsDefault
  259. select i.Image).FirstOrDefault(),
  260. Area = (from u in dBContext.PropertyUserFields
  261. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  262. where u.PropertyId == item.Id
  263. && f.FieldName == "Floor Size"
  264. select u.Value).FirstOrDefault(),
  265. Beds = (from u in dBContext.PropertyUserFields
  266. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  267. where u.PropertyId == item.Id
  268. && f.FieldName == "Bedrooms"
  269. select u.Value).FirstOrDefault(),
  270. Baths = (from u in dBContext.PropertyUserFields
  271. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  272. where u.PropertyId == item.Id
  273. && f.FieldName == "Bathrooms"
  274. select u.Value).FirstOrDefault(),
  275. Garages = (from u in dBContext.PropertyUserFields
  276. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  277. where u.PropertyId == item.Id
  278. && f.FieldName == "Garages"
  279. select u.Value).FirstOrDefault(),
  280. Province = (from p in dBContext.Provinces
  281. where p.Id == item.ProvinceId
  282. select p.Description).FirstOrDefault(),
  283. City = (from c in dBContext.Cities
  284. where c.Id == item.CityId
  285. select c.Description).FirstOrDefault(),
  286. Suburb = (from s in dBContext.Suburbs
  287. where s.Id == item.SuburbId
  288. select s.Description).FirstOrDefault()
  289. };
  290. if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
  291. {
  292. display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
  293. }
  294. properties.Add(display);
  295. }
  296. return properties;
  297. }
  298. }
  299. }