Files
mixtape/zero.Core/Stores/EntityStoreExtensions.cs
T

46 lines
1.6 KiB
C#
Raw Normal View History

2021-11-24 15:49:25 +01:00
using Raven.Client.Documents.Linq;
namespace zero.Stores;
public static class EntityStoreExtensions
{
/// <summary>
/// Stream the collection
/// </summary>
2021-12-29 15:29:33 +01:00
public static IAsyncEnumerable<T> Stream<T>(this IEntityStore<T> store) where T : ZeroIdEntity, ISupportsFlavors, ISupportsSorting, new() => store.Stream(null);
2021-11-24 15:49:25 +01:00
/// <summary>
/// Deletes an entity by Id
/// </summary>
2021-12-29 15:29:33 +01:00
public static async Task<Result<T>> Delete<T>(this IEntityStore<T> store, string id) where T : ZeroIdEntity, ISupportsFlavors, ISupportsSorting, new() => await store.Delete(await store.Load(id));
2021-11-24 15:49:25 +01:00
/// <summary>
/// Deletes entities by Id
/// </summary>
2021-12-29 15:29:33 +01:00
public static async Task<int> Delete<T>(this IEntityStore<T> store, IEnumerable<string> ids) where T : ZeroIdEntity, ISupportsFlavors, ISupportsSorting, new() => await store.Delete((await store.Load(ids)).Select(x => x.Value));
2021-11-24 15:49:25 +01:00
/// <summary>
/// Deletes entities
/// </summary>
2021-12-29 15:29:33 +01:00
public static async Task<int> Delete<T>(this IEntityStore<T> store, IEnumerable<T> models) where T : ZeroIdEntity, ISupportsFlavors, ISupportsSorting, new()
2021-11-24 15:49:25 +01:00
{
int successCount = 0;
foreach (T model in models)
{
2021-11-26 15:47:11 +01:00
Result<T> result = await store.Delete(model);
2021-11-24 15:49:25 +01:00
successCount += result.IsSuccess ? 1 : 0;
}
return successCount;
}
/// <summary>
/// Deletes an entity by Id with all descendents
/// </summary>
2021-12-29 15:29:33 +01:00
public static async Task<Result<string[]>> DeleteWithDescendants<T>(this ITreeEntityStore<T> store, string id) where T : ZeroIdEntity, ISupportsTrees, ISupportsFlavors, ISupportsSorting, new() => await store.DeleteWithDescendants(await store.Load(id));
2021-11-24 15:49:25 +01:00
}