public void Update(ProductViewModel productVm) { List <ProductTag> productTags = new List <ProductTag>(); if (!string.IsNullOrEmpty(productVm.Tags)) { string[] tags = productVm.Tags.Split(','); foreach (string t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!_tagRepository.FindAll(x => x.Id == tagId).Any()) { Tag tag = new Tag(); tag.Id = tagId; tag.Name = t; tag.Type = CommonConstants.ProductTag; _tagRepository.Add(tag); } _productTagRepository.RemoveMultiple(_productTagRepository.FindAll(x => x.Id == productVm.Id).ToList()); ProductTag productTag = new ProductTag { TagId = tagId }; productTags.Add(productTag); } } var product = productVm.AddModel(); foreach (var productTag in productTags) { product.ProductTags.Add(productTag); } _productRepository.Update(product); }
public ProductViewModel Add(ProductViewModel productVm) { List <ProductTag> productTags = new List <ProductTag>(); if (!string.IsNullOrEmpty(productVm.Tags)) { string[] tags = productVm.Tags.Split(','); foreach (string t in tags) { var tagId = TextHelper.ToUnsignString(t); if (!_tagRepository.FindAll(x => x.Id == tagId).Any()) { Tag tag = new Tag { Id = tagId, Name = t, Type = CommonConstants.ProductTag }; _tagRepository.Add(tag); } ProductTag productTag = new ProductTag { TagId = tagId }; productTags.Add(productTag); } } var product = productVm.AddModel(); foreach (var productTag in productTags) { product.ProductTags.Add(productTag); } _productRepository.Add(product); return(productVm); }