PostResult.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace InABox.Core
  6. {
  7. public interface IPostResult<TPostable>
  8. where TPostable : IPostable
  9. {
  10. /// <summary>
  11. /// All successful or failed <typeparamref name="TPostable"/>s.
  12. /// </summary>
  13. public IEnumerable<TPostable> PostedEntities { get; }
  14. public IEnumerable<TPostable> SuccessfulEntities => PostedEntities.Where(x => x.PostedStatus == PostedStatus.Posted);
  15. public IEnumerable<TPostable> FailedEntities => PostedEntities.Where(x => x.PostedStatus == PostedStatus.PostFailed);
  16. public IEnumerable<KeyValuePair<Type, IEnumerable<IPostableFragment<TPostable>>>> Fragments { get; }
  17. }
  18. public class PostResult<TPostable> : IPostResult<TPostable>
  19. where TPostable : IPostable
  20. {
  21. private List<TPostable> posts = new List<TPostable>();
  22. private Dictionary<Type, List<IPostableFragment<TPostable>>> fragments = new Dictionary<Type, List<IPostableFragment<TPostable>>>();
  23. /// <summary>
  24. /// All successful or failed <typeparamref name="TPostable"/>s.
  25. /// </summary>
  26. public IEnumerable<TPostable> PostedEntities => posts;
  27. public IEnumerable<KeyValuePair<Type, IEnumerable<IPostableFragment<TPostable>>>> Fragments =>
  28. fragments.Select(x => new KeyValuePair<Type, IEnumerable<IPostableFragment<TPostable>>>(x.Key, x.Value));
  29. public void AddSuccess(TPostable post)
  30. {
  31. post.Post();
  32. posts.Add(post);
  33. }
  34. public void AddFailed(TPostable post, string note)
  35. {
  36. post.FailPost(note);
  37. posts.Add(post);
  38. }
  39. public void AddFragment<TPostableFragment>(TPostableFragment fragment, PostedStatus status)
  40. where TPostableFragment : IPostableFragment<TPostable>, IPostable
  41. {
  42. var type = fragment.GetType();
  43. if (!fragments.TryGetValue(type, out var fragmentsList))
  44. {
  45. fragmentsList = new List<IPostableFragment<TPostable>>();
  46. fragments[type] = fragmentsList;
  47. }
  48. fragment.Post();
  49. fragment.PostedStatus = status;
  50. fragmentsList.Add(fragment);
  51. }
  52. public void AddFragment(IPostableFragment<TPostable> fragment)
  53. {
  54. var type = fragment.GetType();
  55. if (!fragments.TryGetValue(type, out var fragmentsList))
  56. {
  57. fragmentsList = new List<IPostableFragment<TPostable>>();
  58. fragments[type] = fragmentsList;
  59. }
  60. if(fragment is IPostable postableFragment)
  61. {
  62. postableFragment.Post();
  63. }
  64. fragmentsList.Add(fragment);
  65. }
  66. }
  67. public enum PullResultType
  68. {
  69. New,
  70. Linked,
  71. Updated
  72. }
  73. public class PullResultItem<TPostable>
  74. where TPostable : IPostable
  75. {
  76. public PullResultType Type { get; set; }
  77. public TPostable Item { get; set; }
  78. public PullResultItem(PullResultType type, TPostable item)
  79. {
  80. Type = type;
  81. Item = item;
  82. }
  83. }
  84. public interface IPullResult<TPostable>
  85. where TPostable : IPostable
  86. {
  87. public IEnumerable<PullResultItem<TPostable>> PulledEntities { get; }
  88. }
  89. public class PullResult<TPostable> : IPullResult<TPostable>
  90. where TPostable : IPostable
  91. {
  92. private List<PullResultItem<TPostable>> posts = new List<PullResultItem<TPostable>>();
  93. /// <summary>
  94. /// All successful or failed <typeparamref name="TPostable"/>s.
  95. /// </summary>
  96. public IEnumerable<PullResultItem<TPostable>> PulledEntities => posts;
  97. public void AddEntity(PullResultType type, TPostable post)
  98. {
  99. post.Post();
  100. posts.Add(new PullResultItem<TPostable>(type, post));
  101. }
  102. }
  103. }