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

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