Ejemplo n.º 1
0
        /// <summary>
        ///     Allows the REST API to create or update a product property
        /// </summary>
        /// <param name="parameters">
        ///     Parameters passed in the URL of the REST API call. If there is a first parameter found in the
        ///     URL, the method will assume it is the product property ID (bvin) and that this is an update, otherwise it assumes
        ///     to create a product property.
        /// </param>
        /// <param name="querystring">Name/value pairs from the REST API call querystring. This is not used in this method.</param>
        /// <param name="postdata">Serialized (JSON) version of the ProductPropertyDTO object</param>
        /// <returns>ProductPropertyDTO - Serialized (JSON) version of the ProductProperty</returns>
        public override string PostAction(string parameters, NameValueCollection querystring, string postdata)
        {
            var  data = string.Empty;
            var  ids  = FirstParameter(parameters);
            long id   = 0;

            long.TryParse(ids, out id);

            var isValueSetRequest = GetParameterByIndex(1, parameters);


            if (isValueSetRequest.Trim().ToLowerInvariant() == "valuesforproduct")
            {
                // Set Property value Request
                var response = new ApiResponse <bool>();

                var productBvin   = GetParameterByIndex(2, parameters);
                var propertyValue = postdata;
                var property      = HccApp.CatalogServices.ProductProperties.Find(id);
                response.Content = HccApp.CatalogServices.ProductPropertyValues.SetPropertyValue(productBvin, property,
                                                                                                 propertyValue);

                data = Json.ObjectToJson(response);
            }
            else
            {
                // Regular Create or Update Request

                var response = new ApiResponse <ProductPropertyDTO>();
                ProductPropertyDTO postedItem = null;
                try
                {
                    postedItem = Json.ObjectFromJson <ProductPropertyDTO>(postdata);
                }
                catch (Exception ex)
                {
                    response.Errors.Add(new ApiError("EXCEPTION", ex.Message));
                    return(Json.ObjectToJson(response));
                }

                var item = new ProductProperty();
                item.FromDto(postedItem);


                // Check for existing and create base property
                var existing = HccApp.CatalogServices.ProductProperties.FindByName(item.DisplayName);
                if (existing == null)
                {
                    // Create
                    HccApp.CatalogServices.ProductProperties.Create(item);
                    id = item.Id;
                }
                else
                {
                    // Update
                    HccApp.CatalogServices.ProductProperties.Update(item);
                    id = existing.Id;
                }

                var resultItem = HccApp.CatalogServices.ProductProperties.Find(id);
                if (resultItem != null)
                {
                    response.Content = resultItem.ToDto();
                }

                data = Json.ObjectToJson(response);
            }

            return(data);
        }
Ejemplo n.º 2
0
        // Create or Update
        public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata)
        {
            string data = string.Empty;
            string ids  = FirstParameter(parameters);
            long   id   = 0;

            long.TryParse(ids, out id);

            string isValueSetRequest = GetParameterByIndex(1, parameters);


            if (isValueSetRequest.Trim().ToLowerInvariant() == "valuesforproduct")
            {
                // Set Property value Request
                ApiResponse <bool> response = new ApiResponse <bool>();

                string productBvin   = GetParameterByIndex(2, parameters);
                string propertyValue = postdata;
                response.Content = MTApp.CatalogServices.ProductPropertyValues.SetPropertyValue(productBvin, id, propertyValue);

                data = MerchantTribe.Web.Json.ObjectToJson(response);
            }
            else
            {
                // Regular Create or Update Request

                ApiResponse <ProductPropertyDTO> response = new ApiResponse <ProductPropertyDTO>();
                ProductPropertyDTO postedItem             = null;
                try
                {
                    postedItem = MerchantTribe.Web.Json.ObjectFromJson <ProductPropertyDTO>(postdata);
                }
                catch (Exception ex)
                {
                    response.Errors.Add(new ApiError("EXCEPTION", ex.Message));
                    return(MerchantTribe.Web.Json.ObjectToJson(response));
                }

                ProductProperty item = new ProductProperty();
                item.FromDto(postedItem);


                // Check for existing and create base property
                ProductProperty existing = MTApp.CatalogServices.ProductProperties.FindByName(item.DisplayName);
                if (existing == null)
                {
                    // Create
                    MTApp.CatalogServices.ProductProperties.Create(item);
                    id = item.Id;

                    //// Merge Properties
                    //foreach (ProductPropertyChoice ppc in item.Choices)
                    //{
                    //    ppc.PropertyId = item.Id;
                    //    ProductPropertyChoice.Insert(ppc);
                    //}
                }
                else
                {
                    // Update
                    MTApp.CatalogServices.ProductProperties.Update(item);
                    id = existing.Id;

                    //// Merge Properties
                    //if (item.Choices.Count > 0)
                    //{
                    //    List<ProductPropertyChoice> existingChoices = ProductPropertyChoice.FindByPropertyID(existing.Id);
                    //    foreach (ProductPropertyChoice newChoice in item.Choices)
                    //    {
                    //        var existCount = existingChoices.Where(y => y.Id == newChoice.Id).Count();
                    //        if (existCount < 1)
                    //        {
                    //            newChoice.PropertyId = existing.Id;
                    //            ProductPropertyChoice.Insert(newChoice);
                    //        }
                    //        else
                    //        {
                    //            ProductPropertyChoice.Update(newChoice);
                    //        }
                    //    }
                    //}
                }

                ProductProperty resultItem = MTApp.CatalogServices.ProductProperties.Find(id);
                if (resultItem != null)
                {
                    response.Content = resultItem.ToDto();
                }

                data = MerchantTribe.Web.Json.ObjectToJson(response);
            }


            return(data);
        }