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

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