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

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