Exemplo n.º 1
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);

            ApiResponse <CategoryProductAssociationDTO> response = new ApiResponse <CategoryProductAssociationDTO>();

            CategoryProductAssociationDTO postedItem = null;

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

            CategoryProductAssociation item = new CategoryProductAssociation();

            item.FromDto(postedItem);

            if (id < 1)
            {
                MTApp.CatalogServices.CategoriesXProducts.AddProductToCategory(item.ProductId, item.CategoryId);
                //if (MTApp.CatalogServices.CategoriesXProducts.Create(item))
                //{
                //    id = item.Id;
                //}
            }
            else
            {
                MTApp.CatalogServices.CategoriesXProducts.Update(item);
            }
            CategoryProductAssociation resultItem = MTApp.CatalogServices.CategoriesXProducts.Find(id);

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

            data = MerchantTribe.Web.Json.ObjectToJson(response);
            return(data);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Allows the REST API to create or update a category/product association.
        /// </summary>
        /// <param name="parameters">
        ///     Parameters passed in the URL of the REST API call. Only one parameter is expected, which is
        ///     the ID of the category/product association, but only when updating.
        /// </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 CategoryProductAssociationDTO object</param>
        /// <returns>CategoryProductAssociationDTO - Serialized (JSON) version of the category/product association</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 response = new ApiResponse <CategoryProductAssociationDTO>();

            CategoryProductAssociationDTO postedItem = null;

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

            var item = new CategoryProductAssociation();

            item.FromDto(postedItem);

            if (id < 1)
            {
                item = HccApp.CatalogServices.AddProductToCategory(item.ProductId, item.CategoryId);
            }
            else
            {
                HccApp.CatalogServices.CategoriesXProducts.Update(item);
            }

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

            data = Json.ObjectToJson(response);
            return(data);
        }