public IActionResult Create(ProductForm model) { if (!ModelState.IsValid) { return new BadRequestObjectResult(ModelState); } var product = new Product { Name = model.Product.Name, SeoTitle = StringHelper.ToUrlFriendly(model.Product.Name), ShortDescription = model.Product.ShortDescription, Description = model.Product.Description, Specification = model.Product.Specification, Price = model.Product.Price, OldPrice = model.Product.OldPrice, IsPublished = model.Product.IsPublished, BrandId = model.Product.BrandId, HasOptions = model.Product.Variations.Any() ? true : false, IsVisibleIndividually = true }; foreach (var option in model.Product.Options) { foreach (var value in option.Values) { product.AddOptionValue(new ProductOptionValue { Value = value, OptionId = option.Id }); } } foreach (var attribute in model.Product.Attributes) { var attributeValue = new ProductAttributeValue { AttributeId = attribute.Id, Value = attribute.Value }; product.AddAttributeValue(attributeValue); } foreach (var categoryId in model.Product.CategoryIds) { var productCategory = new ProductCategory { CategoryId = categoryId }; product.AddCategory(productCategory); } SaveProductImages(model, product); MapProductVariationVmToProduct(model, product); productService.Create(product); return Ok(); }
public void AddCategory(ProductCategory category) { category.Product = this; Categories.Add(category); }
private void AddOrDeleteCategories(ProductForm model, Product product) { foreach (var categoryId in model.Product.CategoryIds) { if (product.Categories.Any(x => x.CategoryId == categoryId)) { continue; } var productCategory = new ProductCategory { CategoryId = categoryId }; product.AddCategory(productCategory); } var deletedProductCategories = product.Categories.Where(productCategory => !model.Product.CategoryIds.Contains(productCategory.CategoryId)) .ToList(); foreach (var deletedProductCategory in deletedProductCategories) { deletedProductCategory.Product = null; product.Categories.Remove(deletedProductCategory); productCategoryRepository.Remove(deletedProductCategory); } }