Exemplo n.º 1
0
        public static PropertiesResultJson Properties(string accountNameKey, List <PropertyModel> propertiesIn, string filter = null)
        {
            var propertyJsonOut = new PropertiesResultJson();

            propertyJsonOut.properties = new List <PropertyJson>();

            foreach (PropertyModel property in propertiesIn)
            {
                var propertyJson = new PropertyJson
                {
                    propertyName    = property.PropertyName,
                    propertyNameKey = property.PropertyNameKey,
                    propertyType    = property.PropertyTypeNameKey,
                    searchField     = property.SearchFieldName
                };

                if (property.PropertyTypeNameKey == "number" && property.Symbol != null)
                {
                    propertyJson.symbol = new PropertySymbolJson
                    {
                        value     = property.Symbol,
                        placement = property.SymbolPlacement
                    };
                }

                if (property.PropertyTypeNameKey == "swatch" && property.Swatches != null)
                {
                    propertyJson.swatches = new List <PropertySwatchesJson>();

                    foreach (PropertySwatchModel swatch in property.Swatches)
                    {
                        propertyJson.swatches.Add(new PropertySwatchesJson
                        {
                            label       = swatch.PropertySwatchLabel,
                            image       = swatch.PropertySwatchImage,
                            imageMedium = swatch.PropertySwatchImageMedium,
                            imageSmall  = swatch.PropertySwatchImageSmall
                        });
                    }
                }

                if (filter == "listingsOnly" && property.Listing)
                {
                    propertyJsonOut.properties.Add(propertyJson);
                }
                else if (filter == null)
                {
                    propertyJsonOut.properties.Add(propertyJson);
                }
            }

            propertyJsonOut.count = propertyJsonOut.properties.Count();

            return(propertyJsonOut);
        }
Exemplo n.º 2
0
        public JsonNetResult GetProductProperties(string type, string filter = null)
        {
            ExecutionType executionType = ExecutionType.local;
            Stopwatch     stopWatch     = new Stopwatch();

            stopWatch.Start();

            //Get the subdomain (if exists) for the api call
            string accountNameKey = Common.GetSubDomain(Request.Url);

            if (String.IsNullOrEmpty(accountNameKey))
            {
                return(new JsonNetResult {
                    Data = "Not found"
                });                                              //return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            List <PropertyModel> properties     = null;
            PropertiesResultJson propertiesJson = null;

            string           localCacheKey    = accountNameKey + ":properties:all";
            PropertyListType propertyListType = PropertyListType.All;

            if (!String.IsNullOrEmpty(type))
            {
                if (type == "listings" || type == "listing" || type == "list")
                {
                    localCacheKey    = accountNameKey + ":properties:listings";
                    propertyListType = PropertyListType.Listings;
                }
                else if (type == "details" || type == "detail")
                {
                    localCacheKey    = accountNameKey + ":properties:details";
                    propertyListType = PropertyListType.Details;
                }
                else if (type == "featured" || type == "feature")
                {
                    if (filter == "listingsOnly")
                    {
                        localCacheKey    = accountNameKey + ":properties:featured:listingsOnly";
                        propertyListType = PropertyListType.Featured;
                    }
                    else
                    {
                        localCacheKey    = accountNameKey + ":properties:featured";
                        propertyListType = PropertyListType.Featured;
                    }
                }
            }

            #region (Plan A) Get json from local cache

            try
            {
                propertiesJson = (PropertiesResultJson)HttpRuntime.Cache[localCacheKey];
            }
            catch (Exception e)
            {
                var error = e.Message;
                //TODO: Log: error message for local cache call
            }

            #endregion

            if (propertiesJson == null)
            {
                #region (Plan B) Get Public json from second layer of Redis Cache

                IDatabase cache = CoreServices.RedisConnectionMultiplexers.RedisMultiplexer.GetDatabase();

                string pathAndQuery = Common.GetApiPathAndQuery(Request.Url);

                string hashApiKey   = accountNameKey + ":apicache";
                string hashApiField = pathAndQuery;

                try
                {
                    var redisApiValue = cache.HashGet(hashApiKey, hashApiField);

                    if (redisApiValue.HasValue)
                    {
                        propertiesJson = JsonConvert.DeserializeObject <PropertiesResultJson>(redisApiValue);
                        executionType  = ExecutionType.redis_secondary;
                    }
                }
                catch
                {
                }


                #endregion

                if (propertiesJson == null)
                {
                    #region (Plan C) Get property data from Redis Cache or WCF and Rebuild

                    properties = DataAccess.Properties.GetProperties(accountNameKey, propertyListType, out executionType);

                    #endregion
                }

                #region Transform into json object, add images & cache locally or locally and radisAPI layer

                if (propertiesJson != null)
                {
                    //Just cache locally (we got json from the api redis layer)
                    HttpRuntime.Cache.Insert(localCacheKey, propertiesJson, null, DateTime.Now.AddMinutes(Common.PropertiesCacheTimeInMinutes), TimeSpan.Zero);
                }
                else if (properties != null)
                {
                    //Transform properties into JSON and cache BOTH locally AND into redis
                    propertiesJson = Transforms.Json.PropertyTransforms.Properties(accountNameKey, properties, filter);
                    HttpRuntime.Cache.Insert(localCacheKey, propertiesJson, null, DateTime.Now.AddMinutes(Common.PropertiesCacheTimeInMinutes), TimeSpan.Zero);
                    try
                    {
                        cache.HashSet(hashApiKey, hashApiField, JsonConvert.SerializeObject(propertiesJson), When.Always, CommandFlags.FireAndForget);
                    }
                    catch
                    {
                    }
                }

                #endregion
            }

            //Add execution data
            stopWatch.Stop();
            propertiesJson.executionType = executionType.ToString();
            propertiesJson.executionTime = stopWatch.Elapsed.TotalMilliseconds + "ms";

            JsonNetResult jsonNetResult = new JsonNetResult();
            jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
            jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
            jsonNetResult.Data = propertiesJson;

            return(jsonNetResult);
        }