Exemple #1
0
        // Create or Update
        public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata)
        {
            string data = string.Empty;
            string bvin = FirstParameter(parameters);
            ApiResponse <ProductReviewDTO> response = new ApiResponse <ProductReviewDTO>();

            ProductReviewDTO postedItem = null;

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

            ProductReview item = new ProductReview();

            item.FromDto(postedItem);

            if (bvin == string.Empty)
            {
                if (MTApp.CatalogServices.ProductReviews.Create(item))
                {
                    bvin = item.Bvin;
                }
            }
            else
            {
                MTApp.CatalogServices.ProductReviews.Update(item);
            }
            ProductReview resultItem = MTApp.CatalogServices.ProductReviews.Find(bvin);

            if (resultItem != null)
            {
                response.Content = resultItem.ToDto();
            }

            data = MerchantTribe.Web.Json.ObjectToJson(response);
            return(data);
        }
Exemple #2
0
        /// <summary>
        ///     Allows the REST API to create or update a product review
        /// </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 review ID (bvin) and that this is an update, otherwise it assumes to
        ///     create a product review.
        /// </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 ProductReviewDTO object</param>
        /// <returns>ProductReviewDTO - Serialized (JSON) version of the product review</returns>
        public override string PostAction(string parameters, NameValueCollection querystring, string postdata)
        {
            // an object used to return the response
            var data = string.Empty;

            // the ID of the product review to update
            var bvin = FirstParameter(parameters);

            // a response object used to send back the new/updated review
            var response = new ApiResponse <ProductReviewDTO>();

            // an object used to house the posted review for parsing
            ProductReviewDTO postedItem = null;

            try
            {
                // load the posted review object to the local REST API object
                postedItem = Json.ObjectFromJson <ProductReviewDTO>(postdata);
            }
            catch (Exception ex)
            {
                // add an error to send back to the calling application
                response.Errors.Add(new ApiError("EXCEPTION", ex.Message));
                return(Json.ObjectToJson(response));
            }

            // create a new review object
            var item = new ProductReview();

            // convert the posted review object for parsing
            item.FromDto(postedItem);

            // if bvin is empty this is a new review
            if (bvin == string.Empty)
            {
                // create the new review
                if (HccApp.CatalogServices.ProductReviews.Create(item))
                {
                    // populate the local bvin object for use in querying for the update later
                    bvin = item.Bvin;
                }
            }
            else
            {
                item.StoreId = HccApp.CurrentRequestContext.CurrentStore.Id;

                // update the review in the data source
                HccApp.CatalogServices.ProductReviews.Update(item);
            }


            // find a fresh instance of the new/updated product review
            var resultItem = HccApp.CatalogServices.ProductReviews.Find(bvin);

            // add the review object to the response
            if (resultItem != null)
            {
                response.Content = resultItem.ToDto();
            }

            // serialize the response for return
            data = Json.ObjectToJson(response);

            // send the response back to the calling application
            return(data);
        }