Browse Source

Fixing the StockMovementStore to not load unnecessary data, and also to not override the changes made in BeforeSave

Kenric Nugteren 6 days ago
parent
commit
a97a9e19e6
1 changed files with 9 additions and 3 deletions
  1. 9 3
      prs.stores/StockMovementStore.cs

+ 9 - 3
prs.stores/StockMovementStore.cs

@@ -16,6 +16,7 @@ public class StockMovementStore : BaseStore<StockMovement>
     // These will be initialised in BeforeSave
     HoldingDictionary holdingData = null!;
     StockMovement[] mvtData = null!;
+    HashSet<Guid> currentIDs = new();
 
     protected override void BeforeSave(IEnumerable<StockMovement> entities)
     {
@@ -25,7 +26,9 @@ public class StockMovementStore : BaseStore<StockMovement>
             base.BeforeSave(entity);
         }
 
-        mvtData = StockHoldingStore.LoadMovementData(this, entities.Select(x => x.ID).ToArray());
+        currentIDs = entities.Select(x => x.ID).Where(x => x != Guid.Empty).ToHashSet();
+
+        mvtData = StockHoldingStore.LoadMovementData(this, currentIDs.ToArray());
         holdingData = StockHoldingStore.LoadStockHoldings(this, mvtData);
 
         StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Decrease);
@@ -68,8 +71,11 @@ public class StockMovementStore : BaseStore<StockMovement>
 
     protected override void AfterSave(IEnumerable<StockMovement> entities)
     {
-        mvtData = StockHoldingStore.LoadMovementData(this, entities.Select(x => x.ID).ToArray());
-        holdingData = StockHoldingStore.LoadStockHoldings(this, mvtData);
+        var newIDs = entities.Select(x => x.ID).Where(x => !currentIDs.Contains(x)).ToArray();
+        var newMvts = StockHoldingStore.LoadMovementData(this, newIDs);
+        StockHoldingStore.LoadStockHoldings(this, newMvts, holdingData);
+
+        mvtData = mvtData.Concatenate(newMvts);
         
         // Update the Relevant StockHolding with the details of this movement
         StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Increase);