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); } }
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 }; foreach (var option in model.Product.Options) { foreach (var value in option.Values) { product.AddOptionValue(new ProductOptionValue { Value = value, OptionId = option.Id }); } } MapProductVariationVmToProduct(model, product); 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); productService.Create(product); return(Ok()); }
static void ApplyCategories(Product originalProduct, Product copiedProduct) { foreach (var productCategory in originalProduct.ProductCategories) { copiedProduct.AddCategory(productCategory.Category); } }
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 }; foreach (var categoryId in model.Product.CategoryIds) { var productCategory = new ProductCategory { CategoryId = categoryId }; product.AddCategory(productCategory); } SaveProductImages(model, product); productService.Create(product); return(Ok()); }
void ApplyCategories(Product originalProduct, Product copiedProduct) { foreach (var productCategory in originalProduct.ProductCategories) { var position = productCategoryOrder.NextPosition; copiedProduct.AddCategory(productCategory.Category, position); } }
public void Should_delete_categories_that_are_no_longer_required() { var category11 = new Category { Id = 11 }; categoryRepository.Stub(r => r.GetById(11)).Return(category11); var category14 = new Category { Id = 14 }; var category15 = new Category { Id = 15 }; var product = new Product(); product.AddCategory(category14, 0); product.AddCategory(category15, 0); context.SetProduct(product); context.ProductViewData.CategoryIds.Add(11); contributor.ContributeTo(context); product.ProductCategories.Count.ShouldEqual(1); product.ProductCategories[0].Category.Id.ShouldEqual(11); }
public void Should_delete_categories_that_are_no_longer_required() { var category11 = new Category { Id = 11 }; categoryRepository.Stub(r => r.GetById(11)).Return(category11); var category14 = new Category { Id = 14 }; var category15 = new Category { Id = 15 }; var product = new Product(); product.AddCategory(category14); product.AddCategory(category15); context.SetProduct(product); context.ProductViewData.CategoryIds.Add(11); contributor.ContributeTo(context); product.ProductCategories.Count.ShouldEqual(1); product.ProductCategories[0].Category.Id.ShouldEqual(11); }
private static void AddProductCategories(CreateProductCommand request, Product product) { foreach (var categoryId in request.CategoryIds) { var productCategory = new ProductCategory { CategoryId = categoryId }; product.AddCategory(productCategory); } }
public async Task <IActionResult> Post(ProductIndexViewModel model) { if (ModelState.IsValid) { if (model.CoverImage != null) { var folder = "Images/Product/"; model.CoverImageUrl = await UploadImage(folder, model.CoverImage); } var product = new Product { Id = model.Id, Name = model.Name, Slug = model.Slug, ShortDescription = model.ShortDescription, Description = model.Description, Specification = model.Specification, Price = model.Price, OldPrice = model.OldPrice, SpecialPrice = model.SpecialPrice, SpecialPriceStart = model.SpecialPriceStart, SpecialPriceEnd = model.SpecialPriceEnd, IsFeatured = model.IsFeatured, IsCallForPricing = model.IsCallForPricing, IsAllowToOrder = model.IsAllowToOrder, BrandId = model.BrandId, TaxClassId = model.TaxClassId, StockTrackingIsEnabled = model.StockTrackingIsEnabled, CoverImageUrl = model.CoverImageUrl, GenderType = model.GenderType, UnitType = model.UnitType }; foreach (var categoryId in model.CategoryIds) { var productCategory = new ProductCategory { CategoryId = categoryId }; product.AddCategory(productCategory); } await _productRepository.AddAsync(product); TempData["SM"] = "Вы успешно создали."; return(RedirectToAction("Index", "Product")); } return(View(model)); }
public void Should_add_new_categories_remove_old_ones_and_leave_existing() { var category14 = new Category { Id = 14 }; // delete this one var category15 = new Category { Id = 15 }; // leave this one var category16 = new Category { Id = 16 }; // add this one categoryRepository.Stub(r => r.GetById(16)).Return(category16); var product = new Product(); product.AddCategory(category14, 0); product.AddCategory(category15, 0); context.SetProduct(product); context.ProductViewData.CategoryIds.Add(15); context.ProductViewData.CategoryIds.Add(16); contributor.ContributeTo(context); product.ProductCategories.Count.ShouldEqual(2); product.ProductCategories[0].Category.ShouldBeTheSameAs(category15); product.ProductCategories[1].Category.ShouldBeTheSameAs(category16); }
public void Should_add_new_categories_remove_old_ones_and_leave_existing() { var category14 = new Category { Id = 14 }; // delete this one var category15 = new Category { Id = 15 }; // leave this one var category16 = new Category { Id = 16 }; // add this one categoryRepository.Stub(r => r.GetById(16)).Return(category16); var product = new Product(); product.AddCategory(category14); product.AddCategory(category15); context.SetProduct(product); context.ProductViewData.CategoryIds.Add(15); context.ProductViewData.CategoryIds.Add(16); contributor.ContributeTo(context); product.ProductCategories.Count.ShouldEqual(2); product.ProductCategories[0].Category.ShouldBeTheSameAs(category15); product.ProductCategories[1].Category.ShouldBeTheSameAs(category16); }
public void lazy_loading_enable_should_work_on_spesific_uow_scope() { int productId; using (IUnitOfWorkCompleteHandle uow = _unitOfWorkManager.Begin()) { var product = new Product("Elma"); product.AddCategory("Armut"); product.AddCategory("Şeftali"); productId = _productRepository.InsertAndGetId(product); uow.Complete(); } using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { Product product = _productRepository.GetAll().First(x => x.Id == productId); product.ProductCategories.Count.ShouldBe(2); uow.Complete(); } }
public void Should_be_able_to_create_a_product() { var product = new Product { Name = "Sophie", Description = "A nice sloop", Price = new Money(86.22M), IsActive = true, Position = 1 }; var size = new Size { Name = "10cm" }; var image = new Image { Description = "sophie1.jpg", FileName = new Guid("3DF66C48-ED24-4004-A258-8CD9D56A18C6") }; InSession(session => { var category = session.Get <Category>(categoryId); using (DomainEvent.TurnOff()) { product.AddSize(size); } product.AddCategory(category, 0); product.AddProductImage(image, 1); session.SaveOrUpdate(product); }); // now delete the image InSession(session => { var sameProduct = session.Get <Product>(product.Id); var productImage = sameProduct.ProductImages[0]; sameProduct.ProductImages.Remove(productImage); }); }
public void Should_be_able_to_create_a_product() { var product = new Product { Name = "Sophie", Description = "A nice sloop", Price = new Money(86.22M), IsActive = true, Position = 1 }; var size = new Size { Name = "10cm" }; var image = new Image { Description = "sophie1.jpg", FileName = new Guid("3DF66C48-ED24-4004-A258-8CD9D56A18C6") }; InSession(session => { var category = session.Get<Category>(categoryId); using (DomainEvent.TurnOff()) { product.AddSize(size); } product.AddCategory(category); product.AddProductImage(image, 1); session.SaveOrUpdate(product); }); // now delete the image InSession(session => { var sameProduct = session.Get<Product>(product.Id); var productImage = sameProduct.ProductImages[0]; sameProduct.ProductImages.Remove(productImage); }); }
static Product CreateProduct(Category category) { var product = new Product { Id = 144, Name = "Super Widget", Description = "Some product description", Price = new Money(34.56M), Position = 6, Weight = 345, IsActive = true }; product.AddSize(new Size { Id = 21, IsActive = true, IsInStock = true, Name = "Small" }); product.AddSize(new Size { Id = 22, IsActive = true, IsInStock = true, Name = "Medium" }); product.AddCategory(category, 0); product.AddProductImage(new Image { Id = 3 }, 1); return(product); }
/// <summary> /// Updates an existing product. /// A field is only updated if the CSV field is non-empty. /// Existing Product Categories are kept, and new Categories are added from the CSV file. /// Existing Product Photos are kept, and new Photos are added. /// </summary> /// <param name="p"></param> /// <param name="csvProduct"></param> private void UpdateProduct(Product p, CsvProductInfo csvProduct) { if (NotEmpty(csvProduct.Name)) { p.Name = csvProduct.Name; } if (NotEmpty(csvProduct.Sku)) { p.Sku = csvProduct.Sku; } if (NotEmpty(csvProduct.UrlName)) { p.Slug = csvProduct.UrlName.IsValidSlug() ? csvProduct.UrlName : csvProduct.UrlName.CreateSlug(); } if (csvProduct.Price.HasValue) { p.Price = csvProduct.Price; } if (csvProduct.Weight.HasValue) { p.Weight = csvProduct.Weight; } if (NotEmpty(csvProduct.SeoTitle)) { p.SeoTitle = csvProduct.SeoTitle; } if (NotEmpty(csvProduct.SeoDescription)) { p.SeoDescription = csvProduct.SeoDescription; } if (NotEmpty(csvProduct.SeoKeywords)) { p.SeoKeywords = csvProduct.SeoKeywords; } if (csvProduct.IsActive.HasValue) { p.IsActive = csvProduct.IsActive; } if (csvProduct.TaxableItem.HasValue) { p.IsTaxable = csvProduct.TaxableItem; } if (csvProduct.ShowPrice.HasValue) { p.IsPriceDisplayed = csvProduct.ShowPrice; } if (csvProduct.AvailableForPurchase.HasValue) { p.IsAvailableForPurchase = csvProduct.AvailableForPurchase; } if (csvProduct.EnableInventoryManagement.HasValue) { p.InventoryIsEnabled = csvProduct.EnableInventoryManagement; } p.InventoryQtyInStock = csvProduct.StockLevel.HasValue ? csvProduct.StockLevel : null; if (csvProduct.AllowNegativeStock.HasValue) { p.InventoryAllowNegativeStockLevel = csvProduct.AllowNegativeStock; } if (!p.Slug.IsValidSlug()) { csvLine.Status = ProductImportStatus.Error; csvLine.StatusMsg += string.Format(@"Invalid Slug '{0}' must match the pattern '{1}'", p.Slug, RegexPatterns.IsValidSlug); return; } if (!string.IsNullOrEmpty(p.Sku) && !p.Sku.IsValidSku()) { csvLine.Status = ProductImportStatus.Error; csvLine.StatusMsg += string.Format(@"Invalid Sku '{0}' must match the pattern '{1}'", p.Sku, RegexPatterns.IsValidSku); return; } //--- Digital File if (NotEmpty(csvProduct.DigitalFilename)) { if (NotEmpty(p.DigitalFilename)) { File.Delete(Path.Combine(productFilesFolderFileRoot, p.DigitalFilename)); } ImportDigitalFile(p, csvProduct.DigitalFilename); } //--- Descriptors (update/add if different, but preserve existing) using (esTransactionScope transaction = new esTransactionScope()) { var existingDescriptors = p.GetProductDescriptors(); var newDescriptors = new Dictionary <int, DescriptorInfo>(); for (short i = 0; i < 5; i++) { //var descriptor = (i < existingDescriptors.Count) ? existingDescriptors[i] : new ProductDescriptor(); //descriptor.SortOrder = i; //newDescriptors[i] = descriptor; if (i < existingDescriptors.Count) { newDescriptors[i] = new DescriptorInfo() { Name = existingDescriptors[i].Name, Text = existingDescriptors[i].Text }; } else { newDescriptors[i] = new DescriptorInfo(); } } if (NotEmpty(csvProduct.Desc1Name)) { newDescriptors[0].Name = csvProduct.Desc1Name; } if (NotEmpty(csvProduct.Desc2Name)) { newDescriptors[1].Name = csvProduct.Desc2Name; } if (NotEmpty(csvProduct.Desc3Name)) { newDescriptors[2].Name = csvProduct.Desc3Name; } if (NotEmpty(csvProduct.Desc4Name)) { newDescriptors[3].Name = csvProduct.Desc4Name; } if (NotEmpty(csvProduct.Desc5Name)) { newDescriptors[4].Name = csvProduct.Desc5Name; } if (NotEmpty(csvProduct.Desc1Html)) { newDescriptors[0].Text = csvProduct.Desc1Html; } if (NotEmpty(csvProduct.Desc2Html)) { newDescriptors[1].Text = csvProduct.Desc2Html; } if (NotEmpty(csvProduct.Desc3Html)) { newDescriptors[2].Text = csvProduct.Desc3Html; } if (NotEmpty(csvProduct.Desc4Html)) { newDescriptors[3].Text = csvProduct.Desc4Html; } if (NotEmpty(csvProduct.Desc5Html)) { newDescriptors[4].Text = csvProduct.Desc5Html; } p.ProductDescriptorCollectionByProductId.MarkAllAsDeleted(); for (short i = 0; i < newDescriptors.Count; i++) { var descr = newDescriptors[i]; if (NotEmpty(descr.Name) || NotEmpty(descr.Text)) { AddProductDescriptor(p, i, descr.Name, descr.Text); } } transaction.Complete(); } //--- Categories (add new, but don't delete existing) List <Category> existingProductCategories = p.GetCategories(true); List <string> newCategoryNames = csvProduct.CategoryNames.ToList(",", true); newCategoryNames.RemoveAll(x => existingProductCategories.Exists(c => c.Name == x)); foreach (string newCat in newCategoryNames) { var c = Category.GetByName(storeId, newCat) ?? CreateCategory(newCat); p.AddCategory(c); } //--- Photos (add new, but don't delete existing) IEnumerable <string> importPhotoFiles = GetPhotoFilePathsForImport(p, csvProduct); foreach (string filepath in importPhotoFiles) { AddPhoto(p, filepath); } p.Save(); }
/// <summary> /// Replaces/overwrites fields on an existing Product. /// ALL matching fields are replaced/overwritten with the values from the CSV file (including empty fields). /// Existing Product Categories are first DELETED, and then re-added from the CSV file. /// Existing Product Photos are DELETED, and then re-added from the CSV. /// </summary> /// <param name="p"></param> /// <param name="csvProduct"></param> private void ReplaceProduct(Product p, CsvProductInfo csvProduct) { p.Name = csvProduct.Name; p.Sku = csvProduct.Sku; p.Slug = csvProduct.UrlName.IsValidSlug() ? csvProduct.UrlName : csvProduct.UrlName.CreateSlug(); p.Price = csvProduct.Price; p.Weight = csvProduct.Weight; p.SeoTitle = csvProduct.SeoTitle; p.SeoDescription = csvProduct.SeoDescription; p.SeoKeywords = csvProduct.SeoKeywords; p.IsActive = csvProduct.IsActive.GetValueOrDefault(true); p.IsTaxable = csvProduct.TaxableItem.GetValueOrDefault(true); p.IsPriceDisplayed = csvProduct.ShowPrice.GetValueOrDefault(true); p.IsAvailableForPurchase = csvProduct.AvailableForPurchase.GetValueOrDefault(true); p.InventoryIsEnabled = csvProduct.EnableInventoryManagement.GetValueOrDefault(false); p.InventoryQtyInStock = csvProduct.StockLevel; p.InventoryAllowNegativeStockLevel = csvProduct.AllowNegativeStock.GetValueOrDefault(false); if (!p.Slug.IsValidSlug()) { csvLine.Status = ProductImportStatus.Error; csvLine.StatusMsg += string.Format(@"Invalid Slug '{0}' must match the pattern '{1}'", p.Slug, RegexPatterns.IsValidSlug); return; } if (!string.IsNullOrEmpty(p.Sku) && !p.Sku.IsValidSku()) { csvLine.Status = ProductImportStatus.Error; csvLine.StatusMsg += string.Format(@"Invalid Sku '{0}' must match the pattern '{1}'", p.Sku, RegexPatterns.IsValidSku); return; } // save now so we can get an ID for new products p.Save(); //--- Digital File if (NotEmpty(p.DigitalFilename)) { File.Delete(Path.Combine(productFilesFolderFileRoot, p.DigitalFilename)); p.DigitalFilename = ""; p.DigitalFileDisplayName = ""; p.DeliveryMethodId = (short)ProductDeliveryMethod.Shipped; } ImportDigitalFile(p, csvProduct.DigitalFilename); //--- Descriptors (delete and re-add) using (esTransactionScope transaction = new esTransactionScope()) { p.ProductDescriptorCollectionByProductId.MarkAllAsDeleted(); AddProductDescriptor(p, 1, csvProduct.Desc1Name, csvProduct.Desc1Html); // always add the 1st tab if (!string.IsNullOrEmpty(csvProduct.Desc2Name)) { AddProductDescriptor(p, 2, csvProduct.Desc2Name, csvProduct.Desc2Html); } if (!string.IsNullOrEmpty(csvProduct.Desc3Name)) { AddProductDescriptor(p, 3, csvProduct.Desc3Name, csvProduct.Desc3Html); } if (!string.IsNullOrEmpty(csvProduct.Desc4Name)) { AddProductDescriptor(p, 4, csvProduct.Desc4Name, csvProduct.Desc4Html); } if (!string.IsNullOrEmpty(csvProduct.Desc5Name)) { AddProductDescriptor(p, 5, csvProduct.Desc5Name, csvProduct.Desc5Html); } transaction.Complete(); } //--- Categories (delete and then re-associate/add) p.ProductCategoryCollectionByProductId.MarkAllAsDeleted(); // remove existing product categories //p.ProductCategoryCollectionByProductId.Save(); List <string> newCategoryNames = csvProduct.CategoryNames.ToList(",", true); foreach (string newCat in newCategoryNames) { var c = Category.GetByName(storeId, newCat) ?? CreateCategory(newCat); p.AddCategory(c); } //--- Photos (delete and then re-add) p.DeleteAllPhotos(productPhotoFolderFileRoot); IEnumerable <string> importPhotoFiles = GetPhotoFilePathsForImport(p, csvProduct); foreach (string filepath in importPhotoFiles) { AddPhoto(p, filepath); } p.Save(); }
public async Task <IActionResult> Post(ProductForm model) { if (!ModelState.IsValid) { return(new BadRequestObjectResult(ModelState)); } var currentUser = await _workContext.GetCurrentUser(); var product = new Product { Name = model.Product.Name, SeoTitle = model.Product.Name.ToUrlFriendly(), ShortDescription = model.Product.ShortDescription, Description = model.Product.Description, Specification = model.Product.Specification, Price = model.Product.Price, OldPrice = model.Product.OldPrice, SpecialPrice = model.Product.SpecialPrice, SpecialPriceStart = model.Product.SpecialPriceStart, SpecialPriceEnd = model.Product.SpecialPriceEnd, IsPublished = model.Product.IsPublished, IsFeatured = model.Product.IsFeatured, IsCallForPricing = model.Product.IsCallForPricing, IsAllowToOrder = model.Product.IsAllowToOrder, BrandId = model.Product.BrandId, HasOptions = model.Product.Variations.Any() ? true : false, IsVisibleIndividually = true, CreatedBy = currentUser }; if (!User.IsInRole("admin")) { product.VendorId = currentUser.VendorId; } if (model.Product.IsOutOfStock) { product.StockQuantity = 0; } else { product.StockQuantity = null; } var optionIndex = 0; foreach (var option in model.Product.Options) { product.AddOptionValue(new ProductOptionValue { OptionId = option.Id, Value = JsonConvert.SerializeObject(option.Values), SortIndex = optionIndex }); optionIndex++; } 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); } SaveProductMedias(model, product); MapProductVariationVmToProduct(model, product); MapProductLinkVmToProduct(model, product); _productService.Create(product); return(Ok()); }
public async Task <IActionResult> Post(ProductForm model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUser = await _workContext.GetCurrentUser(); var product = new Product { Name = model.Product.Name, Slug = model.Product.Slug, MetaTitle = model.Product.MetaTitle, MetaKeywords = model.Product.MetaKeywords, MetaDescription = model.Product.MetaDescription, Sku = model.Product.Sku, Gtin = model.Product.Gtin, ShortDescription = model.Product.ShortDescription, Description = model.Product.Description, Specification = model.Product.Specification, Price = model.Product.Price, OldPrice = model.Product.OldPrice, SpecialPrice = model.Product.SpecialPrice, SpecialPriceStart = model.Product.SpecialPriceStart, SpecialPriceEnd = model.Product.SpecialPriceEnd, IsPublished = model.Product.IsPublished, IsFeatured = model.Product.IsFeatured, IsCallForPricing = model.Product.IsCallForPricing, IsAllowToOrder = model.Product.IsAllowToOrder, BrandId = model.Product.BrandId, TaxClassId = model.Product.TaxClassId, StockTrackingIsEnabled = model.Product.StockTrackingIsEnabled, HasOptions = model.Product.Variations.Any() ? true : false, IsVisibleIndividually = true, CreatedBy = currentUser, LatestUpdatedBy = currentUser }; if (!User.IsInRole("admin")) { product.VendorId = currentUser.VendorId; } var optionIndex = 0; foreach (var option in model.Product.Options) { product.AddOptionValue(new ProductOptionValue { OptionId = option.Id, DisplayType = option.DisplayType, Value = JsonConvert.SerializeObject(option.Values), SortIndex = optionIndex }); optionIndex++; } 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); } await SaveProductMedias(model, product); MapProductVariationVmToProduct(currentUser, model, product); MapProductLinkVmToProduct(model, product); var productPriceHistory = CreatePriceHistory(currentUser, product); product.PriceHistories.Add(productPriceHistory); _productService.Create(product); return(CreatedAtAction(nameof(Get), new { id = product.Id }, null)); }
public async Task <IActionResult> Post(ProductForm model) { if (!ModelState.IsValid) { return(new BadRequestObjectResult(ModelState)); } var currentUser = await _workContext.GetCurrentUser(); var product = new Product { Name = model.Product.Name, SeoTitle = model.Product.Slug, ShortDescription = model.Product.ShortDescription, Description = model.Product.Description, Specification = model.Product.Specification, Price = model.Product.Price, OldPrice = model.Product.OldPrice, SpecialPrice = model.Product.SpecialPrice, SpecialPriceStart = model.Product.SpecialPriceStart, SpecialPriceEnd = model.Product.SpecialPriceEnd, IsPublished = model.Product.IsPublished, IsFeatured = model.Product.IsFeatured, IsCallForPricing = model.Product.IsCallForPricing, IsAllowToOrder = model.Product.IsAllowToOrder, BrandId = model.Product.BrandId, TaxClassId = model.Product.TaxClassId, HasOptions = model.Product.Variations.Any() ? true : false, IsVisibleIndividually = true, CreatedBy = currentUser, DisplayOrder = model.Product.Baggage, StockQuantity = model.Product.Seats, Sku = model.Product.TerminalInfo, Via = model.Product.Via, Provider = model.Product.Provider, Currency = model.Product.Currency, ReturnFlightNumber = model.Product.ReturnFlightNumber, ReturnCarrierId = model.Product.ReturnCarrierId, ReturnDepartureDate = model.Product.ReturnDepartureDate, ReturnLandingDate = model.Product.ReturnLandingDate, IsRoundTrip = model.Product.IsRoundTrip, ReturnAircraftId = model.Product.ReturnAircraftId, ReturnTerminal = model.Product.ReturnTerminal, ReturnVia = model.Product.ReturnVia, FlightNumber = model.Product.FlightNumber }; if (!User.IsInRole("admin")) { product.VendorId = currentUser.VendorId; } if (model.Product.IsOutOfStock) { product.StockQuantity = 0; } //else //{ // product.StockQuantity = null; //} var optionIndex = 0; foreach (var option in model.Product.Options) { product.AddOptionValue(new ProductOptionValue { OptionId = option.Id, DisplayType = option.DisplayType, Value = JsonConvert.SerializeObject(option.Values), SortIndex = optionIndex }); optionIndex++; } 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); } await SaveProductMedias(model, product); MapProductVariationVmToProduct(model, product); MapProductLinkVmToProduct(model, product); _productService.Create(product); return(CreatedAtAction(nameof(Get), new { id = product.Id }, null)); }
static Product CreateProduct(Category category) { var product = new Product { Id = 144, Name = "Super Widget", Description = "Some product description", Price = new Money(34.56M), Position = 6, Weight = 345, IsActive = true }; product.AddSize(new Size { Id = 21, IsActive = true, IsInStock = true, Name = "Small" }); product.AddSize(new Size { Id = 22, IsActive = true, IsInStock = true, Name = "Medium" }); product.AddCategory(category, 0); product.AddProductImage(new Image { Id = 3 }, 1); return product; }
public void AddCategory(Product product, Guid categoryId) { product.AddCategory(categoryId); }
public IActionResult Post(ProductForm model) { if (!ModelState.IsValid) { return(new BadRequestObjectResult(ModelState)); } var product = new Product { Name = model.Product.Name, SeoTitle = model.Product.Name.ToUrlFriendly(), ShortDescription = model.Product.ShortDescription, Description = model.Product.Description, Specification = model.Product.Specification, Price = model.Product.Price, OldPrice = model.Product.OldPrice, IsPublished = model.Product.IsPublished, IsFeatured = model.Product.IsFeatured, BrandId = model.Product.BrandId, HasOptions = model.Product.Variations.Any() ? true : false, IsVisibleIndividually = true }; var optionIndex = 0; foreach (var option in model.Product.Options) { product.AddOptionValue(new ProductOptionValue { OptionId = option.Id, Value = JsonConvert.SerializeObject(option.Values), SortIndex = optionIndex }); optionIndex++; } 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()); }