using Raven.Client.Documents.Operations; using Raven.Client.Documents.Queries; using Raven.Client; namespace zero.Raven; public partial class RavenOperations : IRavenOperations { /// public virtual Task> Delete(T model) where T : ZeroIdEntity, new() => Delete(model.Id); /// public virtual async Task> Delete(string id) where T : ZeroIdEntity, new() { T model = await Load(id); if (model == null) { return Result.Fail("@errors.ondelete.idnotfound"); } InterceptorInstruction instruction = Interceptors.ForDelete(model); if (InterceptorBlocker == null && !await instruction.Start(this)) { return instruction.Result; } if (model is ISupportsSoftDelete softDeleteModel) { softDeleteModel.IsDeleted = true; } else { Session.Delete(model); } await Session.SaveChangesAsync(); if (InterceptorBlocker == null) { await instruction.Complete(); } await Session.SaveChangesAsync(); return Result.Success(); } /// public virtual async Task Purge(string querySuffix = null, Parameters parameters = null) where T : ZeroIdEntity, new() { var collectionName = Store.Raven.Conventions.FindCollectionName(typeof(T)); var operationQuery = new DeleteByQueryOperation(new IndexQuery() { Query = $"from {collectionName} c {querySuffix ?? string.Empty}", QueryParameters = parameters }, new QueryOperationOptions { AllowStale = true }); Operation operation = await Store.Raven.Operations.SendAsync(operationQuery); await operation.WaitForCompletionAsync(); } }