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

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