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

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