12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004 |
- #define ReturnImages
- //#undef ReturnImages //Comment out to return images
-
- using Microsoft.EntityFrameworkCore;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnivateProperties_API.Containers.Property;
- using UnivateProperties_API.Containers.Timeshare;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Model.Logging;
- using UnivateProperties_API.Model.Properties;
-
- namespace UnivateProperties_API.Repository.Properties
- {
- public class PropertyRepository : IPropertyRepository
- {
- private readonly DataContext dBContext;
-
- public PropertyRepository(DataContext _dBContext)
- {
- dBContext = _dBContext;
- }
-
- public List<Property> Get(Func<Property, bool> where)
- {
- return dBContext.Properties
- .Include("PropertyType")
- .Where(where).ToList();
- }
-
- public List<Property> GetAll()
- {
- var properties = dBContext.Properties.Include("PropertyType").ToList();
- return properties;
- }
-
- public Property GetDetailed(Func<Property, bool> first)
- {
- throw new NotImplementedException();
- }
-
- public PropertyContainer GetDetailed(int id, bool detailed)
- {
- //var property = dBContext.Properties.Include("StatusId").Where(p => p.Id == id).FirstOrDefault();
- var property = dBContext.Properties.Where(p => p.Id == id).FirstOrDefault();
- if (property != null)
- {
- return GetDetail(property, detailed);
- }
- else
- {
- var prop = new PropertyContainer
- {
- PropertyImages = new List<PropertyImage>(),
- PropertyUserFields = new List<PropertyUserField>(),
- NewImages = new List<NewImage>(),
- PropertyOverviewFields = new List<PropertyFieldGroup>(),
- PropertyFields = new List<PropertyFieldGroup>()
- };
-
- return prop;
- }
- }
-
- public List<Property> GetDetailedAll()
- {
- var properties = dBContext.Properties.ToList();
- return properties;
- }
-
- private PropertyContainer GetDetail(Property property, bool detailed)
- {
- int propID = property.Id;
- var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
- property.DisplayData = new List<PropertyDetailGroup>();
-
- if (detailed)
- {
- var groups = (from g in dBContext.UserDefinedGroups
- where g.UsageType == propertyType.UsageType
- || g.UsageType == PropertyUsageType.Both
- orderby g.Rank
- select g).ToList();
-
- foreach (UserDefinedGroup uGroup in groups)
- {
- var groupFields = (from f in dBContext.PropertyUserFields
- join uf in dBContext.UserDefinedFields on f.UserDefinedFieldId equals uf.Id
- join g in dBContext.UserDefinedGroups on uf.GroupId equals g.Id
- where f.PropertyId == propID
- && g.Id == uGroup.Id
- orderby g.Rank, uf.Rank
- select new { uf.FieldName, f.Value, f.Description }).ToList();
-
- if (groupFields.Count > 0)
- {
- PropertyDetailGroup detailGroup = new PropertyDetailGroup()
- {
- GroupName = uGroup.Description,
- Values = new List<PropertyDetail>()
- };
-
- if (uGroup.Description == "Property Overview")
- {
- detailGroup.Values.Add(new PropertyDetail()
- {
- Name = "Property Type",
- Value = property.PropertyType.Description
- });
- }
-
- foreach (var val in groupFields)
- {
- if (!string.IsNullOrEmpty(val.Value))
- {
- var item = new PropertyDetail()
- {
- Name = val.FieldName,
- Description = val.Description,
- Value = val.Value
- };
-
- detailGroup.Values.Add(item);
- }
- }
- property.DisplayData.Add(detailGroup);
- }
- }
- }
- else
- {
- if (!string.IsNullOrEmpty(property.Video))
- property.Video = string.Format("https://www.youtube.com/watch?v={0}", property.Video);
- }
-
- var propertyDetails = new PropertyContainer();
-
- propertyDetails.SalesTypeString = property.IsSale ? "Sale" : "Rental";
- propertyDetails.OldPrice = property.Price;
-
- foreach (string prop in property.GetAllProperties())
- {
- if (prop != "Item" && prop != "Display")
- propertyDetails[prop] = property[prop];
- }
-
- if (property.StatusId != null)
- {
- propertyDetails.StatusString = dBContext.Status.Where(s => s.Id == property.StatusId).FirstOrDefault()?.Description;
- propertyDetails.OldStatus = propertyDetails.StatusString;
- }
-
- propertyDetails.PropertyUsageType = propertyType.UsageType == PropertyUsageType.Commercial ? "Commercial" : "Residential";
-
- if (property.OwnerId > 0)
- propertyDetails.UserId = (dBContext.Individuals.Where(p => p.Id == property.OwnerId).FirstOrDefault().UserId).Value;
- if (property.AgentId > 0)
- propertyDetails.UserId = (dBContext.Agents.Where(p => p.Id == property.AgentId).FirstOrDefault().UserId).Value;
-
- propertyDetails.NewImages = new List<NewImage>();
-
- if (property.DateAvailable != DateTime.MinValue)
- propertyDetails.DateAvailableString = string.Format("{0:yyyy-MM-dd}", property.DateAvailable);
-
- if (property.CutOffDisplayDate == DateTime.MinValue)
- propertyDetails.CutOffDisplayDateString = string.Format("{0:yyyy-MM-dd}", DateTime.Now.AddMonths(1));
- else
- propertyDetails.CutOffDisplayDateString = string.Format("{0:yyyy-MM-dd}", property.CutOffDisplayDate);
-
- return propertyDetails;
- }
-
- public void Insert(Property item)
- {
- throw new NotImplementedException();
- }
-
- public void Insert(IEnumerable<Property> items)
- {
- foreach (var item in items)
- {
- dBContext.Properties.Add(item);
- }
- Save();
- }
-
- public void Remove(Property item)
- {
- item.IsDeleted = true;
- Save();
- }
-
- public void Remove(IEnumerable<Property> items)
- {
- foreach (var item in items)
- {
- item.IsDeleted = true;
- }
- Save();
- }
-
- public void RemoveAtId(int item)
- {
- var property = Get(x => x.Id == item).FirstOrDefault();
- if (property != null)
- {
- dBContext.Properties.Remove(property);
- Save();
- }
- }
-
- public void Save()
- {
- dBContext.SaveChanges();
- }
-
- public void Update(Property item)
- {
- if (!string.IsNullOrEmpty(item.Video) && item.Video.StartsWith("http"))
- item.Video = item.Video.Replace("https://www.youtube.com/watch?v=", "");
-
- dBContext.Entry(item).State = EntityState.Modified;
- Save();
- }
-
- public void Update(PropertyContainer item)
- {
- if (!string.IsNullOrEmpty(item.Video) && item.Video.StartsWith("http"))
- item.Video = item.Video.Replace("https://www.youtube.com/watch?v=", "");
-
- var property = new Property();
- foreach (string prop in property.GetAllProperties())
- {
- if (prop != "Item" && prop != "Display")
- property[prop] = item[prop];
- }
-
- property.PropertyUserFields = null;
- property.PropertyImages = null;
-
- if (item.Price < item.OldPrice)
- {
- property.PriceRedused = true;
- }
-
- if (!string.IsNullOrEmpty(item.StatusString))
- {
- if (item.StatusString != item.OldStatus)
- property.StatusDate = DateTime.Now;
-
- property.StatusId = dBContext.Status.Where(s => s.Description == item.StatusString && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
- if ((item.StatusString.ToUpper() == "RENTED OUT" || item.StatusString.ToUpper() == "SOLD") && item.StatusString != item.OldStatus)
- {
- property.CutOffDisplayDate = DateTime.Now.AddMonths(1);
- }
- else
- {
- property.CutOffDisplayDate = DateTime.MinValue;
- }
- if (item.StatusString.ToUpper() == "FOR SALE")
- property.IsSale = true;
- if (item.StatusString.ToUpper() == "FOR RENT")
- property.IsSale = false;
- }
-
- if (!string.IsNullOrEmpty(item.DateAvailableString))
- {
- property.DateAvailable = DateTime.Parse(item.DateAvailableString);
- }
-
- dBContext.Entry(property).State = EntityState.Modified;
- Save();
-
- #region Insert New UDFs
-
- foreach (var propGroup in item.PropertyFields)
- {
- foreach (var field in propGroup.Fields)
- {
- if (field.Value != null)
- {
- var propField = dBContext.PropertyUserFields.Where(u => u.PropertyId == property.Id && u.UserDefinedFieldId == field.Id).FirstOrDefault();
- if (propField == null)
- {
- propField = new PropertyUserField()
- {
- PropertyId = property.Id,
- UserDefinedFieldId = field.Id,
- Value = field.Value
- };
- dBContext.Add(propField);
- Save();
- }
- else
- {
- propField.Value = field.Value;
- dBContext.Entry(propField).State = EntityState.Modified;
- Save();
- }
- }
- }
- }
- #endregion
-
- #region Update Images
- //property.PropertyImages = new List<PropertyImage>();
-
- if (item.PropertyImages != null)
- {
- foreach (var propImg in item.PropertyImages)
- {
- var image = dBContext.PropertyImages.Where(pi => pi.Id == propImg.Id).FirstOrDefault();
- if (image != null)
- {
- if (propImg.IsDeleted)
- dBContext.PropertyImages.Remove(image);
- else
- {
- image.IsDefault = propImg.IsDefault;
- dBContext.Entry(image).State = EntityState.Modified;
- Save();
- //property.PropertyImages.Add(image);
- }
- }
- }
- }
-
- if (item.NewImages != null)
- {
- bool saveFiles = false;
- var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
- if (!string.IsNullOrEmpty(loc))
- {
- saveFiles = true;
- loc += string.Format("\\{0}", property.Id);
- if (Directory.Exists(loc))
- {
- Directory.CreateDirectory(loc);
- }
- }
-
-
- var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
- foreach (var image in item.NewImages)
- {
- var propImage = new PropertyImage
- {
- PropertyId = property.Id,
- Image = image.Image,
- IsDefault = image.IsDefault
- };
-
- if (saveFiles)
- {
- string path = ImageFormatter.Base64ToImage(propImage.Image, loc, lastID.ToString());
- propImage.Image = path;
- }
-
- var exists = dBContext.PropertyImages.Where(pi => pi.Image == propImage.Image && pi.PropertyId == property.Id).FirstOrDefault();
- if (exists == null)
- {
- //property.PropertyImages.Add(propImage);
- dBContext.PropertyImages.Add(propImage);
- Save();
- lastID++;
- }
- }
- }
-
- #endregion
-
- Save();
- }
-
- public List<PropertyDisplay> GetDisplay()
- {
- List<Property> props = GetAll();
- return GetDisplayDetails(props);
- }
-
- public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
- {
- List<Property> props;
- if (where != null)
- props = Get(where);
- else
- props = GetAll();
-
- return GetDisplayDetails(props);
- }
-
- public List<PropertyDisplay> GetDisplay(PropertySearch search)
- {
- //return GetDisplayDetails(dBContext.Properties.ToList());
-
- //StreamWriter SW = new StreamWriter(@"c:\temp\SearchData.txt", true);
- //SW.WriteLine(string.Format("{0:yyyy-MM-dd hh:mm:ss} - {1}", DateTime.Now, JsonConvert.SerializeObject(search)));
- //SW.Close();
-
- SearchObject obj = new SearchObject()
- {
- UserName = search.UserName,
- Type = "Property"
- };
-
- if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL" && search.Keyword.ToUpper() != "UNDEFINED")
- {
- string keyword = search.Keyword.ToLower();
-
- List<Property> props = (from p in dBContext.Properties
- join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
- where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
- || EF.Functions.Like(p.Province.ToLower(), $"%{keyword}%")
- || EF.Functions.Like(p.City.ToLower(), $"%{keyword}%")
- || EF.Functions.Like(p.Suburb.ToLower(), $"%{keyword}%")
- || EF.Functions.Like(pt.Description.ToLower(), $"%{keyword}%")
- select p).ToList();
- obj.Property = "Keyword";
- obj.Value = search.Keyword;
- SaveLog(obj);
-
- return GetDisplayDetails(props);
- }
- else
- {
- List<Property> props;
-
- //Property ID search will override other searches.
- if (search.PropertyId > 0)
- {
- obj.Property = "PropertyID";
- obj.Value = search.PropertyId.ToString();
- SaveLog(obj);
-
- props = dBContext.Properties.Where(p => p.Id == search.PropertyId).ToList();
- return GetDisplayDetails(props);
- }
-
- PropertyUsageType uType = PropertyUsageType.Both;
-
- if (!string.IsNullOrEmpty(search.PropertyUsageType) && search.PropertyUsageType != "undefined" && search.PropertyUsageType.ToUpper() != "ALL")
- {
- if (search.PropertyUsageType.ToUpper() == "COMMERCIAL")
- uType = PropertyUsageType.Commercial;
- else
- uType = PropertyUsageType.Residential;
- }
- props = (from p in dBContext.Properties
- join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
- where pt.UsageType == uType
- select p).ToList();
-
- obj.Property = "PropertyUsageType";
- obj.Value = search.PropertyUsageType;
- SaveLog(obj);
-
- if (!string.IsNullOrEmpty(search.SalesType) && search.SalesType != "undefined" && search.SalesType.ToUpper() != "ALL")
- {
- if (search.SalesType.ToUpper() == "SALE")
- {
- props = props.Where(p => p.IsSale).ToList();
- search.AvailableFrom = DateTime.MinValue; //Sales do not have an available from date.
- }
- else
- props = props.Where(p => !p.IsSale).ToList();
-
- obj.Property = "SalesType";
- obj.Value = search.SalesType;
- SaveLog(obj);
- }
-
- if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
- {
- props = (from p in props
- where p.Province.ToUpper() == search.Province.ToUpper()
- select p).ToList();
-
- obj.Property = "Province";
- obj.Value = search.Province;
- SaveLog(obj);
- }
-
- if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
- {
- props = (from p in props
- where p.City.ToUpper() == search.City.ToUpper()
- select p).ToList();
-
- obj.Property = "City";
- obj.Value = search.City;
- SaveLog(obj);
- }
-
- if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
- {
- props = (from p in props
- where p.Suburb.ToUpper() == search.Suburb.ToUpper()
- select p).ToList();
-
- obj.Property = "Suburb";
- obj.Value = search.Suburb;
- SaveLog(obj);
- }
- if (!string.IsNullOrEmpty(search.PropertyType) && search.PropertyType != "Undefined" && search.PropertyType.ToUpper() != "ALL")
- {
- var pType = dBContext.PropertyTypes.Where(t => t.Description == search.PropertyType).FirstOrDefault();
- if (pType != null)
- {
- props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
- }
-
- obj.Property = "PropertyType";
- obj.Value = search.PropertyType;
- SaveLog(obj);
- }
- if (search.MinPrice > 0)
- {
- props = props.Where(p => p.Price >= search.MinPrice).ToList();
-
- obj.Property = "MinPrice";
- obj.Value = search.MinPrice.ToString();
- SaveLog(obj);
- }
- if (search.MaxPrice > 0)
- {
- props = props.Where(p => p.Price <= search.MaxPrice).ToList();
-
- obj.Property = "MaxPrice";
- obj.Value = search.MaxPrice.ToString();
- SaveLog(obj);
- }
-
- if (search.AvailableFrom != DateTime.MinValue)
- {
- props = props.Where(p => p.DateAvailable.Date >= search.AvailableFrom.Date).ToList();
-
- obj.Property = "AvailableFrom";
- obj.Value = search.AvailableFrom.ToString();
- SaveLog(obj);
- }
-
- return GetDisplayDetails(props);
- }
- }
-
- private void SaveLog(SearchObject item)
- {
- var searchLog = new SearchLog
- {
- Type = item.Type,
- Search = JsonConvert.SerializeObject(item)
- };
- dBContext.SearchLogs.Add(searchLog);
- Save();
- }
-
- private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
- {
- var properties = new List<PropertyDisplay>();
- props = props.Where(p => p.Published).ToList();
- foreach (var item in props)
- {
- PropertyDisplay display = new PropertyDisplay
- {
- PropertyReference = item.PropertyRef,
- DateAvailable = item.DateAvailable,
- Id = item.Id,
- ShortDescription = item.ShortDescription,
- IsSale = item.IsSale,
- DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
- DisplayImage = (from i in dBContext.PropertyImages
- where i.PropertyId == item.Id
- && i.IsDefault
- select i.Image).FirstOrDefault(),
- Area = (from u in dBContext.PropertyUserFields
- join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
- where u.PropertyId == item.Id
- && f.FieldName == "Floor Size"
- select u.Value).FirstOrDefault(),
- Beds = (from u in dBContext.PropertyUserFields
- join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
- where u.PropertyId == item.Id
- && f.FieldName == "Bedrooms"
- select u.Value).FirstOrDefault(),
- Baths = (from u in dBContext.PropertyUserFields
- join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
- where u.PropertyId == item.Id
- && f.FieldName == "Bathrooms"
- select u.Value).FirstOrDefault(),
- Garages = (from u in dBContext.PropertyUserFields
- join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
- where u.PropertyId == item.Id
- && f.FieldName == "Garages"
- select u.Value).FirstOrDefault(),
- Province = item.Province,
- City = item.City,
- Suburb = item.Suburb,
- Price = item.Price,
- DateCreated = item.Created,
- PropertyUsageType = (from p in dBContext.PropertyTypes
- where p.Id == item.PropertyTypeId
- select p.UsageType.ToString()).FirstOrDefault()
- };
-
- var status = dBContext.Status.Where(s => s.Id == item.StatusId).FirstOrDefault();
- switch (status.Description.ToUpper())
- {
- case "FOR RENT":
- if (item.DateAvailable < DateTime.Now)
- {
- display.DisplayText = "Available Now";
- display.DisplayColor = "green";
- }
- else
- {
- display.DisplayText = string.Format("Available From: {0:dd MMM yyyy}", item.DateAvailable);
- display.DisplayColor = "blue";
- }
- break;
- case "FOR SALE":
- if (item.PriceRedused)
- {
- display.DisplayText = "Price Redused";
- display.DisplayColor = "green";
- }
- else
- {
- display.DisplayText = "For Sale";
- display.DisplayColor = "blue";
- }
- break;
- case "OFFER PENDING":
- display.DisplayText = "Offer Pending";
- display.DisplayColor = "orange";
- break;
- default:
- display.DisplayText = status.Description;
- display.DisplayColor = "red";
- break;
-
- }
-
- if (display.DisplayImage != null && !display.DisplayImage.StartsWith("data:image"))
- {
- display.DisplayImage = ImageFormatter.ImageToBase64(display.DisplayImage);
- }
- #if !ReturnImages
- display.DisplayImage = "";
- #endif
- properties.Add(display);
- }
-
- return properties;
- }
-
- public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
- {
- List<PropertyType> list;
- if (where != null)
- list = dBContext.PropertyTypes.Where(where).ToList();
- else
- list = dBContext.PropertyTypes.ToList();
-
- return list;
- }
-
- public List<PropertyDisplay> GetLatestDisplay()
- {
- List<Property> props = GetAll().Where(x => x.Published && (x.CutOffDisplayDate == DateTime.MinValue || x.CutOffDisplayDate.Date > DateTime.Now.Date)).OrderByDescending(x => x.DatePublished).Take(4).ToList();
- return GetDisplayDetails(props);
- }
-
- public List<PropertyDisplay> GetLatestDisplay(string PropertyType)
- {
- var type = PropertyUsageType.Both;
- switch (PropertyType.ToUpper())
- {
- case "RESIDENTIAL":
- type = PropertyUsageType.Residential;
- break;
- case "COMMERCIAL":
- type = PropertyUsageType.Commercial;
- break;
- }
-
- List<Property> props = dBContext.Properties.Include("PropertyType").Where(x => x.Published
- && x.PropertyType.UsageType == type
- && (x.CutOffDisplayDate == DateTime.MinValue || x.CutOffDisplayDate.Date > DateTime.Now.Date)).OrderByDescending(x => x.DatePublished).Take(4).ToList();
- return GetDisplayDetails(props);
- }
-
- public List<PropertyList> GetPropertyList(int By)
- {
- List<Property> properties = new List<Property>();
- var user = dBContext.Users.Where(u => u.Id == By).FirstOrDefault();
- if (user != null)
- {
- if (user.Role.ToUpper() == "AGENCY" || user.Role.ToUpper() == "AGENT")
- {
- var agent = dBContext.Agents.Where(a => a.UserId == user.Id).FirstOrDefault();
- if (user.Role.ToUpper() == "AGENCY")
- {
- properties = dBContext.Properties.Where(p => p.AgencyId == agent.AgencyId).ToList();
- }
- else
- {
- properties = dBContext.Properties.Where(p => p.AgentId == agent.Id).ToList();
- }
- }
- if (user.Role.ToUpper() == "PRIVATE USER")
- {
- var individual = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
-
- properties = dBContext.Properties.Where(p => p.OwnerId == individual.Id).ToList();
- }
- if (user.Role.ToUpper() == "SUPER ADMIN")
- properties = dBContext.Properties.ToList();
- }
-
- return SetPropertyList(properties);
- }
-
- public List<PropertyList> GetPropertyList()
- {
- return SetPropertyList(dBContext.Properties.Where(x => x.Published).ToList());
- }
-
- private List<PropertyList> SetPropertyList(List<Property> properties)
- {
- List<PropertyList> list = new List<PropertyList>();
-
- foreach (Property p in properties)
- {
- var prop = new PropertyList()
- {
- Id = p.Id,
- Name = string.IsNullOrEmpty(p.PropertyName) ? p.ShortDescription : p.PropertyName,
- Price = p.Price,
- Publish = p.Published ? "Yes" : "No",
- Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description,
- CarouselDescription = string.Format("{0}, {1} <br/>{2}", p.Suburb, p.City, p.AddressOther),
- DateAvailable = p.IsSale ? DateTime.MinValue : p.DateAvailable,
- IsPublished = p.Published
- };
-
- prop.Size = (from u in dBContext.PropertyUserFields
- join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
- where u.PropertyId == p.Id
- && f.FieldName == "Floor Size"
- select u.Value).FirstOrDefault();
-
- //if (!string.IsNullOrEmpty(prop.Size) && prop.Size.EndsWith("2"))
- //{
- // prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
- //}
-
- prop.UsageType = (dBContext.PropertyTypes.Find(p.PropertyTypeId).UsageType == PropertyUsageType.Residential ? "Residential" : "Commercial");
- prop.SaleType = p.IsSale ? "Sale" : "Rental";
-
- list.Add(prop);
- }
-
- return list;
- }
-
- public int NewId()
- {
- // Not sure if properties need it
- return 0;
- }
-
- public void Insert(PropertyContainer items)
- {
- Property property = new Property();
- PropertyType pt = dBContext.PropertyTypes.Find(items.PropertyTypeId);
- if (pt != null)
- {
- if (pt.UsageType == PropertyUsageType.Residential)
- {
- string type = dBContext.PropertyTypes.Find(items.PropertyTypeId).Description;
- if (items.PropertyUserFields.Count > 0)
- {
- string shortDesc = "{0} {1} {2}";
- UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
- var udValue = items.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
- if (udValue != null)
- items.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
- else
- items.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
- }
- else
- {
- items.ShortDescription = type;
- }
- }
- else
- {
- items.ShortDescription = pt.Description;
- }
- }
-
- var images = items.PropertyImages;
- var fields = items.PropertyUserFields;
-
- items.PropertyImages = null;
- items.PropertyUserFields = null;
-
- var individual = dBContext.Individuals.Where(i => i.UserId == items.UserId).FirstOrDefault();
- var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();
-
- foreach( string prop in property.GetAllProperties())
- {
- if (prop != "Item" && prop != "Display")
- property[prop] = items[prop];
- }
-
- if (individual != null)
- property.OwnerId = individual.Id;
- if (agent != null)
- {
- property.AgencyId = agent.AgencyId;
- property.AgentId = agent.Id;
- }
-
- if (property.IsSale)
- property.StatusId = dBContext.Status.Where(s => s.Description == "For Sale" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
- else
- property.StatusId = dBContext.Status.Where(s => s.Description == "For Rent" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
-
- if (!string.IsNullOrEmpty(property.Video))
- property.Video = property.Video.Replace("https://www.youtube.com/watch?v=", "");
-
- if (images != null)
- {
- bool saveFiles = false;
- var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
- if (!string.IsNullOrEmpty(loc))
- {
- saveFiles = true;
- loc += string.Format("\\{0}", property.Id);
- if (Directory.Exists(loc))
- {
- Directory.CreateDirectory(loc);
- }
- }
-
- property.PropertyImages = new List<PropertyImage>();
- var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
- foreach (PropertyImage image in images)
- {
- image.PropertyId = property.Id;
- if (saveFiles)
- {
- string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
- image.Image = path;
- }
- property.PropertyImages.Add(image);
- lastID++;
- }
- }
-
- if (fields != null)
- {
- property.PropertyUserFields = new List<PropertyUserField>();
- foreach (PropertyUserField field in fields)
- {
- field.PropertyId = property.Id;
- property.PropertyUserFields.Add(field);
- }
- }
-
- dBContext.Properties.Add(property);
- Save();
-
- items.Id = property.Id;
- }
-
- public bool MayEdit(int id)
- {
- var hasBidItems = (from b in dBContext.BidItems
- where b.PropertyId == id
- && b.StatusId == 2
- select b).FirstOrDefault();
-
- return (hasBidItems == null) ? true : false;
- }
-
- public void InsertImages(int propertyID, List<PropertyImage> Images)
- {
- if (Images != null)
- {
- var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
-
- bool saveFiles = false;
- var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
- if (!string.IsNullOrEmpty(loc))
- {
- saveFiles = true;
- loc += string.Format("\\{0}", propertyID);
- if (Directory.Exists(loc))
- {
- Directory.CreateDirectory(loc);
- }
- }
-
- foreach (PropertyImage image in Images)
- {
- image.PropertyId = propertyID;
- if (saveFiles)
- {
- string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
- image.Image = path;
- }
- dBContext.PropertyImages.Add(image);
- lastID++;
- Save();
- }
- }
- }
-
- public void InsertFields(int propertyID, List<PropertyUserField> Fields)
- {
- throw new NotImplementedException();
- }
-
- public void PublishProperty(int propertyID)
- {
- var property = dBContext.Properties.Where(p => p.Id == propertyID).FirstOrDefault();
- if (property != null)
- {
- property.Published = true;
- property.DatePublished = DateTime.Now;
- Update(property);
- }
- }
-
- public void UnpublishProperty(int propertyID)
- {
- var property = dBContext.Properties.Where(p => p.Id == propertyID).FirstOrDefault();
- if (property != null)
- {
- property.Published = false;
- property.DatePublished = DateTime.MinValue;
- Update(property);
- }
- }
-
- public List<PropertyAdminContainer> GetAdminProperties(int UserId)
- {
- var user = dBContext.Users.Where(u => u.Id == UserId).FirstOrDefault();
- List<PropertyAdminContainer> returnProps = new List<PropertyAdminContainer>();
- List<Property> props;
- if ((user.Role.ToUpper() == "SUPER ADMIN"))
- {
- props = dBContext.Properties.Include("Owner").Include("PropertyType").ToList();
- }
- else
- {
- var indiv = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
- props = dBContext.Properties.Include("Owner").Include("PropertyType").Where(p => p.OwnerId == indiv.Id).ToList();
- }
-
- foreach (var prop in props)
- {
- var propAdmin = new PropertyAdminContainer()
- {
- Id = prop.Id,
- Owner = prop.Owner?.FullName,
- Property = prop.PropertyName,
- Reference = prop.PropertyRef,
- Unit = prop.Unit,
- Size = (from u in dBContext.PropertyUserFields
- join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
- where u.PropertyId == prop.Id
- && f.FieldName == "Floor Size"
- select u.Value).FirstOrDefault(),
- Price = prop.Price,
- Region = prop.Province,
- Town = prop.City,
- Suburb = prop.Suburb,
- IsPublished = prop.Published,
- Type = prop.PropertyType.UsageType.ToString()
- };
-
- if (prop.StatusId != null)
- {
- propAdmin.Status = dBContext.Status.Where(s => s.Id == prop.StatusId).FirstOrDefault()?.Description;
- }
-
- returnProps.Add(propAdmin);
- }
-
- return returnProps;
- }
-
- public List<string> GetStatuses()
- {
- return dBContext.Status.Where(s => s.StatusType == StatusType.Property).Select(s => s.Description).ToList();
- }
- }
- }
|