CustomPosterEngine.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using InABox.Core;
  2. using InABox.Poster.Shared;
  3. using InABox.Scripting;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace InABox.Poster.Custom;
  10. public class CustomPosterEngine<TPostable> : BasePosterEngine<TPostable, ICustomPoster<TPostable>, CustomPosterSettings>
  11. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  12. {
  13. public override bool BeforePost(IDataModel<TPostable> model)
  14. {
  15. if (GetScriptDocument() is ScriptDocument script)
  16. {
  17. return script.Execute(methodname: "BeforePost", parameters: new object[] { model });
  18. }
  19. return false;
  20. }
  21. protected override IPostResult<TPostable> DoProcess(IDataModel<TPostable> model)
  22. {
  23. if (GetScriptDocument() is ScriptDocument script)
  24. {
  25. if(!script.Execute(methodname: "Process", parameters: new object[] { model }))
  26. {
  27. throw new Exception("Post Failed.");
  28. }
  29. var resultsObject = script.GetValue("Results");
  30. return (resultsObject as IPostResult<TPostable>)
  31. ?? throw new Exception($"Script 'Results' property expected to be IPostResult<{typeof(TPostable)}>, got {resultsObject}");
  32. }
  33. else
  34. {
  35. throw new Exception("Post Failed.");
  36. }
  37. }
  38. public override void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result)
  39. {
  40. if (GetScriptDocument() is ScriptDocument script)
  41. {
  42. script.Execute(methodname: "AfterPost", parameters: new object[] { model });
  43. }
  44. }
  45. }