Browse Source

Added IPostableFragement interface, and added method to add fragments from the timberline poster

Kenric Nugteren 1 year ago
parent
commit
42dfad6006

+ 13 - 0
InABox.Core/Client/Client.cs

@@ -32,6 +32,7 @@ namespace InABox.Clients
     {
         public abstract CoreTable Query(IFilter? filter = null, IColumns? columns = null, ISortOrder? sortOrder = null);
         public abstract void Save(Entity entity, string auditNote);
+        public abstract void Save(IEnumerable<Entity> entity, string auditNote);
 
         private static IClient CheckClient()
         {
@@ -300,6 +301,18 @@ namespace InABox.Clients
                 throw;
             }
         }
+        public override void Save(IEnumerable<Entity> entities, string auditNote)
+        {
+            try
+            {
+                Save(entities.Cast<TEntity>(), auditNote);
+            }
+            catch (RequestException e)
+            {
+                ClientFactory.RaiseRequestError(e);
+                throw;
+            }
+        }
         public void Save(TEntity entity, string auditnote)
         {
             try

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

@@ -15,7 +15,7 @@ namespace InABox.Core
     /// <summary>
     /// Flags an <see cref="Entity"/> as "Postable"; that is, it can be processed by an <see cref="IPoster{TEntity,TSettings}"/>.
     /// </summary>
-    public interface IPostable
+    public interface IPostable : IPostableFragment
     {
         /// <summary>
         /// At what time this <see cref="IPostable"/> was processed. Set to <see cref="DateTime.MinValue"/> if not processed yet.

+ 18 - 0
InABox.Core/Postable/IPostableFragment.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace InABox.Core
+{
+    /// <summary>
+    /// Intended to be a class that is part of a post of a different <see cref="IPostable"/>, but is not <see cref="IPostable"/> itself.
+    /// We may need to track references for each item, and so the fragment allows that.
+    /// </summary>
+    public interface IPostableFragment
+    {
+        /// <summary>
+        /// Reference details like IDs or numbers so that we can match transactions.
+        /// </summary>
+        string PostedReference { get; set; }
+    }
+}

+ 22 - 0
InABox.Core/Postable/PosterEngine.cs

@@ -55,6 +55,11 @@ namespace InABox.Core
     {
         protected TPoster Poster;
 
+        /// <summary>
+        /// A set of fragments that also need to be saved along with the <see cref="IPostable"/> entities.
+        /// </summary>
+        private Dictionary<Type, List<IPostableFragment>> Fragments = new Dictionary<Type, List<IPostableFragment>>();
+
         public PosterEngine()
         {
             Poster = CreatePoster();
@@ -113,6 +118,17 @@ namespace InABox.Core
             new Client<TPostable>().Save(entities, "Post failed by user.");
         }
 
+        protected void AddFragment(IPostableFragment fragment)
+        {
+            var type = fragment.GetType();
+            if(!Fragments.TryGetValue(type, out var fragments))
+            {
+                fragments = new List<IPostableFragment>();
+                Fragments[type] = fragments;
+            }
+            fragments.Add(fragment);
+        }
+
         public bool Process(IDataModel<TPostable> model)
         {
             if (!BeforePost(model))
@@ -150,7 +166,13 @@ namespace InABox.Core
                         post.PostedStatus = PostedStatus.Posted;
                         post.PostedNote = "";
                     }
+
                     new Client<TPostable>().Save(entities, "Posted by user.");
+
+                    foreach(var (type, fragments) in Fragments)
+                    {
+                        Client.Create(type).Save(fragments.Cast<Entity>(), "");
+                    }
                 }
                 else
                 {

+ 4 - 0
InABox.Poster.Timberline/ITimberlinePoster.cs

@@ -13,6 +13,10 @@ namespace InABox.Poster.Timberline
         where TEntity : Entity, IPostable, IRemotable, IPersistent, new()
         where TSettings : TimberlinePosterSettings<TEntity>
     {
+        public delegate void AddFragmentCallback(IPostableFragment fragment);
+
+        event AddFragmentCallback AddFragment;
+
         ScriptDocument? Script { set; }
 
         bool BeforePost(IDataModel<TEntity> model);

+ 1 - 0
InABox.Poster.Timberline/TimberlinePosterEngine.cs

@@ -23,6 +23,7 @@ namespace InABox.Poster.Timberline
         {
             var poster = base.CreatePoster();
             poster.Script = GetScriptDocument();
+            poster.AddFragment += AddFragment;
             return poster;
         }