瀏覽代碼

Added some utility methods to CoreUtils;
Allowed for custom MYOB poster settings classes

Kenric Nugteren 1 年之前
父節點
當前提交
dc6b437712

+ 19 - 0
InABox.Core/CoreUtils.cs

@@ -2666,6 +2666,15 @@ namespace InABox.Core
             return string.IsNullOrWhiteSpace(result) ? "??" : result;
         }
 
+        public static string Truncate(this string value, int maxLength)
+        {
+            if(value.Length > maxLength)
+            {
+                return value[..maxLength];
+            }
+            return value;
+        }
+
         #endregion
 
         #region IEnumerable Utilities
@@ -2709,6 +2718,16 @@ namespace InABox.Core
             return enumerable.ToArray<T>();
         }
 
+        public static U[] ToArray<T, U>(this T[] from, Func<T, U> mapFunc)
+        {
+            var to = new U[from.Length];
+            for(int i = 0; i < from.Length; ++i)
+            {
+                to[i] = mapFunc(from[i]);
+            }
+            return to;
+        }
+
         public static IEnumerable<T> AnyOr<T>(this IEnumerable<T> enumerable, IEnumerable<T> or)
         {
             var any = false;

+ 22 - 25
InABox.Core/Postable/PostExceptions.cs

@@ -2,40 +2,37 @@
 using System.Collections.Generic;
 using System.Text;
 
-namespace InABox.Core
+namespace InABox.Core.Postable
 {
-    namespace Postable
+    public class EmptyPostException : Exception
     {
-        public class EmptyPostException : Exception
-        {
-            public EmptyPostException() { }
-        }
+        public EmptyPostException() { }
+    }
 
-        public class PostFailedMessageException : Exception
-        {
-            public PostFailedMessageException(string message): base(message) { }
-        }
+    public class PostFailedMessageException : Exception
+    {
+        public PostFailedMessageException(string message): base(message) { }
+    }
 
-        public class MissingSettingsException : Exception
-        {
-            public Type PostableType { get; }
+    public class MissingSettingsException : Exception
+    {
+        public Type PostableType { get; }
 
-            public MissingSettingsException(Type postableType) : base($"No PostableSettings for ${postableType}")
-            {
-                PostableType = postableType;
-            }
+        public MissingSettingsException(Type postableType) : base($"No PostableSettings for ${postableType}")
+        {
+            PostableType = postableType;
         }
-        public class RepostedException : Exception
+    }
+    public class RepostedException : Exception
+    {
+        public RepostedException() : base("Cannot process an item twice.")
         {
-            public RepostedException() : base("Cannot process an item twice.")
-            {
-            }
         }
-        public class PostCancelledException : Exception
+    }
+    public class PostCancelledException : Exception
+    {
+        public PostCancelledException() : base("Processing cancelled")
         {
-            public PostCancelledException() : base("Processing cancelled")
-            {
-            }
         }
     }
 }

+ 1 - 1
InABox.Core/Postable/PosterEngine.cs

@@ -172,7 +172,7 @@ namespace InABox.Core
                 var result = DoProcess(model);
                 AfterPost(model, result);
 
-                new Client<TPostable>().Save(result.PostedEntities, "Posted by user.");
+                Client.Save(result.PostedEntities, "Posted by user.");
 
                 foreach (var (type, fragments) in result.Fragments)
                 {

+ 97 - 0
InABox.Core/Utils/Result.cs

@@ -7,6 +7,15 @@ namespace InABox.Core
 {
     public static class Result
     {
+        public static Result<E> Ok<E>()
+        {
+            return new Result<E>();
+        }
+        public static Result<E> Error<E>(E error)
+        {
+            return new Result<E>(error);
+        }
+
         public static Result<T, E> Ok<T, E>(T value)
         {
             return new Result<T, E>(value);
@@ -15,6 +24,82 @@ namespace InABox.Core
         {
             return new Result<T, E>(error);
         }
+
+        public static Result<E> Flatten<E>(this Result<Result<E>, E> result)
+        {
+            if(result.Get(out var inner, out var error))
+            {
+                return inner;
+            }
+            else
+            {
+                return Result.Error(error);
+            }
+        }
+        public static Result<T, E> Flatten<T, E>(this Result<Result<T, E>, E> result)
+        {
+            if(result.Get(out var inner, out var error))
+            {
+                return inner;
+            }
+            else
+            {
+                return Result.Error<T, E>(error);
+            }
+        }
+    }
+
+    public class Result<E>
+    {
+        public enum Status
+        {
+            Ok,
+            Error
+        }
+
+        private E _error;
+        private Status _status;
+
+        public Result()
+        {
+            _status = Status.Ok;
+        }
+        public Result(E error)
+        {
+            _error = error;
+            _status = Status.Error;
+        }
+
+        public bool Get([NotNullWhen(false)][MaybeNull] out E error)
+        {
+            error = _error;
+            return _status == Status.Ok;
+        }
+
+        public Result<U, E> MapOk<U>(Func<U> mapFunc)
+        {
+            if(_status == Status.Ok)
+            {
+                return Result.Ok<U, E>(mapFunc());
+            }
+            else
+            {
+                return Result.Error<U, E>(_error);
+            }
+        }
+
+        public Result<E> MapOk(Action mapFunc)
+        {
+            if(_status == Status.Ok)
+            {
+                mapFunc();
+                return Result.Ok<E>();
+            }
+            else
+            {
+                return Result.Error<E>(_error);
+            }
+        }
     }
 
     public class Result<T, E>
@@ -48,5 +133,17 @@ namespace InABox.Core
             error = _error;
             return _status == Status.Ok;
         }
+
+        public Result<U, E> MapOk<U>(Func<T, U> mapFunc)
+        {
+            if(_status == Status.Ok)
+            {
+                return Result.Ok<U, E>(mapFunc(_value));
+            }
+            else
+            {
+                return Result.Error<U, E>(_error);
+            }
+        }
     }
 }

+ 2 - 1
InABox.Poster.MYOB/IMYOBPoster.cs

@@ -8,8 +8,9 @@ using System.Threading.Tasks;
 namespace InABox.Poster.MYOB;
 
 [Caption("MYOB")]
-public interface IMYOBPoster<TPostable> : IPoster<TPostable, MYOBPosterSettings>, IGlobalSettingsPoster<MYOBGlobalPosterSettings>
+public interface IMYOBPoster<TPostable, TSettings> : IPoster<TPostable, TSettings>, IGlobalSettingsPoster<MYOBGlobalPosterSettings>
     where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
+    where TSettings : MYOBPosterSettings
 {
     public MYOBConnectionData ConnectionData { get; set; }
 

+ 5 - 4
InABox.Poster.MYOB/MYOBPosterEngine.cs

@@ -30,11 +30,12 @@ public class MYOBConnectionData(ApiConfiguration configuration, CompanyFileServi
     public IOAuthKeyService AuthKey { get; set; } = authKey;
 }
 
-public partial class MYOBPosterEngine<TPostable> :
-    PosterEngine<TPostable, IMYOBPoster<TPostable>, MYOBPosterSettings>,
-    IGlobalSettingsPosterEngine<IMYOBPoster<TPostable>, MYOBGlobalPosterSettings>
+public abstract partial class MYOBPosterEngine<TPostable, TSettings> :
+    PosterEngine<TPostable, IMYOBPoster<TPostable, TSettings>, TSettings>,
+    IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>
 
     where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
+    where TSettings : MYOBPosterSettings, new()
 {
     internal static readonly string DEV_KEY = "f3b27f88-2ef9-4d8e-95c1-d0a045d0afee";
     internal static readonly string SECRET_KEY = "ksm0e8yo6oumcPb63A8cduaN";
@@ -44,7 +45,7 @@ public partial class MYOBPosterEngine<TPostable> :
 
     private static MYOBConnectionData? _connectionData;
 
-    private MYOBGlobalPosterSettings GetGlobalSettings() => (this as IGlobalSettingsPosterEngine<IMYOBPoster<TPostable>, MYOBGlobalPosterSettings>).GetGlobalSettings();
+    private MYOBGlobalPosterSettings GetGlobalSettings() => (this as IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>).GetGlobalSettings();
 
     public override bool BeforePost(IDataModel<TPostable> model)
     {