| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817 | #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")
                .Include("Province")
                .Include("City")
                .Include("Suburb")
                .Where(where).ToList();
        }
        public List<Property> GetAll()
        {
            var properties = dBContext.Properties.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("Status").Where(p => p.Id == id).FirstOrDefault();            
            if (property != null)
            {
                return GetDetail(property, detailed);
            }
            return null;
        }
        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.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>();            
            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)
                        {
                            var item = new PropertyDetail()
                            {
                                Name = val.FieldName,
                                Description = val.Description
                            };
                            detailGroup.Values.Add(item);
                            if (!string.IsNullOrEmpty(val.Value) && (val.FieldName == "Erf Size" || val.FieldName == "Floor Size") && val.Value.EndsWith("2"))
                            {
                                item.Value = val.Value.Substring(0, val.Value.Length - 1) + "<sup>" + val.Value.Last() + "</sup>";
                            }
                            else
                                item.Value = val.Value;
                        }
                        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();
            foreach (string prop in property.GetAllProperties())
            {
                if (prop != "Item" && prop != "Display")
                    propertyDetails[prop] = property[prop];
            }            
            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>();
            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 (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 (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;
            dBContext.Entry(property).State = EntityState.Modified;            
            #region Insert New UDFs
            var lastFieldID = dBContext.GetMaxID("PropertyUserFields");
            foreach (var propGroup in item.PropertyOverviewFields)
            {
                foreach (var field in propGroup.Fields)
                {
                    if (field.ItemId == 0)
                    {
                        lastFieldID++;
                        var propertyField = new PropertyUserField()
                        {
                            Id = lastFieldID,
                            PropertyId = property.Id,
                            UserDefinedFieldId = field.Id,
                            Value = field.Value
                        };
                        dBContext.Add(propertyField);                        
                    }
                    else
                    {
                        var propertyField = dBContext.PropertyUserFields.Where(p => p.Id == field.ItemId).FirstOrDefault();
                        if (propertyField != null)
                        {
                            if (string.IsNullOrEmpty(field.Value))
                                propertyField.IsDeleted = true;
                            else
                                propertyField.Value = field.Value;
                            dBContext.Entry(propertyField).State = EntityState.Modified;                            
                        }
                    }
                }
            }
            foreach (var propGroup in item.PropertyFields)
            {
                foreach (var field in propGroup.Fields)
                {
                    if (field.ItemId == 0)
                    {
                        lastFieldID++;
                        var propertyField = new PropertyUserField()
                        {
                            Id = lastFieldID,
                            PropertyId = property.Id,
                            UserDefinedFieldId = field.Id,
                            Value = field.Value
                        };
                        dBContext.Add(propertyField);                        
                    }
                    else
                    {
                        var propertyField = dBContext.PropertyUserFields.Where(p => p.Id == field.ItemId).FirstOrDefault();
                        if (propertyField != null)
                        {
                            if (string.IsNullOrEmpty(field.Value))
                                propertyField.IsDeleted = true;
                            else
                                propertyField.Value = field.Value;
                            dBContext.Entry(propertyField).State = EntityState.Modified;                            
                        }
                    }
                }
            }
            #endregion
            #region Update Images            
            if (item.PropertyImages != null)
            {
                foreach (var image in item.PropertyImages)
                {
                    var propImage = dBContext.PropertyImages.Where(pi => pi.Id == image.Id).FirstOrDefault();
                    if (propImage != null)
                    {
                        propImage.IsDefault = image.IsDefault;
                        propImage.IsDeleted = image.IsDeleted;
                        dBContext.Entry(propImage).State = EntityState.Modified;
                    }
                }
            }
            #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 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();
                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
                             join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
                             where pp.Description.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
                             join c in dBContext.Cities on p.CityId equals c.Id
                             where c.Description.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
                             join s in dBContext.Suburbs on p.SuburbId equals s.Id
                             where s.Description.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>();
            foreach (var item in props)
            {                
                PropertyDisplay display = new PropertyDisplay
                {          
                    DateAvailable = item.DateAvailable,
                    Available = item.DateAvailable.Date > DateTime.Now.Date ? string.Format("Available form: {0: dd MMM yyyy}", item.DateAvailable) : "Available Now",
                    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(),
                    Price = item.Price,
                    DateCreated = item.Created,
                    PropertyUsageType = (from p in dBContext.PropertyTypes
                                         where p.Id == item.PropertyTypeId
                                         select p.UsageType.ToString()).FirstOrDefault(),
                    HasPendingOffer = !MayEdit(item.Id)
                };
                if (display.DisplayImage != null && !display.DisplayImage.StartsWith("data:image"))
                {
                    display.DisplayImage = ImageFormatter.ImageToBase64(display.DisplayImage);
                }
                if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
                {
                    display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
                }
                if (display.HasPendingOffer)
                    display.Available = "Offer Pending";
#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().OrderByDescending(x => x.Created).Take(3).ToList();
            return GetDisplayDetails(props);
        }
        public List<PropertyList> GetPropertyList(string Type, int By)
        {
            var individual = dBContext.Individuals.Where(x => x.UserId == By).FirstOrDefault();
            var agent = dBContext.Agents.Where(x => x.UserId == By).FirstOrDefault();
            List<Property> properties = new List<Property>();
            if (Type.ToUpper() == "MY")
            {                
                if (individual != null)
                    properties = dBContext.Properties.Include("City").Include("Suburb").Where(x => x.OwnerId == individual.Id).ToList();
                if (agent != null)
                    properties = dBContext.Properties.Include("City").Include("Suburb").Where(x => x.AgentId == agent.Id).ToList();
            }
            else if (Type.ToUpper() == "ADMIN")
            {
                if (individual != null)
                    properties = dBContext.Properties.Include("City").Include("Suburb").Where(x => x.OwnerId == individual.Id).ToList();
                if (agent != null)
                    properties = dBContext.Properties.Include("City").Include("Suburb").Where(x => x.AgencyId == agent.AgencyId).ToList();
            }
            else if (Type.ToUpper() == "SUPERADMIN")
            {
                properties = dBContext.Properties.Include("City").Include("Suburb").ToList();
            }
            
            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 = string.Format("R {0:n}", p.Price),
                    Publish = p.Published.ToString(),
                    Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description,
                    CarouselDescription = string.Format("{0}, {1} <br/>{2}", p.Suburb.Description, p.City.Description, p.AddressLine3),
                    DateAvailable = p.DateAvailable
                };
                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;
            }
            property.Video = property.Video.Replace("https://www.youtube.com/watch?v=", "");
            if (images != null)
            {
                var lastID = dBContext.GetMaxID("PropertyImages");
                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>();
                foreach (PropertyImage image in images)
                {
                    lastID++;
                    image.Id = lastID;
                    image.PropertyId = property.Id;
                    if (saveFiles)
                    {
                        string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
                        image.Image = path;
                    }
                    property.PropertyImages.Add(image);
                }
            }
            if (fields != null)
            {
                var lastID = dBContext.GetMaxID("PropertyUserFields");
                property.PropertyUserFields = new List<PropertyUserField>();
                foreach (PropertyUserField field in fields)
                {
                    lastID++;
                    field.Id = lastID;
                    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.GetMaxID("PropertyImages");
                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)
                {
                    lastID++;
                    image.Id = lastID;
                    image.PropertyId = propertyID;
                    if (saveFiles)
                    {
                        string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
                        image.Image = path;
                    }
                    dBContext.PropertyImages.Add(image);
                    Save();
                }
            }
        }
        public void InsertFields(int propertyID, List<PropertyUserField> Fields)
        {
            throw new NotImplementedException();
        }
    }
}
 |