Browse Source

Improved auto-increment logic.

Kenric Nugteren 1 year ago
parent
commit
bea087575e
2 changed files with 28 additions and 26 deletions
  1. 6 5
      InABox.Core/Configuration/GlobalConfiguration.cs
  2. 22 21
      InABox.Database/Stores/Store.cs

+ 6 - 5
InABox.Core/Configuration/GlobalConfiguration.cs

@@ -13,11 +13,11 @@ namespace InABox.Configuration
     [UserTracking(false)]
     public class GlobalSettings : Entity, IPersistent, IRemotable, ILicense<CoreLicense>
     {
-        public string Section { get; set; }
+        public string Section { get; set; } = "";
 
-        public string Key { get; set; }
+        public string Key { get; set; } = "";
 
-        public string Contents { get; set; }
+        public string Contents { get; set; } = "";
     }
 
     public interface IGlobalConfiguration
@@ -41,7 +41,7 @@ namespace InABox.Configuration
 
         private GlobalSettings GetSettings()
         {
-            GlobalSettings result = null;
+            GlobalSettings? result = null;
             if (ClientFactory.ClientType != null)
             {
                 var client = new Client<GlobalSettings>();
@@ -52,7 +52,8 @@ namespace InABox.Configuration
         }
 
         object IGlobalConfiguration.Load(bool useCache) => Load(useCache);
-        
+
+
         public override T Load(bool useCache = true)
         {
             if (useCache)

+ 22 - 21
InABox.Database/Stores/Store.cs

@@ -422,10 +422,13 @@ namespace InABox.Database
                 var prefix = autoinc.AutoIncrementPrefix() ?? "";
                 
                 var prop = CoreUtils.GetPropertyFromExpression<T, string>(autoinc.AutoIncrementField());
-                var bRequired = false;
-                foreach (var entity in entities)
-                    bRequired = bRequired || string.IsNullOrWhiteSpace(prop.GetValue(entity) as string);
-                if (bRequired)
+
+                var requiredEntities = entities.Where(x =>
+                {
+                    return (prop.GetValue(x) as string).IsNullOrWhiteSpace() && (x.ID == Guid.Empty || x.HasOriginalValue(prop.Name));
+                }).ToList();
+
+                if (requiredEntities.Count > 0)
                 {
                     
                     var filter = new Filter<T>(prop.Name).IsGreaterThanOrEqualTo(String.Format("{0}0",prefix))
@@ -451,12 +454,11 @@ namespace InABox.Database
                         int.TryParse(id, out newvalue);
                     }
                     
-                    foreach (var entity in entities)
-                        if (string.IsNullOrWhiteSpace(prop.GetValue(entity) as string))
-                        {
-                            newvalue++;
-                            prop.SetValue(entity, prefix + string.Format(autoinc.AutoIncrementFormat(), newvalue));
-                        }
+                    foreach (var entity in requiredEntities)
+                    {
+                        newvalue++;
+                        prop.SetValue(entity, prefix + string.Format(autoinc.AutoIncrementFormat(), newvalue));
+                    }
 
                     return true;
                 }
@@ -472,11 +474,13 @@ namespace InABox.Database
             if (entities.First() is INumericAutoIncrement<T> autoinc)
             {
                 var prop = CoreUtils.GetPropertyFromExpression(autoinc.AutoIncrementField());
-                
-                var bRequired = false;
-                foreach (var entity in entities)
-                    bRequired = bRequired || prop.GetValue(entity).Equals(0);
-                if (bRequired)
+
+                var requiredEntities = entities.Where(x =>
+                {
+                    return object.Equals(prop.GetValue(x), 0) && (x.ID == Guid.Empty || x.HasOriginalValue(prop.Name));
+                }).ToList();
+
+                if (requiredEntities.Count > 0)
                 {
                     
                     var row = Provider.Query(
@@ -487,13 +491,10 @@ namespace InABox.Database
                     ).Rows.FirstOrDefault();
                     int newvalue = row != null ? row.Get<int>(prop.Name) : 0;
 
-                    foreach (var entity in entities)
+                    foreach (var entity in requiredEntities)
                     {
-                        if (prop.GetValue(entity).Equals(0))
-                        {
-                            newvalue++;
-                            prop.SetValue(entity, newvalue);
-                        }
+                        newvalue++;
+                        prop.SetValue(entity, newvalue);
                     }
                     return true;
                 }