in src/Modules/SimplCommerce.Module.Catalog/Areas/Catalog/Controllers/ProductApiController.cs [378:466]
public async Task<IActionResult> Put(long id, ProductForm model)
{
MapUploadedFile(model);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var product = _productRepository.Query()
.Include(x => x.ThumbnailImage)
.Include(x => x.Medias).ThenInclude(m => m.Media)
.Include(x => x.ProductLinks).ThenInclude(x => x.LinkedProduct).ThenInclude(p => p.ThumbnailImage)
.Include(x => x.OptionValues).ThenInclude(o => o.Option)
.Include(x => x.AttributeValues).ThenInclude(a => a.Attribute).ThenInclude(g => g.Group)
.Include(x => x.Categories)
.FirstOrDefault(x => x.Id == id);
if(product == null)
{
return NotFound();
}
var currentUser = await _workContext.GetCurrentUser();
if (!User.IsInRole("admin") && product.VendorId != currentUser.VendorId)
{
return BadRequest(new { error = "You don't have permission to manage this product" });
}
var isPriceChanged = false;
if (product.Price != model.Product.Price ||
product.OldPrice != model.Product.OldPrice ||
product.SpecialPrice != model.Product.SpecialPrice ||
product.SpecialPriceStart != model.Product.SpecialPriceStart ||
product.SpecialPriceEnd != model.Product.SpecialPriceEnd)
{
isPriceChanged = true;
}
product.Name = model.Product.Name;
product.Slug = model.Product.Slug;
product.MetaTitle = model.Product.MetaTitle;
product.MetaKeywords = model.Product.MetaKeywords;
product.MetaDescription = model.Product.MetaDescription;
product.Sku = model.Product.Sku;
product.Gtin = model.Product.Gtin;
product.ShortDescription = model.Product.ShortDescription;
product.Description = model.Product.Description;
product.Specification = model.Product.Specification;
product.Price = model.Product.Price;
product.OldPrice = model.Product.OldPrice;
product.SpecialPrice = model.Product.SpecialPrice;
product.SpecialPriceStart = model.Product.SpecialPriceStart;
product.SpecialPriceEnd = model.Product.SpecialPriceEnd;
product.BrandId = model.Product.BrandId;
product.TaxClassId = model.Product.TaxClassId;
product.IsFeatured = model.Product.IsFeatured;
product.IsPublished = model.Product.IsPublished;
product.IsCallForPricing = model.Product.IsCallForPricing;
product.IsAllowToOrder = model.Product.IsAllowToOrder;
product.StockTrackingIsEnabled = model.Product.StockTrackingIsEnabled;
product.LatestUpdatedBy = currentUser;
if (isPriceChanged)
{
var productPriceHistory = CreatePriceHistory(currentUser, product);
product.PriceHistories.Add(productPriceHistory);
}
await SaveProductMedias(model, product);
foreach (var productMediaId in model.Product.DeletedMediaIds)
{
var productMedia = product.Medias.First(x => x.Id == productMediaId);
_productMediaRepository.Remove(productMedia);
await _mediaService.DeleteMediaAsync(productMedia.Media);
}
AddOrDeleteProductOption(model, product);
AddOrDeleteProductAttribute(model, product);
AddOrDeleteCategories(model, product);
await AddOrDeleteProductVariation(currentUser, model, product);
AddOrDeleteProductLinks(model, product);
_productService.Update(product);
return Accepted();
}