2021-11-20 13:52:28 +01:00
|
|
|
using System.IO;
|
2020-10-07 16:08:09 +02:00
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
namespace zero.Utils;
|
|
|
|
|
|
|
|
|
|
public abstract class TableExport<TEntity> : ITableExport<TEntity> where TEntity : ZeroIdEntity
|
2020-10-07 16:08:09 +02:00
|
|
|
{
|
2021-11-20 13:52:28 +01:00
|
|
|
public virtual async Task<Stream> Export(TableFormat format = TableFormat.Excel)
|
2020-10-07 16:08:09 +02:00
|
|
|
{
|
2021-11-20 13:52:28 +01:00
|
|
|
ITableBuilder<TEntity> builder = new TableBuilder<TEntity>(format);
|
2020-10-07 16:08:09 +02:00
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
await Warmup();
|
2021-10-07 15:53:43 +02:00
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
Build(builder);
|
2020-10-07 16:08:09 +02:00
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
IAsyncEnumerable<TEntity> items = Load();
|
2020-10-07 16:08:09 +02:00
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
MemoryStream stream = await builder.ToStream(items);
|
2020-10-07 16:08:09 +02:00
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
return stream;
|
2020-10-07 16:08:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
protected virtual Task Warmup() => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual async IAsyncEnumerable<TEntity> Load()
|
2020-10-07 16:08:09 +02:00
|
|
|
{
|
2021-11-20 13:52:28 +01:00
|
|
|
await Task.Delay(0);
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Build(ITableBuilder<TEntity> builder)
|
|
|
|
|
{
|
|
|
|
|
builder.Column("Id").For(c => c.Id).Size(40);
|
2020-10-07 16:08:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-20 13:52:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface ITableExport<TEntity> where TEntity : ZeroIdEntity
|
|
|
|
|
{
|
|
|
|
|
Task<Stream> Export(TableFormat format = TableFormat.Excel);
|
|
|
|
|
}
|