ICSVPoster.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using CsvHelper.Configuration;
  2. using InABox.Core;
  3. using System.Collections.Generic;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. namespace InABox.Poster.CSV;
  7. public interface ICSVClassMap
  8. {
  9. ClassMap ClassMap { get; }
  10. }
  11. public interface ICSVClassMap<T> : ICSVClassMap
  12. {
  13. new ClassMap<T> ClassMap { get; }
  14. ClassMap ICSVClassMap.ClassMap => ClassMap;
  15. void Map(string name, Expression<Func<T, object>> expr);
  16. }
  17. public class CSVClassMap<T> : ClassMap<T>, ICSVClassMap<T>
  18. {
  19. public ClassMap<T> ClassMap => this;
  20. public void Map(string name, Expression<Func<T, object>> expr)
  21. {
  22. Map(expr).Name(name);
  23. }
  24. }
  25. public interface ICSVExport<TPostable> : IPostResult<TPostable>
  26. where TPostable : IPostable
  27. {
  28. Type Type { get; }
  29. ICSVClassMap ClassMap { get; }
  30. IEnumerable<object> Records { get; }
  31. }
  32. public interface ICSVExport<TExport, TPostable> : ICSVExport<TPostable>
  33. where TExport : class
  34. where TPostable : IPostable
  35. {
  36. new Type Type => typeof(TExport);
  37. new CSVClassMap<TExport> ClassMap { get; }
  38. new IEnumerable<TExport> Records { get; }
  39. Type ICSVExport<TPostable>.Type => Type;
  40. ICSVClassMap ICSVExport<TPostable>.ClassMap => ClassMap;
  41. IEnumerable<object> ICSVExport<TPostable>.Records => Records;
  42. }
  43. public class CSVExport<TExport, TPostable> : ICSVExport<TExport, TPostable>
  44. where TExport : class
  45. where TPostable : IPostable
  46. {
  47. public CSVClassMap<TExport> ClassMap { get; } = new CSVClassMap<TExport>();
  48. public IEnumerable<TExport> Records => items.Where(x => x.Item2.PostedStatus == PostedStatus.Posted).Select(x => x.Item1);
  49. private List<Tuple<TExport, TPostable>> items = new List<Tuple<TExport, TPostable>>();
  50. private Dictionary<Type, List<IPostableFragment<TPostable>>> fragments = new Dictionary<Type, List<IPostableFragment<TPostable>>>();
  51. public IEnumerable<TPostable> PostedEntities => items.Select(x => x.Item2);
  52. public IEnumerable<KeyValuePair<Type, IEnumerable<IPostableFragment<TPostable>>>> Fragments =>
  53. fragments.Select(x => new KeyValuePair<Type, IEnumerable<IPostableFragment<TPostable>>>(x.Key, x.Value));
  54. public void DefineMapping(List<Tuple<string, Expression<Func<TExport, object>>>> mappings)
  55. {
  56. foreach(var (name, expr) in mappings)
  57. {
  58. ClassMap.Map(name, expr);
  59. }
  60. }
  61. public void Map(string name, Expression<Func<TExport, object>> expr)
  62. {
  63. ClassMap.Map(name, expr);
  64. }
  65. public void AddSuccess(TExport export, TPostable postable)
  66. {
  67. postable.Post();
  68. items.Add(new(export, postable));
  69. }
  70. }
  71. /// <summary>
  72. /// Defines an interface for posters that can export to CSV.
  73. /// </summary>
  74. /// <typeparam name="TEntity"></typeparam>
  75. [Caption("CSV")]
  76. public interface ICSVPoster<TEntity> : IPoster<TEntity, CSVPosterSettings>
  77. where TEntity : Entity, IPostable, IRemotable, IPersistent, new()
  78. {
  79. bool BeforePost(IDataModel<TEntity> model);
  80. ICSVExport<TEntity> Process(IDataModel<TEntity> model);
  81. void AfterPost(IDataModel<TEntity> model, IPostResult<TEntity> result);
  82. }