Files
mixtape/mixtape.Raven/Operations/RavenOperationsExtensions.cs
2026-05-05 11:34:32 +02:00

49 lines
1.7 KiB
C#

using System.Linq.Expressions;
namespace Mixtape.Raven;
public static class RavenOperationsExtensions
{
/// <summary>
/// Stream the collection
/// </summary>
public static IAsyncEnumerable<T> Stream<T>(this IRavenOperations ops) where T : MixtapeIdEntity, new() => ops.Stream<T>(null);
/// <summary>
/// Deletes an entity by Id
/// </summary>
public static async Task<Result<T>> Delete<T>(this IRavenOperations ops, string id) where T : MixtapeIdEntity, new() => await ops.Delete(await ops.Load<T>(id));
/// <summary>
/// Deletes entities by selector
/// </summary>
public static async Task<int> Delete<T>(this IRavenOperations ops, Expression<Func<T, bool>> predicate) where T : MixtapeIdEntity, new() => await ops.Delete(await ops.Load<T>(predicate));
/// <summary>
/// Deletes entities by Id
/// </summary>
public static async Task<int> Delete<T>(this IRavenOperations ops, IEnumerable<string> ids) where T : MixtapeIdEntity, 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 IRavenOperations ops, IEnumerable<T> models) where T : MixtapeIdEntity, new()
{
int successCount = 0;
foreach (T model in models)
{
Result<T> result = await ops.Delete(model);
successCount += result.IsSuccess ? 1 : 0;
}
return successCount;
}
/// <summary>
/// Deletes an entity by Id with all descendents
/// </summary>
public static async Task<Result<string[]>> DeleteWithDescendants<T>(this IRavenOperations ops, string id) where T : MixtapeIdEntity, ISupportsTrees, new() => await ops.DeleteWithDescendants(await ops.Load<T>(id));
}