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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. #define ReturnImages
  2. //#undef ReturnImages //Comment out to return images
  3. using Microsoft.EntityFrameworkCore;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using UnivateProperties_API.Containers.Property;
  10. using UnivateProperties_API.Containers.Timeshare;
  11. using UnivateProperties_API.Context;
  12. using UnivateProperties_API.Model.Logging;
  13. using UnivateProperties_API.Model.Properties;
  14. namespace UnivateProperties_API.Repository.Properties
  15. {
  16. public class PropertyRepository : IPropertyRepository
  17. {
  18. private readonly DataContext dBContext;
  19. public PropertyRepository(DataContext _dBContext)
  20. {
  21. dBContext = _dBContext;
  22. }
  23. public List<Property> Get(Func<Property, bool> where)
  24. {
  25. return dBContext.Properties
  26. .Include("PropertyType")
  27. .Where(where).ToList();
  28. }
  29. public List<Property> GetAll()
  30. {
  31. var properties = dBContext.Properties.Include("PropertyType").ToList();
  32. return properties;
  33. }
  34. public Property GetDetailed(Func<Property, bool> first)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. public PropertyContainer GetDetailed(int id, bool detailed)
  39. {
  40. //var property = dBContext.Properties.Include("StatusId").Where(p => p.Id == id).FirstOrDefault();
  41. var property = dBContext.Properties.Where(p => p.Id == id).FirstOrDefault();
  42. if (property != null)
  43. {
  44. return GetDetail(property, detailed);
  45. }
  46. else
  47. {
  48. var prop = new PropertyContainer
  49. {
  50. PropertyImages = new List<PropertyImage>(),
  51. PropertyUserFields = new List<PropertyUserField>(),
  52. NewImages = new List<NewImage>(),
  53. PropertyOverviewFields = new List<PropertyFieldGroup>(),
  54. PropertyFields = new List<PropertyFieldGroup>()
  55. };
  56. return prop;
  57. }
  58. }
  59. public List<Property> GetDetailedAll()
  60. {
  61. var properties = dBContext.Properties.ToList();
  62. return properties;
  63. }
  64. private PropertyContainer GetDetail(Property property, bool detailed)
  65. {
  66. int propID = property.Id;
  67. var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
  68. property.DisplayData = new List<PropertyDetailGroup>();
  69. if (detailed)
  70. {
  71. var groups = (from g in dBContext.UserDefinedGroups
  72. where g.UsageType == propertyType.UsageType
  73. || g.UsageType == PropertyUsageType.Both
  74. orderby g.Rank
  75. select g).ToList();
  76. foreach (UserDefinedGroup uGroup in groups)
  77. {
  78. var groupFields = (from f in dBContext.PropertyUserFields
  79. join uf in dBContext.UserDefinedFields on f.UserDefinedFieldId equals uf.Id
  80. join g in dBContext.UserDefinedGroups on uf.GroupId equals g.Id
  81. where f.PropertyId == propID
  82. && g.Id == uGroup.Id
  83. orderby g.Rank, uf.Rank
  84. select new { uf.FieldName, f.Value, f.Description }).ToList();
  85. if (groupFields.Count > 0)
  86. {
  87. PropertyDetailGroup detailGroup = new PropertyDetailGroup()
  88. {
  89. GroupName = uGroup.Description,
  90. Values = new List<PropertyDetail>()
  91. };
  92. if (uGroup.Description == "Property Overview")
  93. {
  94. detailGroup.Values.Add(new PropertyDetail()
  95. {
  96. Name = "Property Type",
  97. Value = property.PropertyType.Description
  98. });
  99. }
  100. foreach (var val in groupFields)
  101. {
  102. if (!string.IsNullOrEmpty(val.Value))
  103. {
  104. var item = new PropertyDetail()
  105. {
  106. Name = val.FieldName,
  107. Description = val.Description,
  108. Value = val.Value
  109. };
  110. detailGroup.Values.Add(item);
  111. }
  112. }
  113. property.DisplayData.Add(detailGroup);
  114. }
  115. }
  116. }
  117. else
  118. {
  119. if (!string.IsNullOrEmpty(property.Video))
  120. property.Video = string.Format("https://www.youtube.com/watch?v={0}", property.Video);
  121. }
  122. var propertyDetails = new PropertyContainer();
  123. propertyDetails.SalesTypeString = property.IsSale ? "Sale" : "Rental";
  124. propertyDetails.OldPrice = property.Price;
  125. foreach (string prop in property.GetAllProperties())
  126. {
  127. if (prop != "Item" && prop != "Display")
  128. propertyDetails[prop] = property[prop];
  129. }
  130. if (property.StatusId != null)
  131. {
  132. propertyDetails.StatusString = dBContext.Status.Where(s => s.Id == property.StatusId).FirstOrDefault()?.Description;
  133. propertyDetails.OldStatus = propertyDetails.StatusString;
  134. }
  135. propertyDetails.PropertyUsageType = propertyType.UsageType == PropertyUsageType.Commercial ? "Commercial" : "Residential";
  136. if (property.OwnerId > 0)
  137. propertyDetails.UserId = (dBContext.Individuals.Where(p => p.Id == property.OwnerId).FirstOrDefault().UserId).Value;
  138. if (property.AgentId > 0)
  139. propertyDetails.UserId = (dBContext.Agents.Where(p => p.Id == property.AgentId).FirstOrDefault().UserId).Value;
  140. propertyDetails.NewImages = new List<NewImage>();
  141. if (property.DateAvailable != DateTime.MinValue)
  142. propertyDetails.DateAvailableString = string.Format("{0:yyyy-MM-dd}", property.DateAvailable);
  143. if (property.CutOffDisplayDate == DateTime.MinValue)
  144. propertyDetails.CutOffDisplayDateString = string.Format("{0:yyyy-MM-dd}", DateTime.Now.AddMonths(1));
  145. else
  146. propertyDetails.CutOffDisplayDateString = string.Format("{0:yyyy-MM-dd}", property.CutOffDisplayDate);
  147. return propertyDetails;
  148. }
  149. public void Insert(Property item)
  150. {
  151. throw new NotImplementedException();
  152. }
  153. public void Insert(IEnumerable<Property> items)
  154. {
  155. foreach (var item in items)
  156. {
  157. dBContext.Properties.Add(item);
  158. }
  159. Save();
  160. }
  161. public void Remove(Property item)
  162. {
  163. item.IsDeleted = true;
  164. Save();
  165. }
  166. public void Remove(IEnumerable<Property> items)
  167. {
  168. foreach (var item in items)
  169. {
  170. item.IsDeleted = true;
  171. }
  172. Save();
  173. }
  174. public void RemoveAtId(int item)
  175. {
  176. var property = Get(x => x.Id == item).FirstOrDefault();
  177. if (property != null)
  178. {
  179. dBContext.Properties.Remove(property);
  180. Save();
  181. }
  182. }
  183. public void Save()
  184. {
  185. dBContext.SaveChanges();
  186. }
  187. public void Update(Property item)
  188. {
  189. if (!string.IsNullOrEmpty(item.Video) && item.Video.StartsWith("http"))
  190. item.Video = item.Video.Replace("https://www.youtube.com/watch?v=", "");
  191. dBContext.Entry(item).State = EntityState.Modified;
  192. Save();
  193. }
  194. public void Update(PropertyContainer item)
  195. {
  196. if (!string.IsNullOrEmpty(item.Video) && item.Video.StartsWith("http"))
  197. item.Video = item.Video.Replace("https://www.youtube.com/watch?v=", "");
  198. var property = new Property();
  199. foreach (string prop in property.GetAllProperties())
  200. {
  201. if (prop != "Item" && prop != "Display")
  202. property[prop] = item[prop];
  203. }
  204. property.PropertyUserFields = null;
  205. property.PropertyImages = null;
  206. if (item.Price < item.OldPrice)
  207. {
  208. property.PriceRedused = true;
  209. }
  210. if (!string.IsNullOrEmpty(item.StatusString))
  211. {
  212. if (item.StatusString != item.OldStatus)
  213. property.StatusDate = DateTime.Now;
  214. property.StatusId = dBContext.Status.Where(s => s.Description == item.StatusString && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
  215. if ((item.StatusString.ToUpper() == "RENTED OUT" || item.StatusString.ToUpper() == "SOLD") && item.StatusString != item.OldStatus)
  216. {
  217. property.CutOffDisplayDate = DateTime.Now.AddMonths(1);
  218. }
  219. else
  220. {
  221. property.CutOffDisplayDate = DateTime.MinValue;
  222. }
  223. if (item.StatusString.ToUpper() == "FOR SALE")
  224. property.IsSale = true;
  225. if (item.StatusString.ToUpper() == "FOR RENT")
  226. property.IsSale = false;
  227. }
  228. if (!string.IsNullOrEmpty(item.DateAvailableString))
  229. {
  230. property.DateAvailable = DateTime.Parse(item.DateAvailableString);
  231. }
  232. dBContext.Entry(property).State = EntityState.Modified;
  233. Save();
  234. #region Insert New UDFs
  235. foreach (var propGroup in item.PropertyFields)
  236. {
  237. foreach (var field in propGroup.Fields)
  238. {
  239. if (field.Value != null)
  240. {
  241. var propField = dBContext.PropertyUserFields.Where(u => u.PropertyId == property.Id && u.UserDefinedFieldId == field.Id).FirstOrDefault();
  242. if (propField == null)
  243. {
  244. propField = new PropertyUserField()
  245. {
  246. PropertyId = property.Id,
  247. UserDefinedFieldId = field.Id,
  248. Value = field.Value
  249. };
  250. dBContext.Add(propField);
  251. Save();
  252. }
  253. else
  254. {
  255. propField.Value = field.Value;
  256. dBContext.Entry(propField).State = EntityState.Modified;
  257. Save();
  258. }
  259. }
  260. }
  261. }
  262. #endregion
  263. #region Update Images
  264. //property.PropertyImages = new List<PropertyImage>();
  265. if (item.PropertyImages != null)
  266. {
  267. foreach (var propImg in item.PropertyImages)
  268. {
  269. var image = dBContext.PropertyImages.Where(pi => pi.Id == propImg.Id).FirstOrDefault();
  270. if (image != null)
  271. {
  272. if (propImg.IsDeleted)
  273. dBContext.PropertyImages.Remove(image);
  274. else
  275. {
  276. image.IsDefault = propImg.IsDefault;
  277. dBContext.Entry(image).State = EntityState.Modified;
  278. Save();
  279. //property.PropertyImages.Add(image);
  280. }
  281. }
  282. }
  283. }
  284. if (item.NewImages != null)
  285. {
  286. bool saveFiles = false;
  287. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  288. if (!string.IsNullOrEmpty(loc))
  289. {
  290. saveFiles = true;
  291. loc += string.Format("\\{0}", property.Id);
  292. if (Directory.Exists(loc))
  293. {
  294. Directory.CreateDirectory(loc);
  295. }
  296. }
  297. var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
  298. foreach (var image in item.NewImages)
  299. {
  300. var propImage = new PropertyImage
  301. {
  302. PropertyId = property.Id,
  303. Image = image.Image,
  304. IsDefault = image.IsDefault
  305. };
  306. if (saveFiles)
  307. {
  308. string path = ImageFormatter.Base64ToImage(propImage.Image, loc, lastID.ToString());
  309. propImage.Image = path;
  310. }
  311. var exists = dBContext.PropertyImages.Where(pi => pi.Image == propImage.Image && pi.PropertyId == property.Id).FirstOrDefault();
  312. if (exists == null)
  313. {
  314. //property.PropertyImages.Add(propImage);
  315. dBContext.PropertyImages.Add(propImage);
  316. Save();
  317. lastID++;
  318. }
  319. }
  320. }
  321. #endregion
  322. Save();
  323. }
  324. public List<PropertyDisplay> GetDisplay()
  325. {
  326. List<Property> props = GetAll();
  327. return GetDisplayDetails(props);
  328. }
  329. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  330. {
  331. List<Property> props;
  332. if (where != null)
  333. props = Get(where);
  334. else
  335. props = GetAll();
  336. return GetDisplayDetails(props);
  337. }
  338. public List<PropertyDisplay> GetDisplay(PropertySearch search)
  339. {
  340. //return GetDisplayDetails(dBContext.Properties.ToList());
  341. //StreamWriter SW = new StreamWriter(@"c:\temp\SearchData.txt", true);
  342. //SW.WriteLine(string.Format("{0:yyyy-MM-dd hh:mm:ss} - {1}", DateTime.Now, JsonConvert.SerializeObject(search)));
  343. //SW.Close();
  344. SearchObject obj = new SearchObject()
  345. {
  346. UserName = search.UserName,
  347. Type = "Property"
  348. };
  349. if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL" && search.Keyword.ToUpper() != "UNDEFINED")
  350. {
  351. string keyword = search.Keyword.ToLower();
  352. List<Property> props = (from p in dBContext.Properties
  353. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  354. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
  355. || EF.Functions.Like(p.Province.ToLower(), $"%{keyword}%")
  356. || EF.Functions.Like(p.City.ToLower(), $"%{keyword}%")
  357. || EF.Functions.Like(p.Suburb.ToLower(), $"%{keyword}%")
  358. || EF.Functions.Like(pt.Description.ToLower(), $"%{keyword}%")
  359. select p).ToList();
  360. obj.Property = "Keyword";
  361. obj.Value = search.Keyword;
  362. SaveLog(obj);
  363. return GetDisplayDetails(props);
  364. }
  365. else
  366. {
  367. List<Property> props;
  368. //Property ID search will override other searches.
  369. if (search.PropertyId > 0)
  370. {
  371. obj.Property = "PropertyID";
  372. obj.Value = search.PropertyId.ToString();
  373. SaveLog(obj);
  374. props = dBContext.Properties.Where(p => p.Id == search.PropertyId).ToList();
  375. return GetDisplayDetails(props);
  376. }
  377. PropertyUsageType uType = PropertyUsageType.Both;
  378. if (!string.IsNullOrEmpty(search.PropertyUsageType) && search.PropertyUsageType != "undefined" && search.PropertyUsageType.ToUpper() != "ALL")
  379. {
  380. if (search.PropertyUsageType.ToUpper() == "COMMERCIAL")
  381. uType = PropertyUsageType.Commercial;
  382. else
  383. uType = PropertyUsageType.Residential;
  384. }
  385. props = (from p in dBContext.Properties
  386. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  387. where pt.UsageType == uType
  388. select p).ToList();
  389. obj.Property = "PropertyUsageType";
  390. obj.Value = search.PropertyUsageType;
  391. SaveLog(obj);
  392. if (!string.IsNullOrEmpty(search.SalesType) && search.SalesType != "undefined" && search.SalesType.ToUpper() != "ALL")
  393. {
  394. if (search.SalesType.ToUpper() == "SALE")
  395. {
  396. props = props.Where(p => p.IsSale).ToList();
  397. search.AvailableFrom = DateTime.MinValue; //Sales do not have an available from date.
  398. }
  399. else
  400. props = props.Where(p => !p.IsSale).ToList();
  401. obj.Property = "SalesType";
  402. obj.Value = search.SalesType;
  403. SaveLog(obj);
  404. }
  405. if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
  406. {
  407. props = (from p in props
  408. where p.Province.ToUpper() == search.Province.ToUpper()
  409. select p).ToList();
  410. obj.Property = "Province";
  411. obj.Value = search.Province;
  412. SaveLog(obj);
  413. }
  414. if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
  415. {
  416. props = (from p in props
  417. where p.City.ToUpper() == search.City.ToUpper()
  418. select p).ToList();
  419. obj.Property = "City";
  420. obj.Value = search.City;
  421. SaveLog(obj);
  422. }
  423. if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
  424. {
  425. props = (from p in props
  426. where p.Suburb.ToUpper() == search.Suburb.ToUpper()
  427. select p).ToList();
  428. obj.Property = "Suburb";
  429. obj.Value = search.Suburb;
  430. SaveLog(obj);
  431. }
  432. if (!string.IsNullOrEmpty(search.PropertyType) && search.PropertyType != "Undefined" && search.PropertyType.ToUpper() != "ALL")
  433. {
  434. var pType = dBContext.PropertyTypes.Where(t => t.Description == search.PropertyType).FirstOrDefault();
  435. if (pType != null)
  436. {
  437. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  438. }
  439. obj.Property = "PropertyType";
  440. obj.Value = search.PropertyType;
  441. SaveLog(obj);
  442. }
  443. if (search.MinPrice > 0)
  444. {
  445. props = props.Where(p => p.Price >= search.MinPrice).ToList();
  446. obj.Property = "MinPrice";
  447. obj.Value = search.MinPrice.ToString();
  448. SaveLog(obj);
  449. }
  450. if (search.MaxPrice > 0)
  451. {
  452. props = props.Where(p => p.Price <= search.MaxPrice).ToList();
  453. obj.Property = "MaxPrice";
  454. obj.Value = search.MaxPrice.ToString();
  455. SaveLog(obj);
  456. }
  457. if (search.AvailableFrom != DateTime.MinValue)
  458. {
  459. props = props.Where(p => p.DateAvailable.Date >= search.AvailableFrom.Date).ToList();
  460. obj.Property = "AvailableFrom";
  461. obj.Value = search.AvailableFrom.ToString();
  462. SaveLog(obj);
  463. }
  464. return GetDisplayDetails(props);
  465. }
  466. }
  467. private void SaveLog(SearchObject item)
  468. {
  469. var searchLog = new SearchLog
  470. {
  471. Type = item.Type,
  472. Search = JsonConvert.SerializeObject(item)
  473. };
  474. dBContext.SearchLogs.Add(searchLog);
  475. Save();
  476. }
  477. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  478. {
  479. var properties = new List<PropertyDisplay>();
  480. props = props.Where(p => p.Published).ToList();
  481. foreach (var item in props)
  482. {
  483. PropertyDisplay display = new PropertyDisplay
  484. {
  485. PropertyReference = item.PropertyRef,
  486. DateAvailable = item.DateAvailable,
  487. Id = item.Id,
  488. ShortDescription = item.ShortDescription,
  489. IsSale = item.IsSale,
  490. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  491. DisplayImage = (from i in dBContext.PropertyImages
  492. where i.PropertyId == item.Id
  493. && i.IsDefault
  494. select i.Image).FirstOrDefault(),
  495. Area = (from u in dBContext.PropertyUserFields
  496. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  497. where u.PropertyId == item.Id
  498. && f.FieldName == "Floor Size"
  499. select u.Value).FirstOrDefault(),
  500. Beds = (from u in dBContext.PropertyUserFields
  501. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  502. where u.PropertyId == item.Id
  503. && f.FieldName == "Bedrooms"
  504. select u.Value).FirstOrDefault(),
  505. Baths = (from u in dBContext.PropertyUserFields
  506. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  507. where u.PropertyId == item.Id
  508. && f.FieldName == "Bathrooms"
  509. select u.Value).FirstOrDefault(),
  510. Garages = (from u in dBContext.PropertyUserFields
  511. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  512. where u.PropertyId == item.Id
  513. && f.FieldName == "Garages"
  514. select u.Value).FirstOrDefault(),
  515. Province = item.Province,
  516. City = item.City,
  517. Suburb = item.Suburb,
  518. Price = item.Price,
  519. DateCreated = item.Created,
  520. PropertyUsageType = (from p in dBContext.PropertyTypes
  521. where p.Id == item.PropertyTypeId
  522. select p.UsageType.ToString()).FirstOrDefault()
  523. };
  524. var status = dBContext.Status.Where(s => s.Id == item.StatusId).FirstOrDefault();
  525. switch (status.Description.ToUpper())
  526. {
  527. case "FOR RENT":
  528. if (item.DateAvailable < DateTime.Now)
  529. {
  530. display.DisplayText = "Available Now";
  531. display.DisplayColor = "green";
  532. }
  533. else
  534. {
  535. display.DisplayText = string.Format("Available From: {0:dd MMM yyyy}", item.DateAvailable);
  536. display.DisplayColor = "blue";
  537. }
  538. break;
  539. case "FOR SALE":
  540. if (item.PriceRedused)
  541. {
  542. display.DisplayText = "Price Redused";
  543. display.DisplayColor = "green";
  544. }
  545. else
  546. {
  547. display.DisplayText = "For Sale";
  548. display.DisplayColor = "blue";
  549. }
  550. break;
  551. case "OFFER PENDING":
  552. display.DisplayText = "Offer Pending";
  553. display.DisplayColor = "orange";
  554. break;
  555. default:
  556. display.DisplayText = status.Description;
  557. display.DisplayColor = "red";
  558. break;
  559. }
  560. if (display.DisplayImage != null && !display.DisplayImage.StartsWith("data:image"))
  561. {
  562. display.DisplayImage = ImageFormatter.ImageToBase64(display.DisplayImage);
  563. }
  564. #if !ReturnImages
  565. display.DisplayImage = "";
  566. #endif
  567. properties.Add(display);
  568. }
  569. return properties;
  570. }
  571. public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
  572. {
  573. List<PropertyType> list;
  574. if (where != null)
  575. list = dBContext.PropertyTypes.Where(where).ToList();
  576. else
  577. list = dBContext.PropertyTypes.ToList();
  578. return list;
  579. }
  580. public List<PropertyDisplay> GetLatestDisplay()
  581. {
  582. List<Property> props = GetAll().Where(x => x.Published && (x.CutOffDisplayDate == DateTime.MinValue || x.CutOffDisplayDate.Date > DateTime.Now.Date)).OrderByDescending(x => x.DatePublished).Take(4).ToList();
  583. return GetDisplayDetails(props);
  584. }
  585. public List<PropertyDisplay> GetLatestDisplay(string PropertyType)
  586. {
  587. var type = PropertyUsageType.Both;
  588. switch (PropertyType.ToUpper())
  589. {
  590. case "RESIDENTIAL":
  591. type = PropertyUsageType.Residential;
  592. break;
  593. case "COMMERCIAL":
  594. type = PropertyUsageType.Commercial;
  595. break;
  596. }
  597. List<Property> props = dBContext.Properties.Include("PropertyType").Where(x => x.Published
  598. && x.PropertyType.UsageType == type
  599. && (x.CutOffDisplayDate == DateTime.MinValue || x.CutOffDisplayDate.Date > DateTime.Now.Date)).OrderByDescending(x => x.DatePublished).Take(4).ToList();
  600. return GetDisplayDetails(props);
  601. }
  602. public List<PropertyList> GetPropertyList(int By)
  603. {
  604. List<Property> properties = new List<Property>();
  605. var user = dBContext.Users.Where(u => u.Id == By).FirstOrDefault();
  606. if (user != null)
  607. {
  608. if (user.Role.ToUpper() == "AGENCY" || user.Role.ToUpper() == "AGENT")
  609. {
  610. var agent = dBContext.Agents.Where(a => a.UserId == user.Id).FirstOrDefault();
  611. if (user.Role.ToUpper() == "AGENCY")
  612. {
  613. properties = dBContext.Properties.Where(p => p.AgencyId == agent.AgencyId).ToList();
  614. }
  615. else
  616. {
  617. properties = dBContext.Properties.Where(p => p.AgentId == agent.Id).ToList();
  618. }
  619. }
  620. if (user.Role.ToUpper() == "PRIVATE USER")
  621. {
  622. var individual = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
  623. properties = dBContext.Properties.Where(p => p.OwnerId == individual.Id).ToList();
  624. }
  625. if (user.Role.ToUpper() == "SUPER ADMIN")
  626. properties = dBContext.Properties.ToList();
  627. }
  628. return SetPropertyList(properties);
  629. }
  630. public List<PropertyList> GetPropertyList()
  631. {
  632. return SetPropertyList(dBContext.Properties.Where(x => x.Published).ToList());
  633. }
  634. private List<PropertyList> SetPropertyList(List<Property> properties)
  635. {
  636. List<PropertyList> list = new List<PropertyList>();
  637. foreach (Property p in properties)
  638. {
  639. var prop = new PropertyList()
  640. {
  641. Id = p.Id,
  642. Name = string.IsNullOrEmpty(p.PropertyName) ? p.ShortDescription : p.PropertyName,
  643. Price = p.Price,
  644. Publish = p.Published ? "Yes" : "No",
  645. Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description,
  646. CarouselDescription = string.Format("{0}, {1} <br/>{2}", p.Suburb, p.City, p.AddressOther),
  647. DateAvailable = p.IsSale ? DateTime.MinValue : p.DateAvailable,
  648. IsPublished = p.Published
  649. };
  650. prop.Size = (from u in dBContext.PropertyUserFields
  651. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  652. where u.PropertyId == p.Id
  653. && f.FieldName == "Floor Size"
  654. select u.Value).FirstOrDefault();
  655. //if (!string.IsNullOrEmpty(prop.Size) && prop.Size.EndsWith("2"))
  656. //{
  657. // prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
  658. //}
  659. prop.UsageType = (dBContext.PropertyTypes.Find(p.PropertyTypeId).UsageType == PropertyUsageType.Residential ? "Residential" : "Commercial");
  660. prop.SaleType = p.IsSale ? "Sale" : "Rental";
  661. list.Add(prop);
  662. }
  663. return list;
  664. }
  665. public int NewId()
  666. {
  667. // Not sure if properties need it
  668. return 0;
  669. }
  670. public void Insert(PropertyContainer items)
  671. {
  672. Property property = new Property();
  673. PropertyType pt = dBContext.PropertyTypes.Find(items.PropertyTypeId);
  674. if (pt != null)
  675. {
  676. if (pt.UsageType == PropertyUsageType.Residential)
  677. {
  678. string type = dBContext.PropertyTypes.Find(items.PropertyTypeId).Description;
  679. if (items.PropertyUserFields.Count > 0)
  680. {
  681. string shortDesc = "{0} {1} {2}";
  682. UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
  683. var udValue = items.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
  684. if (udValue != null)
  685. items.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
  686. else
  687. items.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
  688. }
  689. else
  690. {
  691. items.ShortDescription = type;
  692. }
  693. }
  694. else
  695. {
  696. items.ShortDescription = pt.Description;
  697. }
  698. }
  699. var images = items.PropertyImages;
  700. var fields = items.PropertyUserFields;
  701. items.PropertyImages = null;
  702. items.PropertyUserFields = null;
  703. var individual = dBContext.Individuals.Where(i => i.UserId == items.UserId).FirstOrDefault();
  704. var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();
  705. foreach( string prop in property.GetAllProperties())
  706. {
  707. if (prop != "Item" && prop != "Display")
  708. property[prop] = items[prop];
  709. }
  710. if (individual != null)
  711. property.OwnerId = individual.Id;
  712. if (agent != null)
  713. {
  714. property.AgencyId = agent.AgencyId;
  715. property.AgentId = agent.Id;
  716. }
  717. if (property.IsSale)
  718. property.StatusId = dBContext.Status.Where(s => s.Description == "For Sale" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
  719. else
  720. property.StatusId = dBContext.Status.Where(s => s.Description == "For Rent" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
  721. if (!string.IsNullOrEmpty(property.Video))
  722. property.Video = property.Video.Replace("https://www.youtube.com/watch?v=", "");
  723. if (images != null)
  724. {
  725. bool saveFiles = false;
  726. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  727. if (!string.IsNullOrEmpty(loc))
  728. {
  729. saveFiles = true;
  730. loc += string.Format("\\{0}", property.Id);
  731. if (Directory.Exists(loc))
  732. {
  733. Directory.CreateDirectory(loc);
  734. }
  735. }
  736. property.PropertyImages = new List<PropertyImage>();
  737. var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
  738. foreach (PropertyImage image in images)
  739. {
  740. image.PropertyId = property.Id;
  741. if (saveFiles)
  742. {
  743. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  744. image.Image = path;
  745. }
  746. property.PropertyImages.Add(image);
  747. lastID++;
  748. }
  749. }
  750. if (fields != null)
  751. {
  752. property.PropertyUserFields = new List<PropertyUserField>();
  753. foreach (PropertyUserField field in fields)
  754. {
  755. field.PropertyId = property.Id;
  756. property.PropertyUserFields.Add(field);
  757. }
  758. }
  759. dBContext.Properties.Add(property);
  760. Save();
  761. items.Id = property.Id;
  762. }
  763. public bool MayEdit(int id)
  764. {
  765. var hasBidItems = (from b in dBContext.BidItems
  766. where b.PropertyId == id
  767. && b.StatusId == 2
  768. select b).FirstOrDefault();
  769. return (hasBidItems == null) ? true : false;
  770. }
  771. public void InsertImages(int propertyID, List<PropertyImage> Images)
  772. {
  773. if (Images != null)
  774. {
  775. var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
  776. bool saveFiles = false;
  777. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  778. if (!string.IsNullOrEmpty(loc))
  779. {
  780. saveFiles = true;
  781. loc += string.Format("\\{0}", propertyID);
  782. if (Directory.Exists(loc))
  783. {
  784. Directory.CreateDirectory(loc);
  785. }
  786. }
  787. foreach (PropertyImage image in Images)
  788. {
  789. image.PropertyId = propertyID;
  790. if (saveFiles)
  791. {
  792. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  793. image.Image = path;
  794. }
  795. dBContext.PropertyImages.Add(image);
  796. lastID++;
  797. Save();
  798. }
  799. }
  800. }
  801. public void InsertFields(int propertyID, List<PropertyUserField> Fields)
  802. {
  803. throw new NotImplementedException();
  804. }
  805. public void PublishProperty(int propertyID)
  806. {
  807. var property = dBContext.Properties.Where(p => p.Id == propertyID).FirstOrDefault();
  808. if (property != null)
  809. {
  810. property.Published = true;
  811. property.DatePublished = DateTime.Now;
  812. Update(property);
  813. }
  814. }
  815. public void UnpublishProperty(int propertyID)
  816. {
  817. var property = dBContext.Properties.Where(p => p.Id == propertyID).FirstOrDefault();
  818. if (property != null)
  819. {
  820. property.Published = false;
  821. property.DatePublished = DateTime.MinValue;
  822. Update(property);
  823. }
  824. }
  825. public List<PropertyAdminContainer> GetAdminProperties(int UserId)
  826. {
  827. var user = dBContext.Users.Where(u => u.Id == UserId).FirstOrDefault();
  828. List<PropertyAdminContainer> returnProps = new List<PropertyAdminContainer>();
  829. List<Property> props;
  830. if ((user.Role.ToUpper() == "SUPER ADMIN"))
  831. {
  832. props = dBContext.Properties.Include("Owner").Include("PropertyType").ToList();
  833. }
  834. else if (user.Role.ToUpper() == "AGENCY")
  835. {
  836. var agent = dBContext.Agents.Where(x => x.UserId == user.Id).FirstOrDefault();
  837. props = dBContext.Properties.Include("Owner").Include("PropertyType").Where(x => x.AgencyId == agent.AgencyId).ToList();
  838. }
  839. else
  840. {
  841. var indiv = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
  842. props = dBContext.Properties.Include("Owner").Include("PropertyType").Where(p => p.OwnerId == indiv.Id).ToList();
  843. }
  844. foreach (var prop in props)
  845. {
  846. var propAdmin = new PropertyAdminContainer()
  847. {
  848. Id = prop.Id,
  849. Owner = prop.Owner?.FullName,
  850. Property = prop.PropertyName,
  851. Reference = prop.PropertyRef,
  852. Unit = prop.Unit,
  853. Size = (from u in dBContext.PropertyUserFields
  854. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  855. where u.PropertyId == prop.Id
  856. && f.FieldName == "Floor Size"
  857. select u.Value).FirstOrDefault(),
  858. Price = prop.Price,
  859. Region = prop.Province,
  860. Town = prop.City,
  861. Suburb = prop.Suburb,
  862. IsPublished = prop.Published,
  863. Type = prop.PropertyType.UsageType.ToString()
  864. };
  865. if (prop.StatusId != null)
  866. {
  867. propAdmin.Status = dBContext.Status.Where(s => s.Id == prop.StatusId).FirstOrDefault()?.Description;
  868. }
  869. returnProps.Add(propAdmin);
  870. }
  871. return returnProps;
  872. }
  873. public List<string> GetStatuses()
  874. {
  875. return dBContext.Status.Where(s => s.StatusType == StatusType.Property).Select(s => s.Description).ToList();
  876. }
  877. }
  878. }