1
0
mirror of https://github.com/bitwarden/server synced 2025-12-13 14:53:34 +00:00

Create DataTableBuilder (#4608)

* Add DataTableBuilder Using Expressions

* Format

* Unwrap Underlying Enum Type

* Formatting
This commit is contained in:
Justin Baur
2024-09-05 20:44:45 -04:00
committed by GitHub
parent ec2522de8b
commit 329eef82cd
6 changed files with 386 additions and 41 deletions

View File

@@ -8,6 +8,26 @@ namespace Bit.Infrastructure.Dapper.Tools.Helpers;
/// </summary>
public static class SendHelpers
{
private static readonly DataTableBuilder<Send> _sendTableBuilder = new(
[
s => s.Id,
s => s.UserId,
s => s.OrganizationId,
s => s.Type,
s => s.Data,
s => s.Key,
s => s.Password,
s => s.MaxAccessCount,
s => s.AccessCount,
s => s.CreationDate,
s => s.RevisionDate,
s => s.ExpirationDate,
s => s.DeletionDate,
s => s.Disabled,
s => s.HideEmail,
]
);
/// <summary>
/// Converts an IEnumerable of Sends to a DataTable
/// </summary>
@@ -16,27 +36,6 @@ public static class SendHelpers
/// <returns>A data table matching the schema of dbo.Send containing one row mapped from the items in <see cref="Send"/>s</returns>
public static DataTable ToDataTable(this IEnumerable<Send> sends)
{
var sendsTable = new DataTable();
var columnData = new List<(string name, Type type, Func<Send, object> getter)>
{
(nameof(Send.Id), typeof(Guid), c => c.Id),
(nameof(Send.UserId), typeof(Guid), c => c.UserId),
(nameof(Send.OrganizationId), typeof(Guid), c => c.OrganizationId),
(nameof(Send.Type), typeof(short), c => c.Type),
(nameof(Send.Data), typeof(string), c => c.Data),
(nameof(Send.Key), typeof(string), c => c.Key),
(nameof(Send.Password), typeof(string), c => c.Password),
(nameof(Send.MaxAccessCount), typeof(int), c => c.MaxAccessCount),
(nameof(Send.AccessCount), typeof(int), c => c.AccessCount),
(nameof(Send.CreationDate), typeof(DateTime), c => c.CreationDate),
(nameof(Send.RevisionDate), typeof(DateTime), c => c.RevisionDate),
(nameof(Send.ExpirationDate), typeof(DateTime), c => c.ExpirationDate),
(nameof(Send.DeletionDate), typeof(DateTime), c => c.DeletionDate),
(nameof(Send.Disabled), typeof(bool), c => c.Disabled),
(nameof(Send.HideEmail), typeof(bool), c => c.HideEmail),
};
return sends.BuildTable(sendsTable, columnData);
return _sendTableBuilder.Build(sends ?? []);
}
}