private void UpdateProductTags(Product product, IReadOnlyCollection <string> productTags) { if (productTags == null) { return; } if (product == null) { throw new ArgumentNullException(nameof(product)); } //Copied from UpdateProductTags method of ProductTagService //product tags var existingProductTags = _productTagService.GetAllProductTagsByProductId(product.Id); var productTagsToRemove = new List <ProductTag>(); foreach (var existingProductTag in existingProductTags) { var found = false; foreach (var newProductTag in productTags) { if (!existingProductTag.Name.Equals(newProductTag, StringComparison.InvariantCultureIgnoreCase)) { continue; } found = true; break; } if (!found) { productTagsToRemove.Add(existingProductTag); } } foreach (var productTag in productTagsToRemove) { //product.ProductTags.Remove(productTag); product.ProductProductTagMappings .Remove(product.ProductProductTagMappings.FirstOrDefault(mapping => mapping.ProductTagId == productTag.Id)); _productService.UpdateProduct(product); } foreach (var productTagName in productTags) { ProductTag productTag; var productTag2 = _productTagService.GetProductTagByName(productTagName); if (productTag2 == null) { //add new product tag productTag = new ProductTag { Name = productTagName }; _productTagService.InsertProductTag(productTag); } else { productTag = productTag2; } if (!_productService.ProductTagExists(product, productTag.Id)) { product.ProductProductTagMappings.Add(new ProductProductTagMapping { ProductTag = productTag }); _productService.UpdateProduct(product); } var seName = _urlRecordService.ValidateSeName(productTag, string.Empty, productTag.Name, true); _urlRecordService.SaveSlug(productTag, seName, 0); } }
private void UpdateProductTags(Product product, List <string> productTags) { if (productTags == null) { return; } if (product == null) { throw new ArgumentNullException("product"); } //product tags var existingProductTags = product.ProductTags.ToList(); var productTagsToRemove = new List <ProductTag>(); foreach (var existingProductTag in existingProductTags) { bool found = false; foreach (string newProductTag in productTags) { if (existingProductTag.Name.Equals(newProductTag, StringComparison.InvariantCultureIgnoreCase)) { found = true; break; } } if (!found) { productTagsToRemove.Add(existingProductTag); } } foreach (var productTag in productTagsToRemove) { product.ProductTags.Remove(productTag); _productService.UpdateProduct(product); } foreach (string productTagName in productTags) { ProductTag productTag; var productTag2 = _productTagService.GetProductTagByName(productTagName); if (productTag2 == null) { //add new product tag productTag = new ProductTag { Name = productTagName }; _productTagService.InsertProductTag(productTag); } else { productTag = productTag2; } if (!product.ProductTagExists(productTag.Id)) { product.ProductTags.Add(productTag); _productService.UpdateProduct(product); } } }
private void UpdateProductTags(Product product, IReadOnlyCollection <string> productTags) { if (productTags == null) { return; } if (product == null) { throw new ArgumentNullException(nameof(product)); } var existingProductTags = _productTagService.GetAllProductTagsByProductId(product.Id); var productTagsToRemove = new List <ProductTag>(); foreach (var existingProductTag in existingProductTags) { var found = false; foreach (var newProductTag in productTags) { if (!existingProductTag.Name.Equals(newProductTag, StringComparison.InvariantCultureIgnoreCase)) { continue; } found = true; break; } if (!found) { productTagsToRemove.Add(existingProductTag); } } try { this._productTagService.UpdateProductTags(product, productTagsToRemove.Select(o => o.Name).ToArray()); foreach (var productTagName in productTags) { ProductTag productTag; var productTag2 = _productTagService.GetProductTagByName(productTagName); if (productTag2 == null) { //add new product tag productTag = new ProductTag { Name = productTagName }; _productTagService.InsertProductTag(productTag); } else { productTag = productTag2; } var seName = _urlRecordService.ValidateSeName(productTag, string.Empty, productTag.Name, true); _urlRecordService.SaveSlug(productTag, seName, 0); //Perform a final check to deal with duplicates etc. var currentProductTags = _productTagService.GetAllProductTagsByProductId(product.Id); if (!currentProductTags.Any(o => o.Id == productTag.Id)) { _productTagService.InsertProductProductTagMapping(new ProductProductTagMapping() { ProductId = product.Id, ProductTagId = productTag.Id }); } } } catch (Exception ex) { throw; } }