コード例 #1
0
        public IHttpActionResult GetCategoryById(int id, string fields = "")
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            Category category = _categoryApiService.GetCategoryById(id);

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

            CategoryDto categoryDto = category.ToDto();

            PrepareDtoAditionalProperties(category, categoryDto);

            var categoriesRootObject = new CategoriesRootObject();

            categoriesRootObject.Categories.Add(categoryDto);

            var json = _jsonFieldsSerializer.Serialize(categoriesRootObject, fields);

            return(new RawJsonActionResult(json));
        }
コード例 #2
0
        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());
            }

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

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

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

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

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

            ProductCategory newProductCategory = new ProductCategory();

            productCategoryDelta.Merge(newProductCategory);

            if (mappingsCount == 0)
            {
                _categoryService.InsertProductCategory(newProductCategory);
                CustomerActivityService.InsertActivity("AddNewProductCategoryMapping", LocalizationService.GetResource("ActivityLog.AddNewProductCategoryMapping"), newProductCategory);
                //return Error(HttpStatusCode.BadRequest, "product_category_mapping", "already exist");
            }
            else
            {
                var mapping = _productCategoryMappingsService.GetMappings(product.Id, category.Id);
                newProductCategory.Id = mapping.FirstOrDefault().Id;
            }

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

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

            var productCategoryMappingsRootObject = new ProductCategoryMappingsRootObject();

            productCategoryMappingsRootObject.ProductCategoryMappingDtos.Add(newProductCategoryMappingDto);

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

            //activity log
            CustomerActivityService.InsertActivity("AddNewProductCategoryMapping", LocalizationService.GetResource("ActivityLog.AddNewProductCategoryMapping"), newProductCategory);

            return(new RawJsonActionResult(json));
        }
コード例 #3
0
        // GET: Categories/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            TempData["active"] = "category";
            if (id == null)
            {
                return(NotFound());
            }

            var category = await _categoryApiService.GetCategoryById((int)id);

            if (category == null)
            {
                return(NotFound());
            }
            return(View(new CategoryUpdateModel {
                Id = category.Id, Name = category.Name
            }));
        }
コード例 #4
0
        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)
            {
                var category = _categoryApiService.GetCategoryById(productCategoryDelta.Dto.CategoryId.Value);
                if (category == null)
                {
                    return(Error(HttpStatusCode.NotFound, "category_id", "not found"));
                }
            }

            if (productCategoryDelta.Dto.ProductId.HasValue)
            {
                var 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.
            var updateProductCategoryId = productCategoryDelta.Dto.Id;

            var 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",
                                                   LocalizationService.GetResource("ActivityLog.UpdateProdutCategoryMapping"), productCategoryEntityToUpdate);

            var updatedProductCategoryDto = productCategoryEntityToUpdate.ToDto();

            var productCategoriesRootObject = new ProductCategoryMappingsRootObject();

            productCategoriesRootObject.ProductCategoryMappingDtos.Add(updatedProductCategoryDto);

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

            return(new RawJsonActionResult(json));
        }
コード例 #5
0
        public IActionResult GetCategoryById(int id, string fields = "")
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var category = _categoryApiService.GetCategoryById(id);

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

            var categoryDto = _dtoHelper.PrepareCategoryDTO(category);

            var categoriesRootObject = new CategoriesRootObject();

            categoriesRootObject.Categories.Add(categoryDto);

            var json = JsonFieldsSerializer.Serialize(categoriesRootObject, fields);

            return(new RawJsonActionResult(json));
        }
コード例 #6
0
        private void HandleStoreMappingEvent(int entityId, string entityName)
        {
            // When creating or editing a category after saving the store mapping the category is not updated
            // so we should listen for StoreMapping update/delete and fire a webhook with the updated entityDto(with correct storeIds).
            if (entityName == "Category")
            {
                var category = _categoryApiService.GetCategoryById(entityId);

                if (category != null)
                {
                    CategoryDto categoryDto = _dtoHelper.PrepareCategoryDTO(category);

                    string webhookEvent = WebHookNames.CategoriesUpdate;

                    if (categoryDto.Deleted == true)
                    {
                        webhookEvent = WebHookNames.CategoriesDelete;
                    }

                    NotifyRegisteredWebHooks(categoryDto, webhookEvent, categoryDto.StoreIds);
                }
            }
            else if (entityName == "Product")
            {
                var product = _productApiService.GetProductById(entityId);

                if (product != null)
                {
                    ProductDto productDto = _dtoHelper.PrepareProductDTO(product);

                    string webhookEvent = WebHookNames.ProductsUpdate;

                    if (productDto.Deleted == true)
                    {
                        webhookEvent = WebHookNames.ProductsDelete;
                    }

                    NotifyRegisteredWebHooks(productDto, webhookEvent, productDto.StoreIds);
                }
            }
        }
コード例 #7
0
        public IActionResult DeleteCategory(int id)
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var categoryToDelete = _categoryApiService.GetCategoryById(id);

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

            _categoryService.DeleteCategory(categoryToDelete);

            //activity log
            CustomerActivityService.InsertActivity("DeleteCategory", LocalizationService.GetResource("ActivityLog.DeleteCategory"), categoryToDelete);

            return(new RawJsonActionResult("{}"));
        }
コード例 #8
0
 public IViewComponentResult Invoke(int categoryId)
 {
     return(View(_categoryApiService.GetCategoryById(categoryId).Result));
 }