in src/Modules/SimplCommerce.Module.Catalog/Areas/Catalog/Controllers/ProductApiController.cs [285:375]
public async Task<IActionResult> Post(ProductForm model)
{
MapUploadedFile(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);
await 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);
}