using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace InABox.Core { public interface IPostResult where TPostable : IPostable { /// /// All successful or failed s. /// public IEnumerable PostedEntities { get; } public IEnumerable SuccessfulEntities => PostedEntities.Where(x => x.PostedStatus == PostedStatus.Posted); public IEnumerable FailedEntities => PostedEntities.Where(x => x.PostedStatus == PostedStatus.PostFailed); public IEnumerable>>> Fragments { get; } } public class PostResult : IPostResult where TPostable : IPostable { private List posts = new List(); private Dictionary>> fragments = new Dictionary>>(); /// /// All successful or failed s. /// public IEnumerable PostedEntities => posts; public IEnumerable>>> Fragments => fragments.Select(x => new KeyValuePair>>(x.Key, x.Value)); public void AddSuccess(TPostable post) { post.Post(); posts.Add(post); } public void AddFailed(TPostable post, string note) { post.FailPost(note); posts.Add(post); } public void AddFragment(TPostableFragment fragment, PostedStatus status) where TPostableFragment : IPostableFragment, IPostable { var type = fragment.GetType(); if (!fragments.TryGetValue(type, out var fragmentsList)) { fragmentsList = new List>(); fragments[type] = fragmentsList; } fragment.Post(); fragment.PostedStatus = status; fragmentsList.Add(fragment); } public void AddFragment(IPostableFragment fragment) { var type = fragment.GetType(); if (!fragments.TryGetValue(type, out var fragmentsList)) { fragmentsList = new List>(); fragments[type] = fragmentsList; } if(fragment is IPostable postableFragment) { postableFragment.Post(); } fragmentsList.Add(fragment); } } public enum PullResultType { New, Linked, Updated } public class PullResultItem where TPostable : IPostable { public PullResultType Type { get; set; } public TPostable Item { get; set; } public PullResultItem(PullResultType type, TPostable item) { Type = type; Item = item; } } public interface IPullResult where TPostable : IPostable { public IEnumerable> PulledEntities { get; } } public class PullResult : IPullResult where TPostable : IPostable { private List> posts = new List>(); /// /// All successful or failed s. /// public IEnumerable> PulledEntities => posts; public void AddEntity(PullResultType type, TPostable post) { post.Post(); posts.Add(new PullResultItem(type, post)); } } }