|
@@ -13,7 +13,7 @@ namespace InABox.Core
|
|
|
public interface IPosterEngine<TPostable>
|
|
|
where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
|
|
|
{
|
|
|
- bool Process(IDataModel<TPostable> model);
|
|
|
+ IPostResult<TPostable>? Process(IDataModel<TPostable> model);
|
|
|
}
|
|
|
|
|
|
public interface IPosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable>
|
|
@@ -55,11 +55,6 @@ 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();
|
|
@@ -74,7 +69,7 @@ namespace InABox.Core
|
|
|
return poster;
|
|
|
}
|
|
|
|
|
|
- private TSettings _settings;
|
|
|
+ private TSettings? _settings;
|
|
|
protected TSettings GetSettings()
|
|
|
{
|
|
|
_settings ??= PosterUtils.LoadPosterSettings<TPostable, TSettings>();
|
|
@@ -97,7 +92,7 @@ namespace InABox.Core
|
|
|
return settings.ScriptEnabled ? settings.Script : null;
|
|
|
}
|
|
|
|
|
|
- protected abstract bool DoProcess(IDataModel<TPostable> model);
|
|
|
+ protected abstract IPostResult<TPostable> DoProcess(IDataModel<TPostable> model);
|
|
|
|
|
|
/// <summary>
|
|
|
/// Process the <paramref name="model"/> before loading;
|
|
@@ -111,7 +106,8 @@ namespace InABox.Core
|
|
|
/// This is only called if <see cref="Process(IDataModel{TPostable})"/> returned <see langword="true"/>.
|
|
|
/// </summary>
|
|
|
/// <param name="model"></param>
|
|
|
- public abstract void AfterPost(IDataModel<TPostable> model);
|
|
|
+ /// <param name="result">The result object returned by <see cref="DoProcess(IDataModel{TPostable})"/></param>
|
|
|
+ public abstract void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result);
|
|
|
|
|
|
private static void SetFailed(IList<TPostable> entities)
|
|
|
{
|
|
@@ -123,22 +119,11 @@ 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)
|
|
|
+ public IPostResult<TPostable>? Process(IDataModel<TPostable> model)
|
|
|
{
|
|
|
if (!BeforePost(model))
|
|
|
{
|
|
|
- return false;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
// Add posted flags; if the columns is null, then we don't need to worry, because it will be loaded.
|
|
@@ -161,35 +146,16 @@ namespace InABox.Core
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- var success = DoProcess(model);
|
|
|
- if (success)
|
|
|
- {
|
|
|
- AfterPost(model);
|
|
|
- }
|
|
|
+ var result = DoProcess(model);
|
|
|
+ AfterPost(model, result);
|
|
|
|
|
|
- var entities = data.ToObjects<TPostable>().ToList();
|
|
|
+ new Client<TPostable>().Save(result.PostedEntities, "Posted by user.");
|
|
|
|
|
|
- if (success)
|
|
|
- {
|
|
|
- foreach (var post in entities)
|
|
|
- {
|
|
|
- post.Posted = DateTime.Now;
|
|
|
- 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
|
|
|
+ foreach (var (type, fragments) in result.Fragments)
|
|
|
{
|
|
|
- SetFailed(entities);
|
|
|
+ Client.Create(type).Save(fragments.Cast<Entity>(), "");
|
|
|
}
|
|
|
- return success;
|
|
|
+ return result;
|
|
|
}
|
|
|
catch (PostCancelledException)
|
|
|
{
|