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

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