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

45 lines
1.4 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 StoreOperationsExtensions
{
/// <summary>
/// Stream the collection
/// </summary>
public static IAsyncEnumerable<T> Stream<T>(this IStoreOperations ops) where T : ZeroIdEntity, new() => ops.Stream<T>(null);
/// <summary>
/// Deletes an entity by Id
/// </summary>
2021-11-26 15:47:11 +01:00
public static async Task<Result<T>> Delete<T>(this IStoreOperations ops, string id) where T : ZeroIdEntity, new() => await ops.Delete(await ops.Load<T>(id));
2021-11-24 15:49:25 +01:00
/// <summary>
/// Deletes entities by Id
/// </summary>
public static async Task<int> Delete<T>(this IStoreOperations ops, IEnumerable<string> ids) where T : ZeroIdEntity, new() => await ops.Delete((await ops.Load<T>(ids)).Select(x => x.Value));
/// <summary>
/// Deletes entities
/// </summary>
public static async Task<int> Delete<T>(this IStoreOperations ops, IEnumerable<T> models) where T : ZeroIdEntity, new()
{
int successCount = 0;
foreach (T model in models)
{
2021-11-26 15:47:11 +01:00
Result<T> result = await ops.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-01 15:54:11 +01:00
public static async Task<Result<string[]>> DeleteWithDescendants<T>(this IStoreOperations ops, string id) where T : ZeroIdEntity, ISupportsTrees, new() => await ops.DeleteWithDescendants(await ops.Load<T>(id));
2021-11-24 15:49:25 +01:00
}