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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. if (item.Video == "")
  199. {
  200. item.Video = null;
  201. }
  202. if (item.VirtualTour == "")
  203. {
  204. item.VirtualTour = null;
  205. }
  206. var property = new Property();
  207. foreach (string prop in property.GetAllProperties())
  208. {
  209. if (prop != "Item" && prop != "Display")
  210. property[prop] = item[prop];
  211. }
  212. property.PropertyUserFields = null;
  213. property.PropertyImages = null;
  214. if (item.Price < item.OldPrice)
  215. {
  216. property.PriceRedused = true;
  217. }
  218. if (!string.IsNullOrEmpty(item.StatusString))
  219. {
  220. if (item.StatusString != item.OldStatus)
  221. property.StatusDate = DateTime.Now;
  222. property.StatusId = dBContext.Status.Where(s => s.Description == item.StatusString && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
  223. if ((item.StatusString.ToUpper() == "RENTED OUT" || item.StatusString.ToUpper() == "SOLD") && item.StatusString != item.OldStatus)
  224. {
  225. property.CutOffDisplayDate = DateTime.Now.AddMonths(1);
  226. }
  227. else
  228. {
  229. property.CutOffDisplayDate = DateTime.MinValue;
  230. }
  231. if (item.StatusString.ToUpper() == "FOR SALE")
  232. property.IsSale = true;
  233. if (item.StatusString.ToUpper() == "FOR RENT")
  234. property.IsSale = false;
  235. }
  236. if (!string.IsNullOrEmpty(item.DateAvailableString))
  237. {
  238. property.DateAvailable = DateTime.Parse(item.DateAvailableString);
  239. }
  240. dBContext.Entry(property).State = EntityState.Modified;
  241. Save();
  242. #region Insert New UDFs
  243. foreach (var propGroup in item.PropertyFields)
  244. {
  245. foreach (var field in propGroup.Fields)
  246. {
  247. if (field.Value != null)
  248. {
  249. var propField = dBContext.PropertyUserFields.Where(u => u.PropertyId == property.Id && u.UserDefinedFieldId == field.Id).FirstOrDefault();
  250. if (propField == null)
  251. {
  252. propField = new PropertyUserField()
  253. {
  254. PropertyId = property.Id,
  255. UserDefinedFieldId = field.Id,
  256. Value = field.Value
  257. };
  258. dBContext.Add(propField);
  259. Save();
  260. }
  261. else
  262. {
  263. propField.Value = field.Value;
  264. dBContext.Entry(propField).State = EntityState.Modified;
  265. Save();
  266. }
  267. }
  268. }
  269. }
  270. #endregion
  271. #region Update Images
  272. //property.PropertyImages = new List<PropertyImage>();
  273. if (item.PropertyImages != null)
  274. {
  275. foreach (var propImg in item.PropertyImages)
  276. {
  277. var image = dBContext.PropertyImages.Where(pi => pi.Id == propImg.Id).FirstOrDefault();
  278. if (image != null)
  279. {
  280. if (propImg.IsDeleted)
  281. dBContext.PropertyImages.Remove(image);
  282. else
  283. {
  284. image.IsDefault = propImg.IsDefault;
  285. dBContext.Entry(image).State = EntityState.Modified;
  286. Save();
  287. //property.PropertyImages.Add(image);
  288. }
  289. }
  290. }
  291. }
  292. if (item.NewImages != null)
  293. {
  294. bool saveFiles = false;
  295. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  296. if (!string.IsNullOrEmpty(loc))
  297. {
  298. saveFiles = true;
  299. if (loc.EndsWith("\\"))
  300. {
  301. loc = loc.Substring(0, loc.Length - 1);
  302. }
  303. loc += string.Format("\\{0}", property.Id);
  304. if (!Directory.Exists(loc))
  305. {
  306. Directory.CreateDirectory(loc);
  307. }
  308. }
  309. var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
  310. foreach (var image in item.NewImages)
  311. {
  312. if (!image.Image.StartsWith("data:image"))
  313. continue;
  314. var propImage = new PropertyImage
  315. {
  316. PropertyId = property.Id,
  317. Image = image.Image,
  318. IsDefault = image.IsDefault
  319. };
  320. if (saveFiles)
  321. {
  322. string path = ImageFormatter.Base64ToImage(propImage.Image, loc, lastID.ToString());
  323. propImage.Image = path;
  324. }
  325. var exists = dBContext.PropertyImages.Where(pi => pi.Image == propImage.Image && pi.PropertyId == property.Id).FirstOrDefault();
  326. if (exists == null)
  327. {
  328. //property.PropertyImages.Add(propImage);
  329. dBContext.PropertyImages.Add(propImage);
  330. Save();
  331. lastID++;
  332. }
  333. }
  334. }
  335. #endregion
  336. Save();
  337. }
  338. public List<PropertyDisplay> GetDisplay()
  339. {
  340. List<Property> props = GetAll();
  341. return GetDisplayDetails(props);
  342. }
  343. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  344. {
  345. List<Property> props;
  346. if (where != null)
  347. props = Get(where);
  348. else
  349. props = GetAll();
  350. return GetDisplayDetails(props);
  351. }
  352. public List<PropertyDisplay> GetDisplay(PropertySearch search)
  353. {
  354. //return GetDisplayDetails(dBContext.Properties.ToList());
  355. //StreamWriter SW = new StreamWriter(@"c:\temp\SearchData.txt", true);
  356. //SW.WriteLine(string.Format("{0:yyyy-MM-dd hh:mm:ss} - {1}", DateTime.Now, JsonConvert.SerializeObject(search)));
  357. //SW.Close();
  358. SearchObject obj = new SearchObject()
  359. {
  360. UserName = search.UserName,
  361. Type = "Property"
  362. };
  363. if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL" && search.Keyword.ToUpper() != "UNDEFINED")
  364. {
  365. string keyword = search.Keyword.ToLower();
  366. List<Property> props = (from p in dBContext.Properties
  367. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  368. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
  369. || EF.Functions.Like(p.Province.ToLower(), $"%{keyword}%")
  370. || EF.Functions.Like(p.City.ToLower(), $"%{keyword}%")
  371. || EF.Functions.Like(p.Suburb.ToLower(), $"%{keyword}%")
  372. || EF.Functions.Like(pt.Description.ToLower(), $"%{keyword}%")
  373. select p).ToList();
  374. obj.Property = "Keyword";
  375. obj.Value = search.Keyword;
  376. SaveLog(obj);
  377. return GetDisplayDetails(props);
  378. }
  379. else
  380. {
  381. List<Property> props;
  382. //Property ID search will override other searches.
  383. if (search.PropertyId > 0)
  384. {
  385. obj.Property = "PropertyID";
  386. obj.Value = search.PropertyId.ToString();
  387. SaveLog(obj);
  388. props = dBContext.Properties.Where(p => p.Id == search.PropertyId).ToList();
  389. return GetDisplayDetails(props);
  390. }
  391. PropertyUsageType uType = PropertyUsageType.Both;
  392. if (!string.IsNullOrEmpty(search.PropertyUsageType) && search.PropertyUsageType != "undefined" && search.PropertyUsageType.ToUpper() != "ALL")
  393. {
  394. if (search.PropertyUsageType.ToUpper() == "COMMERCIAL")
  395. uType = PropertyUsageType.Commercial;
  396. else
  397. uType = PropertyUsageType.Residential;
  398. }
  399. props = (from p in dBContext.Properties
  400. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  401. where pt.UsageType == uType
  402. select p).ToList();
  403. obj.Property = "PropertyUsageType";
  404. obj.Value = search.PropertyUsageType;
  405. SaveLog(obj);
  406. if (!string.IsNullOrEmpty(search.SalesType) && search.SalesType != "undefined" && search.SalesType.ToUpper() != "ALL")
  407. {
  408. if (search.SalesType.ToUpper() == "SALE")
  409. {
  410. props = props.Where(p => p.IsSale).ToList();
  411. search.AvailableFrom = DateTime.MinValue; //Sales do not have an available from date.
  412. }
  413. else
  414. props = props.Where(p => !p.IsSale).ToList();
  415. obj.Property = "SalesType";
  416. obj.Value = search.SalesType;
  417. SaveLog(obj);
  418. }
  419. if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
  420. {
  421. props = (from p in props
  422. where p.Province.ToUpper() == search.Province.ToUpper()
  423. select p).ToList();
  424. obj.Property = "Province";
  425. obj.Value = search.Province;
  426. SaveLog(obj);
  427. }
  428. if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
  429. {
  430. props = (from p in props
  431. where p.City.ToUpper() == search.City.ToUpper()
  432. select p).ToList();
  433. obj.Property = "City";
  434. obj.Value = search.City;
  435. SaveLog(obj);
  436. }
  437. if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
  438. {
  439. props = (from p in props
  440. where p.Suburb.ToUpper() == search.Suburb.ToUpper()
  441. select p).ToList();
  442. obj.Property = "Suburb";
  443. obj.Value = search.Suburb;
  444. SaveLog(obj);
  445. }
  446. if (!string.IsNullOrEmpty(search.PropertyType) && search.PropertyType != "Undefined" && search.PropertyType.ToUpper() != "ALL")
  447. {
  448. var pType = dBContext.PropertyTypes.Where(t => t.Description == search.PropertyType).FirstOrDefault();
  449. if (pType != null)
  450. {
  451. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  452. }
  453. obj.Property = "PropertyType";
  454. obj.Value = search.PropertyType;
  455. SaveLog(obj);
  456. }
  457. if (search.MinPrice > 0)
  458. {
  459. props = props.Where(p => p.Price >= search.MinPrice).ToList();
  460. obj.Property = "MinPrice";
  461. obj.Value = search.MinPrice.ToString();
  462. SaveLog(obj);
  463. }
  464. if (search.MaxPrice > 0)
  465. {
  466. props = props.Where(p => p.Price <= search.MaxPrice).ToList();
  467. obj.Property = "MaxPrice";
  468. obj.Value = search.MaxPrice.ToString();
  469. SaveLog(obj);
  470. }
  471. if (search.AvailableFrom != DateTime.MinValue)
  472. {
  473. props = props.Where(p => p.DateAvailable.Date >= search.AvailableFrom.Date).ToList();
  474. obj.Property = "AvailableFrom";
  475. obj.Value = search.AvailableFrom.ToString();
  476. SaveLog(obj);
  477. }
  478. return GetDisplayDetails(props);
  479. }
  480. }
  481. private void SaveLog(SearchObject item)
  482. {
  483. var searchLog = new SearchLog
  484. {
  485. Type = item.Type,
  486. Search = JsonConvert.SerializeObject(item)
  487. };
  488. dBContext.SearchLogs.Add(searchLog);
  489. Save();
  490. }
  491. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  492. {
  493. var properties = new List<PropertyDisplay>();
  494. props = props.Where(p => p.Published).ToList();
  495. foreach (var item in props)
  496. {
  497. PropertyDisplay display = new PropertyDisplay
  498. {
  499. PropertyReference = item.PropertyRef,
  500. DateAvailable = item.DateAvailable,
  501. Id = item.Id,
  502. ShortDescription = item.ShortDescription,
  503. IsSale = item.IsSale,
  504. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  505. DisplayImage = (from i in dBContext.PropertyImages
  506. where i.PropertyId == item.Id
  507. && i.IsDefault
  508. select i.Image).FirstOrDefault(),
  509. Area = (from u in dBContext.PropertyUserFields
  510. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  511. where u.PropertyId == item.Id
  512. && f.FieldName == "Floor Size"
  513. select u.Value).FirstOrDefault(),
  514. Beds = (from u in dBContext.PropertyUserFields
  515. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  516. where u.PropertyId == item.Id
  517. && f.FieldName == "Bedrooms"
  518. select u.Value).FirstOrDefault(),
  519. Baths = (from u in dBContext.PropertyUserFields
  520. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  521. where u.PropertyId == item.Id
  522. && f.FieldName == "Bathrooms"
  523. select u.Value).FirstOrDefault(),
  524. Garages = (from u in dBContext.PropertyUserFields
  525. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  526. where u.PropertyId == item.Id
  527. && f.FieldName == "Garages"
  528. select u.Value).FirstOrDefault(),
  529. Province = item.Province,
  530. City = item.City,
  531. Suburb = item.Suburb,
  532. Price = item.Price,
  533. DateCreated = item.Created,
  534. PropertyUsageType = (from p in dBContext.PropertyTypes
  535. where p.Id == item.PropertyTypeId
  536. select p.UsageType.ToString()).FirstOrDefault()
  537. };
  538. var status = dBContext.Status.Where(s => s.Id == item.StatusId).FirstOrDefault();
  539. switch (status.Description.ToUpper())
  540. {
  541. case "FOR RENT":
  542. if (item.DateAvailable < DateTime.Now)
  543. {
  544. display.DisplayText = "Available Now";
  545. display.DisplayColor = "green";
  546. }
  547. else
  548. {
  549. display.DisplayText = string.Format("Available From: {0:dd MMM yyyy}", item.DateAvailable);
  550. display.DisplayColor = "blue";
  551. }
  552. break;
  553. case "FOR SALE":
  554. if (item.PriceRedused)
  555. {
  556. display.DisplayText = "Price Reduced";
  557. display.DisplayColor = "green";
  558. }
  559. else
  560. {
  561. display.DisplayText = "For Sale";
  562. display.DisplayColor = "blue";
  563. }
  564. break;
  565. case "OFFER PENDING":
  566. display.DisplayText = "Offer Pending";
  567. display.DisplayColor = "orange";
  568. break;
  569. default:
  570. display.DisplayText = status.Description;
  571. display.DisplayColor = "red";
  572. break;
  573. }
  574. if (display.DisplayImage != null && !display.DisplayImage.StartsWith("data:image"))
  575. {
  576. display.DisplayImage = ImageFormatter.ImageToBase64(display.DisplayImage);
  577. }
  578. #if !ReturnImages
  579. display.DisplayImage = "";
  580. #endif
  581. properties.Add(display);
  582. }
  583. return properties;
  584. }
  585. public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
  586. {
  587. List<PropertyType> list;
  588. if (where != null)
  589. list = dBContext.PropertyTypes.Where(where).ToList();
  590. else
  591. list = dBContext.PropertyTypes.ToList();
  592. return list;
  593. }
  594. public List<PropertyDisplay> GetLatestDisplay()
  595. {
  596. 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();
  597. return GetDisplayDetails(props);
  598. }
  599. public List<PropertyDisplay> GetLatestDisplay(string PropertyType)
  600. {
  601. var type = PropertyUsageType.Both;
  602. switch (PropertyType.ToUpper())
  603. {
  604. case "RESIDENTIAL":
  605. type = PropertyUsageType.Residential;
  606. break;
  607. case "COMMERCIAL":
  608. type = PropertyUsageType.Commercial;
  609. break;
  610. }
  611. List<Property> props = dBContext.Properties.Include("PropertyType").Where(x => x.Published
  612. && x.PropertyType.UsageType == type
  613. && (x.CutOffDisplayDate == DateTime.MinValue || x.CutOffDisplayDate.Date > DateTime.Now.Date)).OrderByDescending(x => x.DatePublished).Take(4).ToList();
  614. return GetDisplayDetails(props);
  615. }
  616. public List<PropertyList> GetPropertyList(int By)
  617. {
  618. List<Property> properties = new List<Property>();
  619. var user = dBContext.Users.Where(u => u.Id == By).FirstOrDefault();
  620. if (user != null)
  621. {
  622. if (user.Role.ToUpper() == "AGENCY" || user.Role.ToUpper() == "AGENT")
  623. {
  624. var agent = dBContext.Agents.Where(a => a.UserId == user.Id).FirstOrDefault();
  625. if (user.Role.ToUpper() == "AGENCY")
  626. {
  627. properties = dBContext.Properties.Where(p => p.AgencyId == agent.AgencyId).ToList();
  628. }
  629. else
  630. {
  631. properties = dBContext.Properties.Where(p => p.AgentId == agent.Id).ToList();
  632. }
  633. }
  634. if (user.Role.ToUpper() == "PRIVATE USER")
  635. {
  636. var individual = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
  637. properties = dBContext.Properties.Where(p => p.OwnerId == individual.Id).ToList();
  638. }
  639. if (user.Role.ToUpper() == "SUPER ADMIN")
  640. properties = dBContext.Properties.ToList();
  641. }
  642. return SetPropertyList(properties);
  643. }
  644. public List<PropertyList> GetPropertyList()
  645. {
  646. return SetPropertyList(dBContext.Properties.Where(x => x.Published).ToList());
  647. }
  648. private List<PropertyList> SetPropertyList(List<Property> properties)
  649. {
  650. List<PropertyList> list = new List<PropertyList>();
  651. foreach (Property p in properties)
  652. {
  653. var prop = new PropertyList()
  654. {
  655. Id = p.Id,
  656. Name = string.IsNullOrEmpty(p.PropertyName) ? p.ShortDescription : p.PropertyName,
  657. Price = p.Price,
  658. Publish = p.Published ? "Yes" : "No",
  659. Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description,
  660. CarouselDescription = string.Format("{0}, {1} <br/>{2}", p.Suburb, p.City, p.AddressOther),
  661. DateAvailable = p.IsSale ? DateTime.MinValue : p.DateAvailable,
  662. IsPublished = p.Published
  663. };
  664. prop.Size = (from u in dBContext.PropertyUserFields
  665. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  666. where u.PropertyId == p.Id
  667. && f.FieldName == "Floor Size"
  668. select u.Value).FirstOrDefault();
  669. //if (!string.IsNullOrEmpty(prop.Size) && prop.Size.EndsWith("2"))
  670. //{
  671. // prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
  672. //}
  673. prop.UsageType = (dBContext.PropertyTypes.Find(p.PropertyTypeId).UsageType == PropertyUsageType.Residential ? "Residential" : "Commercial");
  674. prop.SaleType = p.IsSale ? "Sale" : "Rental";
  675. list.Add(prop);
  676. }
  677. return list;
  678. }
  679. public int NewId()
  680. {
  681. // Not sure if properties need it
  682. return 0;
  683. }
  684. public void Insert(PropertyContainer items)
  685. {
  686. Property property = new Property();
  687. PropertyType pt = dBContext.PropertyTypes.Find(items.PropertyTypeId);
  688. if (pt != null)
  689. {
  690. if (pt.UsageType == PropertyUsageType.Residential)
  691. {
  692. string type = dBContext.PropertyTypes.Find(items.PropertyTypeId).Description;
  693. if (items.PropertyUserFields.Count > 0)
  694. {
  695. string shortDesc = "{0} {1} {2}";
  696. UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
  697. var udValue = items.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
  698. if (udValue != null)
  699. items.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
  700. else
  701. items.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
  702. }
  703. else
  704. {
  705. items.ShortDescription = type;
  706. }
  707. }
  708. else
  709. {
  710. items.ShortDescription = pt.Description;
  711. }
  712. }
  713. var images = items.PropertyImages;
  714. var fields = items.PropertyUserFields;
  715. items.PropertyImages = null;
  716. items.PropertyUserFields = null;
  717. var individual = dBContext.Individuals.Where(i => i.UserId == items.UserId).FirstOrDefault();
  718. var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();
  719. foreach (string prop in property.GetAllProperties())
  720. {
  721. if (prop != "Item" && prop != "Display")
  722. property[prop] = items[prop];
  723. }
  724. if (individual != null)
  725. property.OwnerId = individual.Id;
  726. if (agent != null)
  727. {
  728. property.AgencyId = agent.AgencyId;
  729. property.AgentId = agent.Id;
  730. }
  731. if (property.IsSale)
  732. property.StatusId = dBContext.Status.Where(s => s.Description == "For Sale" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
  733. else
  734. property.StatusId = dBContext.Status.Where(s => s.Description == "For Rent" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
  735. if (!string.IsNullOrEmpty(property.Video))
  736. property.Video = property.Video.Replace("https://www.youtube.com/watch?v=", "");
  737. if (images != null)
  738. {
  739. bool saveFiles = false;
  740. var nextPropID = dBContext.GetMaxID("Properties") + 1;
  741. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  742. if (!string.IsNullOrEmpty(loc))
  743. {
  744. saveFiles = true;
  745. if (loc.EndsWith("\\"))
  746. {
  747. loc = loc.Substring(0, loc.Length - 1);
  748. }
  749. loc += string.Format("\\{0}", nextPropID);
  750. if (!Directory.Exists(loc))
  751. {
  752. Directory.CreateDirectory(loc);
  753. }
  754. }
  755. property.PropertyImages = new List<PropertyImage>();
  756. var lastID = 0;
  757. if (dBContext.PropertyImages.Count() == 0)
  758. lastID = 1;
  759. else
  760. lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
  761. foreach (PropertyImage image in images)
  762. {
  763. image.PropertyId = property.Id;
  764. if (saveFiles)
  765. {
  766. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  767. image.Image = path;
  768. }
  769. property.PropertyImages.Add(image);
  770. lastID++;
  771. }
  772. }
  773. if (fields != null)
  774. {
  775. property.PropertyUserFields = new List<PropertyUserField>();
  776. foreach (PropertyUserField field in fields)
  777. {
  778. field.PropertyId = property.Id;
  779. property.PropertyUserFields.Add(field);
  780. }
  781. }
  782. dBContext.Properties.Add(property);
  783. Save();
  784. items.Id = property.Id;
  785. }
  786. public bool MayEdit(int id)
  787. {
  788. var hasBidItems = (from b in dBContext.BidItems
  789. where b.PropertyId == id
  790. && b.StatusId == 2
  791. select b).FirstOrDefault();
  792. return (hasBidItems == null) ? true : false;
  793. }
  794. public void InsertImages(int propertyID, List<PropertyImage> Images)
  795. {
  796. if (Images != null)
  797. {
  798. var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
  799. bool saveFiles = false;
  800. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  801. if (!string.IsNullOrEmpty(loc))
  802. {
  803. saveFiles = true;
  804. loc += string.Format("\\{0}", propertyID);
  805. if (Directory.Exists(loc))
  806. {
  807. Directory.CreateDirectory(loc);
  808. }
  809. }
  810. foreach (PropertyImage image in Images)
  811. {
  812. image.PropertyId = propertyID;
  813. if (saveFiles)
  814. {
  815. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  816. image.Image = path;
  817. }
  818. dBContext.PropertyImages.Add(image);
  819. lastID++;
  820. Save();
  821. }
  822. }
  823. }
  824. public void InsertFields(int propertyID, List<PropertyUserField> Fields)
  825. {
  826. throw new NotImplementedException();
  827. }
  828. public void PublishProperty(int propertyID)
  829. {
  830. var property = dBContext.Properties.Where(p => p.Id == propertyID).FirstOrDefault();
  831. if (property != null)
  832. {
  833. property.Published = true;
  834. property.DatePublished = DateTime.Now;
  835. Update(property);
  836. }
  837. }
  838. public void UnpublishProperty(int propertyID)
  839. {
  840. var property = dBContext.Properties.Where(p => p.Id == propertyID).FirstOrDefault();
  841. if (property != null)
  842. {
  843. property.Published = false;
  844. property.DatePublished = DateTime.MinValue;
  845. Update(property);
  846. }
  847. }
  848. public List<PropertyAdminContainer> GetAdminProperties(int UserId)
  849. {
  850. var user = dBContext.Users.Where(u => u.Id == UserId).FirstOrDefault();
  851. List<PropertyAdminContainer> returnProps = new List<PropertyAdminContainer>();
  852. List<Property> props;
  853. if ((user.Role.ToUpper() == "SUPER ADMIN"))
  854. {
  855. props = dBContext.Properties.Include("Owner").Include("PropertyType").ToList();
  856. }
  857. else if (user.Role.ToUpper() == "AGENCY")
  858. {
  859. var agent = dBContext.Agents.Where(x => x.UserId == user.Id).FirstOrDefault();
  860. props = dBContext.Properties.Include("Owner").Include("PropertyType").Where(x => x.AgencyId == agent.AgencyId).ToList();
  861. }
  862. else if (user.Role.ToUpper() == "DESIGNER")
  863. {
  864. props = dBContext.Properties.Include("Owner").Include("PropertyType").ToList();
  865. }
  866. else
  867. {
  868. var indiv = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
  869. props = dBContext.Properties.Include("Owner").Include("PropertyType").Where(p => p.OwnerId == indiv.Id).ToList();
  870. }
  871. foreach (var prop in props)
  872. {
  873. var propAdmin = new PropertyAdminContainer()
  874. {
  875. Id = prop.Id,
  876. Owner = prop.Owner?.FullName,
  877. Property = prop.PropertyName,
  878. Reference = prop.PropertyRef,
  879. Unit = prop.Unit,
  880. Size = (from u in dBContext.PropertyUserFields
  881. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  882. where u.PropertyId == prop.Id
  883. && f.FieldName == "Floor Size"
  884. select u.Value).FirstOrDefault(),
  885. Price = prop.Price,
  886. Region = prop.Province,
  887. Town = prop.City,
  888. Suburb = prop.Suburb,
  889. IsPublished = prop.Published,
  890. Type = prop.PropertyType.UsageType.ToString()
  891. };
  892. if (prop.StatusId != null)
  893. {
  894. propAdmin.Status = dBContext.Status.Where(s => s.Id == prop.StatusId).FirstOrDefault()?.Description;
  895. }
  896. returnProps.Add(propAdmin);
  897. }
  898. return returnProps;
  899. }
  900. public List<string> GetStatuses()
  901. {
  902. return dBContext.Status.Where(s => s.StatusType == StatusType.Property).Select(s => s.Description).ToList();
  903. }
  904. }
  905. }