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

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