using FluentValidation.Results; using Raven.Client.Documents.Indexes; using Raven.Client.Documents.Linq; namespace zero.Collections; public abstract partial class EntityCollection : IEntityCollection where T : ZeroIdEntity, new() { /// public Guid Guid { get; private set; } = Guid.NewGuid(); /// public IZeroDocumentSession Session => Context.Store.Session(); protected record EntityCollectionOptions(bool IncludeInactive); protected IZeroContext Context { get; private set; } protected EntityCollectionOptions Options { get; set; } protected IInterceptors Interceptors { get; private set; } protected ICollectionOperations Operations { get; private set; } public EntityCollection(ICollectionContext collectionContext) { Operations = collectionContext.Operations; Context = collectionContext.Context; Interceptors = collectionContext.Interceptors; Options = new(true); } /// public virtual Task Empty() => Operations.Empty(); /// public virtual Task Load(string id, string changeVector = null) => Operations.Load(id, changeVector); /// public virtual Task> Load(IEnumerable ids) => Operations.Load(ids); /// public virtual Task> Load(int pageNumber, int pageSize, Func, IQueryable> querySelector = default) => Operations.Load(pageNumber, pageSize, querySelector); /// public virtual Task> Load(int pageNumber, int pageSize, Func, IQueryable> querySelector = default) where TIndex : AbstractCommonApiForIndexes, new() => Operations.Load(pageNumber, pageSize, querySelector); /// public virtual Task> LoadAll() => Operations.LoadAll(); /// public virtual IAsyncEnumerable Stream() => Operations.Stream(); /// public virtual IAsyncEnumerable Stream(Func, IQueryable> expression) => Operations.Stream(expression); /// public virtual Task> Create(T model) => Operations.Create(model, async m => await Validate(m)); /// public virtual Task> Update(T model) => Operations.Update(model, async m => await Validate(m)); /// public virtual Task> Delete(T model) => Operations.Delete(model); /// public virtual Task Delete(IEnumerable models) => Operations.Delete(models); /// public virtual Task> Delete(string id) => Operations.Delete(id); /// public virtual Task Delete(IEnumerable ids) => Operations.Delete(ids); /// public virtual async Task Validate(T model) { ZeroValidator validator = new(); ValidationRules(validator); return await validator.ValidateAsync(model); } /// /// Create rules for validation /// protected virtual void ValidationRules(ZeroValidator validator) { } /// /// Do only return the model when it is set to active or inactive entities are included with IncludeInactive() /// protected virtual T WhenActive(T model) => model != null && (Options.IncludeInactive || (model is ZeroEntity ? (model as ZeroEntity).IsActive : true)) ? model : default; } public interface IEntityCollection where T : ZeroIdEntity, new() { /// /// Get new instance of an entity /// Task Empty(); /// /// Get an entity by Id /// Task Load(string id, string changeVector = null); /// /// Get entities by ids /// Task> Load(IEnumerable ids); /// /// Get entities by query /// Task> Load(int pageNumber, int pageSize, Func, IQueryable> querySelector = default); /// /// Get entities by query (by using the specified index) /// Task> Load(int pageNumber, int pageSize, Func, IQueryable> querySelector = default) where TIndex : AbstractCommonApiForIndexes, new(); /// /// Get all entities from this collection. /// Warning: Don't use this method for large collections. Stream the results instead. /// Task> LoadAll(); /// /// Stream the collection /// IAsyncEnumerable Stream(); /// /// Stream the collection /// IAsyncEnumerable Stream(Func, IQueryable> expression); /// /// Validates an entity in this collection /// Task Validate(T model); /// /// Creates an entity with an optional validator /// Task> Create(T model); /// /// Updates an entity with an optional validator /// Task> Update(T model); /// /// Deletes an entity /// Task> Delete(T model); /// /// Deletes entities /// Task Delete(IEnumerable models); /// /// Deletes an entity by Id /// Task> Delete(string id); /// /// Deletes entities by Id /// Task Delete(IEnumerable ids); }