public IActionResult UpdateProductCategoryMapping([ModelBinder(typeof(JsonModelBinder <ProductCategoryMappingDto>))] Delta <ProductCategoryMappingDto> productCategoryDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            if (productCategoryDelta.Dto.CategoryId.HasValue)
            {
                Category category = _categoryApiService.GetCategoryById(productCategoryDelta.Dto.CategoryId.Value);
                if (category == null)
                {
                    return(Error(HttpStatusCode.NotFound, "category_id", "not found"));
                }
            }

            if (productCategoryDelta.Dto.ProductId.HasValue)
            {
                Product product = _productApiService.GetProductById(productCategoryDelta.Dto.ProductId.Value);
                if (product == null)
                {
                    return(Error(HttpStatusCode.NotFound, "product_id", "not found"));
                }
            }

            // We do not need to validate the category id, because this will happen in the model binder using the dto validator.
            int updateProductCategoryId = productCategoryDelta.Dto.Id;

            ProductCategory productCategoryEntityToUpdate = _categoryService.GetProductCategoryById(updateProductCategoryId);

            if (productCategoryEntityToUpdate == null)
            {
                return(Error(HttpStatusCode.NotFound, "product_category_mapping", "not found"));
            }

            productCategoryDelta.Merge(productCategoryEntityToUpdate);

            _categoryService.UpdateProductCategory(productCategoryEntityToUpdate);

            //activity log
            _customerActivityService.InsertActivity("UpdateProdutCategoryMapping",
                                                    string.Format(_localizationService.GetResource("ActivityLog.UpdateProdutCategoryMapping"), productCategoryEntityToUpdate.Id), productCategoryEntityToUpdate);

            ProductCategoryMappingDto updatedProductCategoryDto = productCategoryEntityToUpdate.ToDto();

            var productCategoriesRootObject = new ProductCategoryMappingsRootObject();

            productCategoriesRootObject.ProductCategoryMappingDtos.Add(updatedProductCategoryDto);

            var json = _jsonFieldsSerializer.Serialize(productCategoriesRootObject, string.Empty);

            return(new RawJsonActionResult(json));
        }
Exemplo n.º 2
0
        private void NotifyProductCategoryMappingWebhook(ProductCategory productCategory, string eventName)
        {
            var storeIds = GetStoreIdsForProductCategoryMap(productCategory);

            if (storeIds == null)
            {
                return;
            }

            ProductCategoryMappingDto productCategoryMappingDto = productCategory.ToDto();

            NotifyRegisteredWebHooks(productCategoryMappingDto, eventName, storeIds);
        }
        public IActionResult CreateProductCategoryMapping([ModelBinder(typeof(JsonModelBinder <ProductCategoryMappingDto>))] Delta <ProductCategoryMappingDto> productCategoryDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            Category category = _categoryApiService.GetCategoryById(productCategoryDelta.Dto.CategoryId.Value);

            if (category == null)
            {
                return(Error(HttpStatusCode.NotFound, "category_id", "not found"));
            }

            Product product = _productApiService.GetProductById(productCategoryDelta.Dto.ProductId.Value);

            if (product == null)
            {
                return(Error(HttpStatusCode.NotFound, "product_id", "not found"));
            }

            int mappingsCount = _productCategoryMappingsService.GetMappingsCount(product.Id, category.Id);

            if (mappingsCount > 0)
            {
                return(Error(HttpStatusCode.BadRequest, "product_category_mapping", "already exist"));
            }

            ProductCategory newProductCategory = new ProductCategory();

            productCategoryDelta.Merge(newProductCategory);

            //inserting new category
            _categoryService.InsertProductCategory(newProductCategory);

            // Preparing the result dto of the new product category mapping
            ProductCategoryMappingDto newProductCategoryMappingDto = newProductCategory.ToDto();

            var productCategoryMappingsRootObject = new ProductCategoryMappingsRootObject();

            productCategoryMappingsRootObject.ProductCategoryMappingDtos.Add(newProductCategoryMappingDto);

            var json = _jsonFieldsSerializer.Serialize(productCategoryMappingsRootObject, string.Empty);

            //activity log
            _customerActivityService.InsertActivity("AddNewProductCategoryMapping",
                                                    string.Format(_localizationService.GetResource("ActivityLog.AddNewProductCategoryMapping"), newProductCategory.Id), newProductCategory);

            return(new RawJsonActionResult(json));
        }
Exemplo n.º 4
0
        private void NotifyProductCategoryMappingWebhook(ProductCategory productCategory, string eventName)
        {
            if (!ProductCategoryMapIsForTheCurrentStore(productCategory))
            {
                return;
            }

            ProductCategoryMappingDto productCategoryMappingDto = productCategory.ToDto();

            var storeIds = new List <int> {
                _storeContext.CurrentStore.Id
            };

            NotifyRegisteredWebHooks(productCategoryMappingDto, eventName, storeIds);
        }