API
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PropertyRepository.cs 22KB

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