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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. .Include("Province")
  28. .Include("City")
  29. .Include("Suburb")
  30. .Where(where).ToList();
  31. }
  32. public List<Property> GetAll()
  33. {
  34. var properties = dBContext.Properties.ToList();
  35. return properties;
  36. }
  37. public Property GetDetailed(Func<Property, bool> first)
  38. {
  39. throw new NotImplementedException();
  40. }
  41. public PropertyContainer GetDetailed(int id, bool detailed)
  42. {
  43. var property = dBContext.Properties.Include("Status").Where(p => p.Id == id).FirstOrDefault();
  44. if (property != null)
  45. {
  46. return GetDetail(property, detailed);
  47. }
  48. return null;
  49. }
  50. public List<Property> GetDetailedAll()
  51. {
  52. var properties = dBContext.Properties.ToList();
  53. return properties;
  54. }
  55. private PropertyContainer GetDetail(Property property, bool detailed)
  56. {
  57. int propID = property.Id;
  58. var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
  59. property.Province = dBContext.Provinces.Find(property.ProvinceId);
  60. property.City = dBContext.Cities.Find(property.CityId);
  61. property.Suburb = dBContext.Suburbs.Find(property.SuburbId);
  62. property.DisplayData = new List<PropertyDetailGroup>();
  63. if (detailed)
  64. {
  65. var groups = (from g in dBContext.UserDefinedGroups
  66. where g.UsageType == propertyType.UsageType
  67. || g.UsageType == PropertyUsageType.Both
  68. orderby g.Rank
  69. select g).ToList();
  70. foreach (UserDefinedGroup uGroup in groups)
  71. {
  72. var groupFields = (from f in dBContext.PropertyUserFields
  73. join uf in dBContext.UserDefinedFields on f.UserDefinedFieldId equals uf.Id
  74. join g in dBContext.UserDefinedGroups on uf.GroupId equals g.Id
  75. where f.PropertyId == propID
  76. && g.Id == uGroup.Id
  77. orderby g.Rank, uf.Rank
  78. select new { uf.FieldName, f.Value, f.Description }).ToList();
  79. if (groupFields.Count > 0)
  80. {
  81. PropertyDetailGroup detailGroup = new PropertyDetailGroup()
  82. {
  83. GroupName = uGroup.Description,
  84. Values = new List<PropertyDetail>()
  85. };
  86. if (uGroup.Description == "Property Overview")
  87. {
  88. detailGroup.Values.Add(new PropertyDetail()
  89. {
  90. Name = "Property Type",
  91. Value = property.PropertyType.Description
  92. });
  93. }
  94. foreach (var val in groupFields)
  95. {
  96. var item = new PropertyDetail()
  97. {
  98. Name = val.FieldName,
  99. Description = val.Description
  100. };
  101. detailGroup.Values.Add(item);
  102. if (!string.IsNullOrEmpty(val.Value) && (val.FieldName == "Erf Size" || val.FieldName == "Floor Size") && val.Value.EndsWith("2"))
  103. {
  104. item.Value = val.Value.Substring(0, val.Value.Length - 1) + "<sup>" + val.Value.Last() + "</sup>";
  105. }
  106. else
  107. item.Value = val.Value;
  108. }
  109. property.DisplayData.Add(detailGroup);
  110. }
  111. }
  112. }
  113. else
  114. {
  115. if (!string.IsNullOrEmpty(property.Video))
  116. property.Video = string.Format("https://www.youtube.com/watch?v={0}", property.Video);
  117. }
  118. var propertyDetails = new PropertyContainer();
  119. foreach (string prop in property.GetAllProperties())
  120. {
  121. if (prop != "Item" && prop != "Display")
  122. propertyDetails[prop] = property[prop];
  123. }
  124. propertyDetails.PropertyUsageType = propertyType.UsageType == PropertyUsageType.Commercial ? "Commercial" : "Residential";
  125. if (property.OwnerId > 0)
  126. propertyDetails.UserId = (dBContext.Individuals.Where(p => p.Id == property.OwnerId).FirstOrDefault().UserId).Value;
  127. if (property.AgentId > 0)
  128. propertyDetails.UserId = (dBContext.Agents.Where(p => p.Id == property.AgentId).FirstOrDefault().UserId).Value;
  129. propertyDetails.NewImages = new List<NewImage>();
  130. return propertyDetails;
  131. }
  132. public void Insert(Property item)
  133. {
  134. throw new NotImplementedException();
  135. }
  136. public void Insert(IEnumerable<Property> items)
  137. {
  138. foreach (var item in items)
  139. {
  140. dBContext.Properties.Add(item);
  141. }
  142. Save();
  143. }
  144. public void Remove(Property item)
  145. {
  146. item.IsDeleted = true;
  147. Save();
  148. }
  149. public void Remove(IEnumerable<Property> items)
  150. {
  151. foreach (var item in items)
  152. {
  153. item.IsDeleted = true;
  154. }
  155. Save();
  156. }
  157. public void RemoveAtId(int item)
  158. {
  159. var property = Get(x => x.Id == item).FirstOrDefault();
  160. if (property != null)
  161. {
  162. dBContext.Properties.Remove(property);
  163. Save();
  164. }
  165. }
  166. public void Save()
  167. {
  168. dBContext.SaveChanges();
  169. }
  170. public void Update(Property item)
  171. {
  172. if (item.Video.StartsWith("http"))
  173. item.Video = item.Video.Replace("https://www.youtube.com/watch?v=", "");
  174. dBContext.Entry(item).State = EntityState.Modified;
  175. Save();
  176. }
  177. public void Update(PropertyContainer item)
  178. {
  179. if (item.Video.StartsWith("http"))
  180. item.Video = item.Video.Replace("https://www.youtube.com/watch?v=", "");
  181. var property = new Property();
  182. foreach (string prop in property.GetAllProperties())
  183. {
  184. if (prop != "Item" && prop != "Display")
  185. property[prop] = item[prop];
  186. }
  187. property.PropertyUserFields = null;
  188. property.PropertyImages = null;
  189. dBContext.Entry(property).State = EntityState.Modified;
  190. #region Insert New UDFs
  191. var lastFieldID = dBContext.GetMaxID("PropertyUserFields");
  192. foreach (var propGroup in item.PropertyOverviewFields)
  193. {
  194. foreach (var field in propGroup.Fields)
  195. {
  196. if (field.ItemId == 0)
  197. {
  198. lastFieldID++;
  199. var propertyField = new PropertyUserField()
  200. {
  201. Id = lastFieldID,
  202. PropertyId = property.Id,
  203. UserDefinedFieldId = field.Id,
  204. Value = field.Value
  205. };
  206. dBContext.Add(propertyField);
  207. }
  208. else
  209. {
  210. var propertyField = dBContext.PropertyUserFields.Where(p => p.Id == field.ItemId).FirstOrDefault();
  211. if (propertyField != null)
  212. {
  213. if (string.IsNullOrEmpty(field.Value))
  214. propertyField.IsDeleted = true;
  215. else
  216. propertyField.Value = field.Value;
  217. dBContext.Entry(propertyField).State = EntityState.Modified;
  218. }
  219. }
  220. }
  221. }
  222. foreach (var propGroup in item.PropertyFields)
  223. {
  224. foreach (var field in propGroup.Fields)
  225. {
  226. if (field.ItemId == 0)
  227. {
  228. lastFieldID++;
  229. var propertyField = new PropertyUserField()
  230. {
  231. Id = lastFieldID,
  232. PropertyId = property.Id,
  233. UserDefinedFieldId = field.Id,
  234. Value = field.Value
  235. };
  236. dBContext.Add(propertyField);
  237. }
  238. else
  239. {
  240. var propertyField = dBContext.PropertyUserFields.Where(p => p.Id == field.ItemId).FirstOrDefault();
  241. if (propertyField != null)
  242. {
  243. if (string.IsNullOrEmpty(field.Value))
  244. propertyField.IsDeleted = true;
  245. else
  246. propertyField.Value = field.Value;
  247. dBContext.Entry(propertyField).State = EntityState.Modified;
  248. }
  249. }
  250. }
  251. }
  252. #endregion
  253. #region Update Images
  254. if (item.PropertyImages != null)
  255. {
  256. foreach (var image in item.PropertyImages)
  257. {
  258. var propImage = dBContext.PropertyImages.Where(pi => pi.Id == image.Id).FirstOrDefault();
  259. if (propImage != null)
  260. {
  261. propImage.IsDefault = image.IsDefault;
  262. propImage.IsDeleted = image.IsDeleted;
  263. dBContext.Entry(propImage).State = EntityState.Modified;
  264. }
  265. }
  266. }
  267. #endregion
  268. Save();
  269. }
  270. public List<PropertyDisplay> GetDisplay()
  271. {
  272. List<Property> props = GetAll();
  273. return GetDisplayDetails(props);
  274. }
  275. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  276. {
  277. List<Property> props;
  278. if (where != null)
  279. props = Get(where);
  280. else
  281. props = GetAll();
  282. return GetDisplayDetails(props);
  283. }
  284. public List<PropertyDisplay> GetDisplay(PropertySearch search)
  285. {
  286. //return GetDisplayDetails(dBContext.Properties.ToList());
  287. //StreamWriter SW = new StreamWriter(@"c:\temp\SearchData.txt", true);
  288. //SW.WriteLine(string.Format("{0:yyyy-MM-dd hh:mm:ss} - {1}", DateTime.Now, JsonConvert.SerializeObject(search)));
  289. //SW.Close();
  290. SearchObject obj = new SearchObject()
  291. {
  292. UserName = search.UserName,
  293. Type = "Property"
  294. };
  295. if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL" && search.Keyword.ToUpper() != "UNDEFINED")
  296. {
  297. string keyword = search.Keyword.ToLower();
  298. List<Property> props = (from p in dBContext.Properties
  299. join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
  300. join c in dBContext.Cities on p.CityId equals c.Id
  301. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  302. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  303. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
  304. || EF.Functions.Like(pr.Description.ToLower(), $"%{keyword}%")
  305. || EF.Functions.Like(c.Description.ToLower(), $"%{keyword}%")
  306. || EF.Functions.Like(s.Description.ToLower(), $"%{keyword}%")
  307. || EF.Functions.Like(pt.Description.ToLower(), $"%{keyword}%")
  308. select p).ToList();
  309. obj.Property = "Keyword";
  310. obj.Value = search.Keyword;
  311. SaveLog(obj);
  312. return GetDisplayDetails(props);
  313. }
  314. else
  315. {
  316. List<Property> props;
  317. //Property ID search will override other searches.
  318. if (search.PropertyId > 0)
  319. {
  320. obj.Property = "PropertyID";
  321. obj.Value = search.PropertyId.ToString();
  322. SaveLog(obj);
  323. props = dBContext.Properties.Where(p => p.Id == search.PropertyId).ToList();
  324. return GetDisplayDetails(props);
  325. }
  326. PropertyUsageType uType = PropertyUsageType.Both;
  327. if (!string.IsNullOrEmpty(search.PropertyUsageType) && search.PropertyUsageType != "undefined" && search.PropertyUsageType.ToUpper() != "ALL")
  328. {
  329. if (search.PropertyUsageType.ToUpper() == "COMMERCIAL")
  330. uType = PropertyUsageType.Commercial;
  331. else
  332. uType = PropertyUsageType.Residential;
  333. }
  334. props = (from p in dBContext.Properties
  335. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  336. where pt.UsageType == uType
  337. select p).ToList();
  338. obj.Property = "PropertyUsageType";
  339. obj.Value = search.PropertyUsageType;
  340. SaveLog(obj);
  341. if (!string.IsNullOrEmpty(search.SalesType) && search.SalesType != "undefined" && search.SalesType.ToUpper() != "ALL")
  342. {
  343. if (search.SalesType.ToUpper() == "SALE")
  344. {
  345. props = props.Where(p => p.IsSale).ToList();
  346. search.AvailableFrom = DateTime.MinValue; //Sales do not have an available from date.
  347. }
  348. else
  349. props = props.Where(p => !p.IsSale).ToList();
  350. obj.Property = "SalesType";
  351. obj.Value = search.SalesType;
  352. SaveLog(obj);
  353. }
  354. if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
  355. {
  356. props = (from p in props
  357. join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
  358. where pp.Description.ToUpper() == search.Province.ToUpper()
  359. select p).ToList();
  360. obj.Property = "Province";
  361. obj.Value = search.Province;
  362. SaveLog(obj);
  363. }
  364. if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
  365. {
  366. props = (from p in props
  367. join c in dBContext.Cities on p.CityId equals c.Id
  368. where c.Description.ToUpper() == search.City.ToUpper()
  369. select p).ToList();
  370. obj.Property = "City";
  371. obj.Value = search.City;
  372. SaveLog(obj);
  373. }
  374. if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
  375. {
  376. props = (from p in props
  377. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  378. where s.Description.ToUpper() == search.Suburb.ToUpper()
  379. select p).ToList();
  380. obj.Property = "Suburb";
  381. obj.Value = search.Suburb;
  382. SaveLog(obj);
  383. }
  384. if (!string.IsNullOrEmpty(search.PropertyType) && search.PropertyType != "Undefined" && search.PropertyType.ToUpper() != "ALL")
  385. {
  386. var pType = dBContext.PropertyTypes.Where(t => t.Description == search.PropertyType).FirstOrDefault();
  387. if (pType != null)
  388. {
  389. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  390. }
  391. obj.Property = "PropertyType";
  392. obj.Value = search.PropertyType;
  393. SaveLog(obj);
  394. }
  395. if (search.MinPrice > 0)
  396. {
  397. props = props.Where(p => p.Price >= search.MinPrice).ToList();
  398. obj.Property = "MinPrice";
  399. obj.Value = search.MinPrice.ToString();
  400. SaveLog(obj);
  401. }
  402. if (search.MaxPrice > 0)
  403. {
  404. props = props.Where(p => p.Price <= search.MaxPrice).ToList();
  405. obj.Property = "MaxPrice";
  406. obj.Value = search.MaxPrice.ToString();
  407. SaveLog(obj);
  408. }
  409. if (search.AvailableFrom != DateTime.MinValue)
  410. {
  411. props = props.Where(p => p.DateAvailable.Date >= search.AvailableFrom.Date).ToList();
  412. obj.Property = "AvailableFrom";
  413. obj.Value = search.AvailableFrom.ToString();
  414. SaveLog(obj);
  415. }
  416. return GetDisplayDetails(props);
  417. }
  418. }
  419. private void SaveLog(SearchObject item)
  420. {
  421. var searchLog = new SearchLog
  422. {
  423. Type = item.Type,
  424. Search = JsonConvert.SerializeObject(item)
  425. };
  426. searchLog.Id = dBContext.GetMaxID("SearchLogs") + 1;
  427. dBContext.SearchLogs.Add(searchLog);
  428. Save();
  429. }
  430. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  431. {
  432. var properties = new List<PropertyDisplay>();
  433. props = props.Where(p => p.Published).ToList();
  434. foreach (var item in props)
  435. {
  436. PropertyDisplay display = new PropertyDisplay
  437. {
  438. DateAvailable = item.DateAvailable,
  439. Available = item.DateAvailable.Date > DateTime.Now.Date ? string.Format("Available form: {0: dd MMM yyyy}", item.DateAvailable) : "Available Now",
  440. Id = item.Id,
  441. ShortDescription = item.ShortDescription,
  442. IsSale = item.IsSale,
  443. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  444. DisplayImage = (from i in dBContext.PropertyImages
  445. where i.PropertyId == item.Id
  446. && i.IsDefault
  447. select i.Image).FirstOrDefault(),
  448. Area = (from u in dBContext.PropertyUserFields
  449. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  450. where u.PropertyId == item.Id
  451. && f.FieldName == "Floor Size"
  452. select u.Value).FirstOrDefault(),
  453. Beds = (from u in dBContext.PropertyUserFields
  454. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  455. where u.PropertyId == item.Id
  456. && f.FieldName == "Bedrooms"
  457. select u.Value).FirstOrDefault(),
  458. Baths = (from u in dBContext.PropertyUserFields
  459. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  460. where u.PropertyId == item.Id
  461. && f.FieldName == "Bathrooms"
  462. select u.Value).FirstOrDefault(),
  463. Garages = (from u in dBContext.PropertyUserFields
  464. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  465. where u.PropertyId == item.Id
  466. && f.FieldName == "Garages"
  467. select u.Value).FirstOrDefault(),
  468. Province = (from p in dBContext.Provinces
  469. where p.Id == item.ProvinceId
  470. select p.Description).FirstOrDefault(),
  471. City = (from c in dBContext.Cities
  472. where c.Id == item.CityId
  473. select c.Description).FirstOrDefault(),
  474. Suburb = (from s in dBContext.Suburbs
  475. where s.Id == item.SuburbId
  476. select s.Description).FirstOrDefault(),
  477. Price = item.Price,
  478. DateCreated = item.Created,
  479. PropertyUsageType = (from p in dBContext.PropertyTypes
  480. where p.Id == item.PropertyTypeId
  481. select p.UsageType.ToString()).FirstOrDefault(),
  482. HasPendingOffer = !MayEdit(item.Id)
  483. };
  484. if (display.DisplayImage != null && !display.DisplayImage.StartsWith("data:image"))
  485. {
  486. display.DisplayImage = ImageFormatter.ImageToBase64(display.DisplayImage);
  487. }
  488. if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
  489. {
  490. display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
  491. }
  492. if (display.HasPendingOffer)
  493. display.Available = "Offer Pending";
  494. #if !ReturnImages
  495. display.DisplayImage = "";
  496. #endif
  497. properties.Add(display);
  498. }
  499. return properties;
  500. }
  501. public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
  502. {
  503. List<PropertyType> list;
  504. if (where != null)
  505. list = dBContext.PropertyTypes.Where(where).ToList();
  506. else
  507. list = dBContext.PropertyTypes.ToList();
  508. return list;
  509. }
  510. public List<PropertyDisplay> GetLatestDisplay()
  511. {
  512. List<Property> props = GetAll().Where(x => x.Published).OrderByDescending(x => x.DatePublished).Take(3).ToList();
  513. return GetDisplayDetails(props);
  514. }
  515. public List<PropertyList> GetPropertyList(int By)
  516. {
  517. List<Property> properties = new List<Property>();
  518. var user = dBContext.Users.Where(u => u.Id == By).FirstOrDefault();
  519. if (user != null)
  520. {
  521. if (user.Role.ToUpper() == "AGENCY" || user.Role.ToUpper() == "AGENT")
  522. {
  523. var agent = dBContext.Agents.Where(a => a.UserId == user.Id).FirstOrDefault();
  524. if (user.Role.ToUpper() == "AGENCY")
  525. {
  526. properties = dBContext.Properties.Where(p => p.AgencyId == agent.AgencyId).ToList();
  527. }
  528. else
  529. {
  530. properties = dBContext.Properties.Where(p => p.AgentId == agent.Id).ToList();
  531. }
  532. }
  533. if (user.Role.ToUpper() == "PRIVATE USER")
  534. {
  535. var individual = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
  536. properties = dBContext.Properties.Where(p => p.OwnerId == individual.Id).ToList();
  537. }
  538. if (user.Role.ToUpper() == "SUPER ADMIN")
  539. properties = dBContext.Properties.Include("City").Include("Suburb").ToList();
  540. }
  541. return SetPropertyList(properties);
  542. }
  543. public List<PropertyList> GetPropertyList()
  544. {
  545. return SetPropertyList(dBContext.Properties.Where(x => x.Published).ToList());
  546. }
  547. private List<PropertyList> SetPropertyList(List<Property> properties)
  548. {
  549. List<PropertyList> list = new List<PropertyList>();
  550. foreach (Property p in properties)
  551. {
  552. var prop = new PropertyList()
  553. {
  554. Id = p.Id,
  555. Name = string.IsNullOrEmpty(p.PropertyName) ? p.ShortDescription : p.PropertyName,
  556. Price = p.Price,
  557. Publish = p.Published ? "Yes" : "No",
  558. Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description,
  559. CarouselDescription = string.Format("{0}, {1} <br/>{2}", p.Suburb?.Description, p.City?.Description, p.AddressLine3),
  560. DateAvailable = p.IsSale ? DateTime.MinValue : p.DateAvailable,
  561. IsPublished = p.Published
  562. };
  563. prop.Size = (from u in dBContext.PropertyUserFields
  564. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  565. where u.PropertyId == p.Id
  566. && f.FieldName == "Floor Size"
  567. select u.Value).FirstOrDefault();
  568. if (!string.IsNullOrEmpty(prop.Size) && prop.Size.EndsWith("2"))
  569. {
  570. prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
  571. }
  572. prop.UsageType = (dBContext.PropertyTypes.Find(p.PropertyTypeId).UsageType == PropertyUsageType.Residential ? "Residential" : "Commercial");
  573. prop.SaleType = p.IsSale ? "Sale" : "Rental";
  574. list.Add(prop);
  575. }
  576. return list;
  577. }
  578. public int NewId()
  579. {
  580. // Not sure if properties need it
  581. return 0;
  582. }
  583. public void Insert(PropertyContainer items)
  584. {
  585. Property property = new Property();
  586. PropertyType pt = dBContext.PropertyTypes.Find(items.PropertyTypeId);
  587. if (pt != null)
  588. {
  589. if (pt.UsageType == PropertyUsageType.Residential)
  590. {
  591. string type = dBContext.PropertyTypes.Find(items.PropertyTypeId).Description;
  592. if (items.PropertyUserFields.Count > 0)
  593. {
  594. string shortDesc = "{0} {1} {2}";
  595. UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
  596. var udValue = items.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
  597. if (udValue != null)
  598. items.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
  599. else
  600. items.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
  601. }
  602. else
  603. {
  604. items.ShortDescription = type;
  605. }
  606. }
  607. else
  608. {
  609. items.ShortDescription = pt.Description;
  610. }
  611. }
  612. var images = items.PropertyImages;
  613. var fields = items.PropertyUserFields;
  614. items.PropertyImages = null;
  615. items.PropertyUserFields = null;
  616. var individual = dBContext.Individuals.Where(i => i.UserId == items.UserId).FirstOrDefault();
  617. var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();
  618. foreach( string prop in property.GetAllProperties())
  619. {
  620. if (prop != "Item" && prop != "Display")
  621. property[prop] = items[prop];
  622. }
  623. if (individual != null)
  624. property.OwnerId = individual.Id;
  625. if (agent != null)
  626. {
  627. property.AgencyId = agent.AgencyId;
  628. property.AgentId = agent.Id;
  629. }
  630. property.Video = property.Video.Replace("https://www.youtube.com/watch?v=", "");
  631. if (images != null)
  632. {
  633. var lastID = dBContext.GetMaxID("PropertyImages");
  634. bool saveFiles = false;
  635. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  636. if (!string.IsNullOrEmpty(loc))
  637. {
  638. saveFiles = true;
  639. loc += string.Format("\\{0}", property.Id);
  640. if (Directory.Exists(loc))
  641. {
  642. Directory.CreateDirectory(loc);
  643. }
  644. }
  645. property.PropertyImages = new List<PropertyImage>();
  646. foreach (PropertyImage image in images)
  647. {
  648. lastID++;
  649. image.Id = lastID;
  650. image.PropertyId = property.Id;
  651. if (saveFiles)
  652. {
  653. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  654. image.Image = path;
  655. }
  656. property.PropertyImages.Add(image);
  657. }
  658. }
  659. if (fields != null)
  660. {
  661. var lastID = dBContext.GetMaxID("PropertyUserFields");
  662. property.PropertyUserFields = new List<PropertyUserField>();
  663. foreach (PropertyUserField field in fields)
  664. {
  665. lastID++;
  666. field.Id = lastID;
  667. field.PropertyId = property.Id;
  668. property.PropertyUserFields.Add(field);
  669. }
  670. }
  671. dBContext.Properties.Add(property);
  672. Save();
  673. items.Id = property.Id;
  674. }
  675. public bool MayEdit(int id)
  676. {
  677. var hasBidItems = (from b in dBContext.BidItems
  678. where b.PropertyId == id
  679. && b.StatusId == 2
  680. select b).FirstOrDefault();
  681. return (hasBidItems == null) ? true : false;
  682. }
  683. public void InsertImages(int propertyID, List<PropertyImage> Images)
  684. {
  685. if (Images != null)
  686. {
  687. var lastID = dBContext.GetMaxID("PropertyImages");
  688. bool saveFiles = false;
  689. var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
  690. if (!string.IsNullOrEmpty(loc))
  691. {
  692. saveFiles = true;
  693. loc += string.Format("\\{0}", propertyID);
  694. if (Directory.Exists(loc))
  695. {
  696. Directory.CreateDirectory(loc);
  697. }
  698. }
  699. foreach (PropertyImage image in Images)
  700. {
  701. lastID++;
  702. image.Id = lastID;
  703. image.PropertyId = propertyID;
  704. if (saveFiles)
  705. {
  706. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  707. image.Image = path;
  708. }
  709. dBContext.PropertyImages.Add(image);
  710. Save();
  711. }
  712. }
  713. }
  714. public void InsertFields(int propertyID, List<PropertyUserField> Fields)
  715. {
  716. throw new NotImplementedException();
  717. }
  718. public void PublishProperty(int propertyID)
  719. {
  720. var property = dBContext.Properties.Where(p => p.Id == propertyID).FirstOrDefault();
  721. if (property != null)
  722. {
  723. property.Published = true;
  724. property.DatePublished = DateTime.Now;
  725. Update(property);
  726. }
  727. }
  728. public void UnpublishProperty(int propertyID)
  729. {
  730. var property = dBContext.Properties.Where(p => p.Id == propertyID).FirstOrDefault();
  731. if (property != null)
  732. {
  733. property.Published = false;
  734. property.DatePublished = DateTime.MinValue;
  735. Update(property);
  736. }
  737. }
  738. }
  739. }