using Raven.Client.Documents.Linq;
namespace zero.Stores;
public static class StoreOperationsExtensions
{
///
/// Stream the collection
///
public static IAsyncEnumerable Stream(this IStoreOperations ops) where T : ZeroIdEntity, new() => ops.Stream(null);
///
/// Deletes an entity by Id
///
public static async Task> Delete(this IStoreOperations ops, string id) where T : ZeroIdEntity, new() => await ops.Delete(await ops.Load(id));
///
/// Deletes entities by Id
///
public static async Task Delete(this IStoreOperations ops, IEnumerable ids) where T : ZeroIdEntity, new() => await ops.Delete((await ops.Load(ids)).Select(x => x.Value));
///
/// Deletes entities
///
public static async Task Delete(this IStoreOperations ops, IEnumerable models) where T : ZeroIdEntity, new()
{
int successCount = 0;
foreach (T model in models)
{
Result result = await ops.Delete(model);
successCount += result.IsSuccess ? 1 : 0;
}
return successCount;
}
///
/// Deletes an entity by Id with all descendents
///
public static async Task> DeleteWithDescendants(this IStoreOperations ops, string id) where T : ZeroIdEntity, ISupportsTrees, new() => await ops.DeleteWithDescendants(await ops.Load(id));
}