123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnivateProperties_API.Containers.Property;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Model.Property;
-
- 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.Where(where).ToList();
- }
-
- public List<Property> GetAll()
- {
- var properties = dBContext.Properties.ToList();
- return properties;
- }
-
- public Property GetDetailed(Func<Property, bool> first)
- {
- var property = dBContext.Properties.FirstOrDefault(first);
- if (property != null)
- {
- GetDetail(ref property);
- }
- return property;
- }
-
- public List<Property> GetDetailedAll()
- {
- var properties = dBContext.Properties.ToList();
- return properties;
- }
-
- private void GetDetail(ref Property property)
- {
- int propID = property.Id;
- var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
- property.Province = dBContext.Provinces.Find(property.ProvinceId);
- property.City = dBContext.Cities.Find(property.CityId);
- property.Suburb = dBContext.Suburbs.Find(property.SuburbId);
- property.DisplayData = new List<PropertyDetailGroup>();
-
- 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)
- {
- var irem = new PropertyDetail()
- {
- Name = val.FieldName,
- Description = val.Description
- };
-
- detailGroup.Values.Add(irem);
-
- if ((val.FieldName == "Erf Size" || val.FieldName == "Floor Size") && val.Value.EndsWith("2"))
- {
- irem.Value = val.Value.Substring(0, val.Value.Length - 1) + "<sup>" + val.Value.Last() + "</sup>";
- }
- else
- irem.Value = val.Value;
- }
-
- property.DisplayData.Add(detailGroup);
- }
- }
- }
-
- public void Insert(Property item)
- {
- PropertyType pt = dBContext.PropertyTypes.Find(item.PropertyTypeId);
- if (pt != null)
- {
- if (pt.UsageType == PropertyUsageType.Residential)
- {
- if (item.PropertyUserFields.Count > 0)
- {
- string shortDesc = "{0} {1} {2}";
- string type = dBContext.PropertyTypes.Find(item.PropertyTypeId).Description;
- UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
- var udValue = item.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
- if (udValue != null)
- item.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
- else
- item.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
- }
- }
- else
- {
- item.ShortDescription = pt.Description;
- }
- }
-
- dBContext.Properties.Add(item);
- Save();
- }
-
- public void Insert(IEnumerable<Property> items)
- {
- foreach (var item in items)
- {
- dBContext.Properties.Add(item);
- }
- Save();
- }
-
- public void Remove(Property item)
- {
- dBContext.Properties.Remove(item);
- Save();
- }
-
- public void Remove(IEnumerable<Property> items)
- {
- foreach (var item in items)
- {
- dBContext.Properties.Remove(item);
- }
- 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)
- {
- dBContext.Entry(item).State = EntityState.Modified;
- 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(string Keyword)
- {
- Keyword = Keyword.ToLower();
-
- List<Property> props = (from p in dBContext.Properties
- join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
- join c in dBContext.Cities on p.CityId equals c.Id
- join s in dBContext.Suburbs on p.SuburbId equals s.Id
- join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
- where EF.Functions.Like(p.PropertyName.ToLower(), $"%{Keyword}%")
- || EF.Functions.Like(pr.Description.ToLower(), $"%{Keyword}%")
- || EF.Functions.Like(c.Description.ToLower(), $"%{Keyword}%")
- || EF.Functions.Like(s.Description.ToLower(), $"%{Keyword}%")
- || EF.Functions.Like(pt.Description.ToLower(), $"%{Keyword}%")
- select p).ToList();
-
- return GetDisplayDetails(props);
- }
-
- public List<PropertyDisplay> GetDisplay(string type, string propertyType, string province, string city, string suburb, string propType)
- {
- List<Property> props;
- PropertyUsageType uType = PropertyUsageType.Both;
-
- if (propertyType != "" && propertyType != "undefined")
- {
- if (propertyType.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();
-
- if (type != "" && type != "undefined")
- {
- if (type.ToUpper() == "SALE")
- props = props.Where(p => p.IsSale).ToList();
- else
- props = props.Where(p => !p.IsSale).ToList();
- }
-
- if (province != "" && province != "undefined" && province.ToUpper() != "ALL")
- {
- props = (from p in props
- join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
- where pp.Description.ToUpper() == province.ToUpper()
- select p).ToList();
- }
-
- if (city != "" && city != "undefined" && city.ToUpper() != "ALL")
- {
- props = (from p in props
- join c in dBContext.Cities on p.CityId equals c.Id
- where c.Description.ToUpper() == city.ToUpper()
- select p).ToList();
- }
-
- if (suburb != "" && suburb != "undefined" && suburb.ToUpper() != "ALL")
- {
- props = (from p in props
- join s in dBContext.Suburbs on p.SuburbId equals s.Id
- where s.Description.ToUpper() == suburb.ToUpper()
- select p).ToList();
- }
- if (propType != "" && propType != "Undefined" && propType.ToUpper() != "ALL")
- {
- var pType = dBContext.PropertyTypes.Where(t => t.Description == propType).FirstOrDefault();
- if (pType != null)
- {
- props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
- }
- }
-
- return GetDisplayDetails(props);
- }
-
- private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
- {
- var properties = new List<PropertyDisplay>();
- foreach (var item in props)
- {
- PropertyDisplay display = new PropertyDisplay
- {
- 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 = (from p in dBContext.Provinces
- where p.Id == item.ProvinceId
- select p.Description).FirstOrDefault(),
- City = (from c in dBContext.Cities
- where c.Id == item.CityId
- select c.Description).FirstOrDefault(),
- Suburb = (from s in dBContext.Suburbs
- where s.Id == item.SuburbId
- select s.Description).FirstOrDefault()
-
- };
-
- if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
- {
- display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
- }
-
- properties.Add(display);
- }
-
- return properties;
- }
- }
- }
|