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

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