private void AddOrDeleteProductAttribute(ProductForm model, Product product) { foreach (var productAttributeVm in model.Product.Attributes) { var productAttrValue = product.AttributeValues.FirstOrDefault(x => x.AttributeId == productAttributeVm.Id); if (productAttrValue == null) { productAttrValue = new ProductAttributeValue { AttributeId = productAttributeVm.Id, Value = productAttributeVm.Value }; product.AddAttributeValue(productAttrValue); } else { productAttrValue.Value = productAttributeVm.Value; } } var deletedAttrValues = product.AttributeValues.Where(attrValue => model.Product.Attributes.All(x => x.Id != attrValue.AttributeId)) .ToList(); foreach (var deletedAttrValue in deletedAttrValues) { deletedAttrValue.Product = null; product.AttributeValues.Remove(deletedAttrValue); _productAttributeValueRepository.Remove(deletedAttrValue); } }
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()); }
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 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()); }
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)); }