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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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
  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.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. if (item.PropertyUserFields.Count > 0)
  109. {
  110. string shortDesc = "{0} {1} {2}";
  111. string type = dBContext.PropertyTypes.Find(item.PropertyTypeId).Description;
  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. }
  120. else
  121. {
  122. item.ShortDescription = pt.Description;
  123. }
  124. }
  125. dBContext.Properties.Add(item);
  126. Save();
  127. }
  128. public void Insert(IEnumerable<Property> items)
  129. {
  130. foreach (var item in items)
  131. {
  132. dBContext.Properties.Add(item);
  133. }
  134. Save();
  135. }
  136. public void Remove(Property item)
  137. {
  138. dBContext.Properties.Remove(item);
  139. Save();
  140. }
  141. public void Remove(IEnumerable<Property> items)
  142. {
  143. foreach (var item in items)
  144. {
  145. dBContext.Properties.Remove(item);
  146. }
  147. Save();
  148. }
  149. public void RemoveAtId(int item)
  150. {
  151. var property = Get(x => x.Id == item).FirstOrDefault();
  152. if (property != null)
  153. {
  154. dBContext.Properties.Remove(property);
  155. Save();
  156. }
  157. }
  158. public void Save()
  159. {
  160. dBContext.SaveChanges();
  161. }
  162. public void Update(Property item)
  163. {
  164. dBContext.Entry(item).State = EntityState.Modified;
  165. Save();
  166. }
  167. public List<PropertyDisplay> GetDisplay()
  168. {
  169. List<Property> props = GetAll();
  170. return GetDisplayDetails(props);
  171. }
  172. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  173. {
  174. List<Property> props;
  175. if (where != null)
  176. props = Get(where);
  177. else
  178. props = GetAll();
  179. return GetDisplayDetails(props);
  180. }
  181. public List<PropertyDisplay> GetDisplay(string Keyword)
  182. {
  183. Keyword = Keyword.ToLower();
  184. List<Property> props = (from p in dBContext.Properties
  185. join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
  186. join c in dBContext.Cities on p.CityId equals c.Id
  187. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  188. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  189. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{Keyword}%")
  190. || EF.Functions.Like(pr.Description.ToLower(), $"%{Keyword}%")
  191. || EF.Functions.Like(c.Description.ToLower(), $"%{Keyword}%")
  192. || EF.Functions.Like(s.Description.ToLower(), $"%{Keyword}%")
  193. || EF.Functions.Like(pt.Description.ToLower(), $"%{Keyword}%")
  194. select p).ToList();
  195. return GetDisplayDetails(props);
  196. }
  197. public List<PropertyDisplay> GetDisplay(string type, string propertyType, string province, string city, string suburb, string propType)
  198. {
  199. List<Property> props;
  200. PropertyUsageType uType = PropertyUsageType.Both;
  201. if (propertyType != "" && propertyType != "undefined")
  202. {
  203. if (propertyType.ToUpper() == "COMMERCIAL")
  204. uType = PropertyUsageType.Commercial;
  205. else
  206. uType = PropertyUsageType.Residential;
  207. }
  208. props = (from p in dBContext.Properties
  209. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  210. where pt.UsageType == uType
  211. select p).ToList();
  212. if (type != "" && type != "undefined")
  213. {
  214. if (type.ToUpper() == "SALE")
  215. props = props.Where(p => p.IsSale).ToList();
  216. else
  217. props = props.Where(p => !p.IsSale).ToList();
  218. }
  219. if (province != "" && province != "undefined" && province.ToUpper() != "ALL")
  220. {
  221. props = (from p in props
  222. join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
  223. where pp.Description.ToUpper() == province.ToUpper()
  224. select p).ToList();
  225. }
  226. if (city != "" && city != "undefined" && city.ToUpper() != "ALL")
  227. {
  228. props = (from p in props
  229. join c in dBContext.Cities on p.CityId equals c.Id
  230. where c.Description.ToUpper() == city.ToUpper()
  231. select p).ToList();
  232. }
  233. if (suburb != "" && suburb != "undefined" && suburb.ToUpper() != "ALL")
  234. {
  235. props = (from p in props
  236. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  237. where s.Description.ToUpper() == suburb.ToUpper()
  238. select p).ToList();
  239. }
  240. if (propType != "" && propType != "Undefined" && propType.ToUpper() != "ALL")
  241. {
  242. var pType = dBContext.PropertyTypes.Where(t => t.Description == propType).FirstOrDefault();
  243. if (pType != null)
  244. {
  245. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  246. }
  247. }
  248. return GetDisplayDetails(props);
  249. }
  250. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  251. {
  252. var properties = new List<PropertyDisplay>();
  253. foreach (var item in props)
  254. {
  255. PropertyDisplay display = new PropertyDisplay
  256. {
  257. Id = item.Id,
  258. ShortDescription = item.ShortDescription,
  259. IsSale = item.IsSale,
  260. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  261. DisplayImage = (from i in dBContext.PropertyImages
  262. where i.PropertyId == item.Id
  263. && i.IsDefault
  264. select i.Image).FirstOrDefault(),
  265. Area = (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 == "Floor Size"
  269. select u.Value).FirstOrDefault(),
  270. Beds = (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 == "Bedrooms"
  274. select u.Value).FirstOrDefault(),
  275. Baths = (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 == "Bathrooms"
  279. select u.Value).FirstOrDefault(),
  280. Garages = (from u in dBContext.PropertyUserFields
  281. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  282. where u.PropertyId == item.Id
  283. && f.FieldName == "Garages"
  284. select u.Value).FirstOrDefault(),
  285. Province = (from p in dBContext.Provinces
  286. where p.Id == item.ProvinceId
  287. select p.Description).FirstOrDefault(),
  288. City = (from c in dBContext.Cities
  289. where c.Id == item.CityId
  290. select c.Description).FirstOrDefault(),
  291. Suburb = (from s in dBContext.Suburbs
  292. where s.Id == item.SuburbId
  293. select s.Description).FirstOrDefault()
  294. };
  295. if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
  296. {
  297. display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
  298. }
  299. properties.Add(display);
  300. }
  301. return properties;
  302. }
  303. public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
  304. {
  305. List<PropertyType> list;
  306. if (where != null)
  307. list = dBContext.PropertyTypes.Where(where).ToList();
  308. else
  309. list = dBContext.PropertyTypes.ToList();
  310. return list;
  311. }
  312. public List<PropertyDisplay> GetLatestDisplay()
  313. {
  314. List<Property> props = GetAll().OrderBy(x => x.Created).Take(3).ToList();
  315. return GetDisplayDetails(props);
  316. }
  317. public PropertyEditDetails GetEditDisplay(Func<Property, bool> where)
  318. {
  319. var property = dBContext.Properties.Include("PropertyType").Where(where).FirstOrDefault();
  320. PropertyEditDetails details = new PropertyEditDetails();
  321. if (property != null)
  322. {
  323. details.SaleType = property.IsSale ? "Sale" : "Rental";
  324. details.PropertyType = property.PropertyType.UsageType.ToString();
  325. }
  326. return details;
  327. }
  328. }
  329. }