using CsvHelper.Configuration; using InABox.Core; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; namespace InABox.Poster.CSV { public interface ICSVClassMap { ClassMap ClassMap { get; } } public interface ICSVClassMap : ICSVClassMap { new ClassMap ClassMap { get; } ClassMap ICSVClassMap.ClassMap => ClassMap; void Map(string name, Expression> expr); } public class CSVClassMap : ClassMap, ICSVClassMap { public ClassMap ClassMap => this; public void Map(string name, Expression> expr) { Map(expr).Name(name); } } public interface ICSVExport { Type Type { get; } ICSVClassMap ClassMap { get; } IEnumerable Records { get; } } public interface ICSVExport : ICSVExport where T : class { new Type Type => typeof(T); new CSVClassMap ClassMap { get; } new IEnumerable Records { get; } Type ICSVExport.Type => Type; ICSVClassMap ICSVExport.ClassMap => ClassMap; IEnumerable ICSVExport.Records => Records; } public class CSVExport : ICSVExport where T : class { public CSVClassMap ClassMap { get; } = new CSVClassMap(); public IEnumerable Records => Items; public List Items { get; } = new List(); public void DefineMapping(List>>> mappings) { foreach(var (name, expr) in mappings) { ClassMap.Map(name, expr); } } public void Map(string name, Expression> expr) { ClassMap.Map(name, expr); } public void Add(T item) { Items.Add(item); } public void AddRange(IEnumerable items) { Items.AddRange(items); } } /// /// Defines an interface for posters that can export to CSV. /// /// public interface ICSVPoster : IPoster> where TEntity : Entity, IPostable { ICSVExport Process(IEnumerable entities); } }