Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Batch inventory stock writes into single repository updates (#777)

Krzysztof Pajak3c938385d1ff
diff --git a/src/Business/Grand.Business.Catalog/Services/Products/InventoryManageService.cs b/src/Business/Grand.Business.Catalog/Services/Products/InventoryManageService.cs
index 289127824..ed91f8ff8 100644
--- a/src/Business/Grand.Business.Catalog/Services/Products/InventoryManageService.cs
+++ b/src/Business/Grand.Business.Catalog/Services/Products/InventoryManageService.cs
@@ -62,8 +62,6 @@ public class InventoryManageService : IInventoryManageService
pwi.ReservedQuantity = 0;
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductWarehouseInventory, z => z.Id == pwi.Id, pwi);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
-
product.StockQuantity = product.ProductWarehouseInventory.Sum(x => x.StockQuantity);
product.ReservedQuantity = product.ProductWarehouseInventory.Sum(x => x.ReservedQuantity);
await UpdateStockProduct(product);
@@ -90,7 +88,6 @@ public class InventoryManageService : IInventoryManageService
combination.ReservedQuantity = 0;
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductAttributeCombinations, z => z.Id == combination.Id, combination);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
}
else
{
@@ -107,7 +104,6 @@ public class InventoryManageService : IInventoryManageService
combination.ReservedQuantity = combination.WarehouseInventory.Sum(x => x.ReservedQuantity);
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductAttributeCombinations, z => z.Id == combination.Id, combination);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
}
product.StockQuantity = product.ProductAttributeCombinations.Sum(x => x.StockQuantity);
@@ -187,7 +183,6 @@ public class InventoryManageService : IInventoryManageService
pwi.ReservedQuantity = 0;
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductWarehouseInventory, z => z.Id == pwi.Id, pwi);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
break;
}
case ManageInventoryMethod.ManageStock:
@@ -213,8 +208,6 @@ public class InventoryManageService : IInventoryManageService
product.ReservedQuantity = product.ProductAttributeCombinations.Sum(x => x.ReservedQuantity);
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductAttributeCombinations, z => z.Id == combination.Id, combination);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
-
await UpdateStockProduct(product);
}
else
@@ -236,7 +229,6 @@ public class InventoryManageService : IInventoryManageService
product.ReservedQuantity = product.ProductAttributeCombinations.Sum(x => x.ReservedQuantity);
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductAttributeCombinations, z => z.Id == combination.Id, combination);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
await UpdateStockProduct(product);
}
@@ -310,10 +302,11 @@ public class InventoryManageService : IInventoryManageService
product.DisableBuyButton = true;
product.LowStock = true;
- await _productRepository.UpdateField(product.Id, x => x.DisableBuyButton,
- product.DisableBuyButton);
- await _productRepository.UpdateField(product.Id, x => x.LowStock, product.LowStock);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
+ await _productRepository.UpdateOneAsync(x => x.Id == product.Id,
+ UpdateBuilder<Product>.Create()
+ .Set(x => x.DisableBuyButton, product.DisableBuyButton)
+ .Set(x => x.LowStock, product.LowStock));
+
//cache
await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, product.Id));
@@ -325,9 +318,10 @@ public class InventoryManageService : IInventoryManageService
product.Published = false;
product.LowStock = true;
- await _productRepository.UpdateField(product.Id, x => x.Published, product.Published);
- await _productRepository.UpdateField(product.Id, x => x.LowStock, product.LowStock);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
+ await _productRepository.UpdateOneAsync(x => x.Id == product.Id,
+ UpdateBuilder<Product>.Create()
+ .Set(x => x.Published, product.Published)
+ .Set(x => x.LowStock, product.LowStock));
//cache
await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, product.Id));
@@ -350,10 +344,10 @@ public class InventoryManageService : IInventoryManageService
product.DisableBuyButton = false;
product.LowStock = product.MinStockQuantity <= prevStockQuantity;
- await _productRepository.UpdateField(product.Id, x => x.DisableBuyButton,
- product.DisableBuyButton);
- await _productRepository.UpdateField(product.Id, x => x.LowStock, product.LowStock);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
+ await _productRepository.UpdateOneAsync(x => x.Id == product.Id,
+ UpdateBuilder<Product>.Create()
+ .Set(x => x.DisableBuyButton, product.DisableBuyButton)
+ .Set(x => x.LowStock, product.LowStock));
//cache
await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, product.Id));
@@ -366,9 +360,10 @@ public class InventoryManageService : IInventoryManageService
product.Published = true;
product.LowStock = product.MinStockQuantity < prevStockQuantity;
- await _productRepository.UpdateField(product.Id, x => x.Published, product.Published);
- await _productRepository.UpdateField(product.Id, x => x.LowStock, product.LowStock);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
+ await _productRepository.UpdateOneAsync(x => x.Id == product.Id,
+ UpdateBuilder<Product>.Create()
+ .Set(x => x.Published, product.Published)
+ .Set(x => x.LowStock, product.LowStock));
//cache
await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, product.Id));
@@ -465,7 +460,6 @@ public class InventoryManageService : IInventoryManageService
pwi.ReservedQuantity = 0;
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductWarehouseInventory, z => z.Id == pwi.Id, pwi);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
}
//cache
@@ -503,8 +497,8 @@ public class InventoryManageService : IInventoryManageService
product.ReservedQuantity = product.ProductAttributeCombinations.Sum(x => x.ReservedQuantity);
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductAttributeCombinations, z => z.Id == combination.Id, combination);
- await _productRepository.UpdateField(product.Id, x => x.ReservedQuantity, product.ReservedQuantity);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
+ await _productRepository.UpdateOneAsync(x => x.Id == product.Id,
+ UpdateBuilder<Product>.Create().Set(x => x.ReservedQuantity, product.ReservedQuantity));
}
else
{
@@ -522,8 +516,8 @@ public class InventoryManageService : IInventoryManageService
product.ReservedQuantity = product.ProductAttributeCombinations.Sum(x => x.ReservedQuantity);
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductAttributeCombinations, z => z.Id == combination.Id, combination);
- await _productRepository.UpdateField(product.Id, x => x.ReservedQuantity, product.ReservedQuantity);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
+ await _productRepository.UpdateOneAsync(x => x.Id == product.Id,
+ UpdateBuilder<Product>.Create().Set(x => x.ReservedQuantity, product.ReservedQuantity));
}
//cache
@@ -567,7 +561,6 @@ public class InventoryManageService : IInventoryManageService
pwi.ReservedQuantity = 0;
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductWarehouseInventory, z => z.Id == pwi.Id, pwi);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
}
//cache
@@ -601,8 +594,8 @@ public class InventoryManageService : IInventoryManageService
product.ReservedQuantity = product.ProductAttributeCombinations.Sum(x => x.ReservedQuantity);
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductAttributeCombinations, z => z.Id == combination.Id, combination);
- await _productRepository.UpdateField(product.Id, x => x.ReservedQuantity, product.ReservedQuantity);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
+ await _productRepository.UpdateOneAsync(x => x.Id == product.Id,
+ UpdateBuilder<Product>.Create().Set(x => x.ReservedQuantity, product.ReservedQuantity));
}
else
{
@@ -617,7 +610,6 @@ public class InventoryManageService : IInventoryManageService
combination.ReservedQuantity = combination.WarehouseInventory.Sum(x => x.ReservedQuantity);
await _productRepository.UpdateCollectionFieldItem(product.Id, x => x.ProductAttributeCombinations, z => z.Id == combination.Id, combination);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
}
//cache
@@ -704,14 +696,15 @@ public class InventoryManageService : IInventoryManageService
if (product.ReservedQuantity < 0)
product.ReservedQuantity = 0;
- //update
- await _productRepository.UpdateField(product.Id, x => x.StockQuantity, product.StockQuantity);
- await _productRepository.UpdateField(product.Id, x => x.ReservedQuantity, product.ReservedQuantity);
- await _productRepository.UpdateField(product.Id, x => x.LowStock,
- (product.MinStockQuantity > 0 &&
- product.MinStockQuantity >= product.StockQuantity - product.ReservedQuantity) ||
- product.StockQuantity - product.ReservedQuantity <= 0);
- await _productRepository.UpdateField(product.Id, x => x.UpdatedOnUtc, DateTime.UtcNow);
+ //update - one write, so the three stock fields never land out of step
+ await _productRepository.UpdateOneAsync(x => x.Id == product.Id,
+ UpdateBuilder<Product>.Create()
+ .Set(x => x.StockQuantity, product.StockQuantity)
+ .Set(x => x.ReservedQuantity, product.ReservedQuantity)
+ .Set(x => x.LowStock,
+ (product.MinStockQuantity > 0 &&
+ product.MinStockQuantity >= product.StockQuantity - product.ReservedQuantity) ||
+ product.StockQuantity - product.ReservedQuantity <= 0));
//cache