public IActionResult UpdateProduct([ModelBinder(typeof(JsonModelBinder <ProductDto>))] Delta <ProductDto> productDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } int id = 0; var product = _productApiService.GetProductBySKU(productDelta.Dto.Sku); //var product = _productApiService.GetProductById(productDelta.Dto.Id); if (product == null) { return(Error(HttpStatusCode.NotFound, "product", "not found")); } id = product.Id; productDelta.Merge(product); product.UpdatedOnUtc = DateTime.UtcNow; product.Id = id; var vendor = _vendorApiService.GetVendorByName(productDelta.Dto.VName); if (vendor != null) { product.VendorId = vendor.Id; } _productService.UpdateProduct(product); UpdateProductAttributes(product, productDelta); UpdateProductPictures(product, productDelta.Dto.Images); UpdateProductTags(product, productDelta.Dto.Tags); UpdateProductManufacturers(product, productDelta.Dto.ManufacturerIds); UpdateAssociatedProducts(product, productDelta.Dto.AssociatedProductIds); // Update the SeName if specified if (productDelta.Dto.SeName != null) { var seName = _urlRecordService.ValidateSeName(product, productDelta.Dto.SeName, product.Name, true); _urlRecordService.SaveSlug(product, seName, 0); } UpdateDiscountMappings(product, productDelta.Dto.DiscountIds); UpdateStoreMappings(product, productDelta.Dto.StoreIds); UpdateAclRoles(product, productDelta.Dto.RoleIds); product.Id = id; _productService.UpdateProduct(product); CustomerActivityService.InsertActivity("UpdateProduct", LocalizationService.GetResource("ActivityLog.UpdateProduct"), product); // Preparing the result dto of the new product var productDto = _dtoHelper.PrepareProductDTO(product); var productsRootObject = new ProductsRootObjectDto(); productsRootObject.Products.Add(productDto); var json = JsonFieldsSerializer.Serialize(productsRootObject, string.Empty); return(new RawJsonActionResult(json)); }
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 product = _productApiService.GetProductBySKU(productCategoryDelta.Dto.ProductSku); if (product == null) { return(Error(HttpStatusCode.NotFound, "product_sku", "not found")); } var categories = _categoryService.GetByProductId(product.Id); //delete for (int i = 0; i < categories.Count; i++) { if (categories[i].Category.CategoryId == null || !productCategoryDelta.Dto.Categories.Contains((int)categories[i].Category.CategoryId) || categories[i].Category.CategoryId == null) { _categoryService.DeleteProductCategory(categories[i]); //activity log CustomerActivityService.InsertActivity("DeleteProductCategoryMapping", LocalizationService.GetResource("ActivityLog.DeleteProductCategoryMapping"), categories[i]); } } //insert for (int i = 0; i < productCategoryDelta.Dto.Categories.Count; i++) { if (!categories.Any(l => l.Id == productCategoryDelta.Dto.Categories[i])) { var cateTemp = _categoryApiService.GetCategoryByAXId(productCategoryDelta.Dto.Categories[i]); if (cateTemp != null) { var temp = new ProductCategory() { Id = 0, ProductId = product.Id, IsFeaturedProduct = productCategoryDelta.Dto.IsFeaturedProduct ?? false, DisplayOrder = productCategoryDelta.Dto.DisplayOrder ?? 0, CategoryId = cateTemp.Id }; _categoryService.InsertProductCategory(temp); //activity log CustomerActivityService.InsertActivity("AddNewProductCategoryMapping", LocalizationService.GetResource("ActivityLog.AddNewProductCategoryMapping"), temp); } } } //var category = _categoryApiService.GetCategoryByAXId(productCategoryDelta.Dto.CategoryIdDynamics.Value); //if (category == null) //{ // return Error(HttpStatusCode.NotFound, "category_id", "not found"); //} //productCategoryDelta.Dto.CategoryId = category.Id; //var mappingsCount = _productCategoryMappingsService.GetMappingsCount(product.Id, category.Id); //if (mappingsCount > 0) //{ // return Error(HttpStatusCode.BadRequest, "product_category_mapping", "already exist"); //} //var newProductCategory = new ProductCategory(); //productCategoryDelta.Merge(newProductCategory); //newProductCategory.ProductId = product.Id; //newProductCategory.CategoryId = category.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("Done")); }