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

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