Prechádzať zdrojové kódy

Implemented CustomPoster

Kenric Nugteren 1 rok pred
rodič
commit
f34f88328b

+ 0 - 3
InABox.Core/Postable/IPoster.cs

@@ -16,8 +16,5 @@ namespace InABox.Core
         where TEntity : Entity, IPostable, IRemotable, IPersistent, new()
         where TSettings : PosterSettings
     {
-        bool BeforePost(IDataModel<TEntity> model);
-
-        void AfterPost(IDataModel<TEntity> model);
     }
 }

+ 3 - 0
InABox.Poster.CSV/ICSVPoster.cs

@@ -91,6 +91,9 @@ namespace InABox.Poster.CSV
     public interface ICSVPoster<TEntity> : IPoster<TEntity, CSVPosterSettings>
         where TEntity : Entity, IPostable, IRemotable, IPersistent, new()
     {
+        bool BeforePost(IDataModel<TEntity> model);
+
         ICSVExport Process(IDataModel<TEntity> model);
+        void AfterPost(IDataModel<TEntity> model);
     }
 }

+ 68 - 0
InABox.Poster.Custom/CustomPosterEngine.cs

@@ -0,0 +1,68 @@
+using InABox.Core;
+using InABox.Scripting;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Poster.Custom
+{
+    public class CustomPosterEngine<TPostable> : PosterEngine<TPostable, ICustomPoster<TPostable>, CustomPosterSettings>
+        where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
+    {
+        private ScriptDocument? _script;
+        private bool _hasCheckedScript;
+
+        private ScriptDocument? GetScriptDocument()
+        {
+            if (_hasCheckedScript)
+            {
+                return _script;
+            }
+
+            var settings = GetSettings();
+            if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
+            {
+                var document = new ScriptDocument(settings.Script);
+                if (!document.Compile())
+                {
+                    throw new Exception("Script failed to compile!");
+                }
+                _script = document;
+            }
+            else
+            {
+                _script = null;
+            }
+
+            _hasCheckedScript = true;
+            return _script;
+        }
+
+        public override bool BeforePost(IDataModel<TPostable> model)
+        {
+            if (GetScriptDocument() is ScriptDocument script)
+            {
+                return script.Execute(methodname: "BeforePost", parameters: new object[] { model });
+            }
+            return false;
+        }
+
+        protected override bool DoProcess(IDataModel<TPostable> model)
+        {
+            if (GetScriptDocument() is ScriptDocument script)
+            {
+                return script.Execute(methodname: "Process", parameters: new object[] { model });
+            }
+            return false;
+        }
+        public override void AfterPost(IDataModel<TPostable> model)
+        {
+            if (GetScriptDocument() is ScriptDocument script)
+            {
+                script.Execute(methodname: "AfterPost", parameters: new object[] { model });
+            }
+        }
+    }
+}

+ 49 - 0
InABox.Poster.Custom/CustomPosterSettings.cs

@@ -0,0 +1,49 @@
+using InABox.Core;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Poster.Custom
+{
+    public class CustomPosterSettings : PosterSettings
+    {
+        public override string DefaultScript(Type TPostable)
+        {
+            var tName = TPostable.Name;
+            var decapital = tName[0..1].ToLower() + tName[1..];
+
+            var ns = TPostable.Namespace;
+
+            return @"
+using " + ns + @";
+using InABox.Core;
+using System.Collections.Generic;
+
+public class Module
+{
+    // Customise 'model' before loading data. Return false if the post needs to be cancelled.
+    public bool BeforePost(IDataModel<" + tName + @"> model)
+    {
+        return true;
+    }
+
+    public bool Process(IDataModel<" + tName + @"> model)
+    {
+        foreach(var " + decapital + @" in model.GetTable<" + tName + @">().ToObjects<" + tName + @">())
+        {
+            // Do processing
+        }
+
+        return true; // return true for success.
+    }
+
+    // Perform any post-processing. All the items of type '" + tName + @" will be saved after this function is called.
+    public void AfterPost(IDataModel<" + tName + @"> model)
+    {
+    }
+}";
+        }
+    }
+}

+ 9 - 0
InABox.Poster.Custom/ICustomPoster.cs

@@ -0,0 +1,9 @@
+using InABox.Core;
+
+namespace InABox.Poster.Custom
+{
+    public interface ICustomPoster<TEntity> : IPoster<TEntity, CustomPosterSettings>
+        where TEntity : Entity, IPostable, IRemotable, IPersistent, new()
+    {
+    }
+}

+ 9 - 0
InABox.Poster.Custom/InABox.Poster.Custom.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net6.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+</Project>