2021-11-24 13:56:08 +01:00
|
|
|
using Raven.Client.Documents.Indexes;
|
|
|
|
|
using Raven.Client.Documents.Linq;
|
|
|
|
|
|
|
|
|
|
namespace zero.Stores;
|
|
|
|
|
|
2021-12-29 15:29:33 +01:00
|
|
|
public abstract class TreeEntityStore<T> : EntityStore<T>, ITreeEntityStore<T> where T : ZeroIdEntity, ISupportsTrees, ISupportsFlavors, ISupportsSorting, new()
|
2021-11-24 13:56:08 +01:00
|
|
|
{
|
|
|
|
|
public TreeEntityStore(IStoreContext collectionContext) : base(collectionContext) { }
|
|
|
|
|
|
2021-11-25 15:38:36 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public override async Task<Result<T>> Create(T model)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
if (!await IsAllowedAsChild(model, model.ParentId))
|
|
|
|
|
{
|
2021-11-26 15:47:11 +01:00
|
|
|
return Result<T>.Fail("@errors.treeentity.parentnotallowed");
|
2021-11-25 15:38:36 +01:00
|
|
|
}
|
|
|
|
|
return await base.Create(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public override async Task<Result<T>> Update(T model)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
if (!await IsAllowedAsChild(model, model.ParentId))
|
|
|
|
|
{
|
2021-11-26 15:47:11 +01:00
|
|
|
return Result<T>.Fail("@errors.treeentity.parentnotallowed");
|
2021-11-25 15:38:36 +01:00
|
|
|
}
|
|
|
|
|
return await base.Update(model);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 13:56:08 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual Task<bool> IsAllowedAsChild(T model, string parentId) => Task.FromResult(true);
|
|
|
|
|
|
2021-12-16 13:55:09 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual Task<Paged<T>> LoadChildren(string parentId, int pageNumber, int pageSize, Func<IRavenQueryable<T>, IQueryable<T>> querySelector = null)
|
|
|
|
|
=> Operations.LoadChildren(parentId, pageNumber, pageSize, querySelector);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual Task<Paged<T>> LoadChildren<TIndex>(string parentId, int pageNumber, int pageSize, Func<IRavenQueryable<T>, IQueryable<T>> querySelector = null) where TIndex : AbstractCommonApiForIndexes, new()
|
|
|
|
|
=> Operations.LoadChildren<T, TIndex>(parentId, pageNumber, pageSize, querySelector);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual Task<T[]> GetHierarchy<TIndex>(string id) where TIndex : ZeroTreeHierarchyIndex<T>, new() => Operations.GetHierarchy<T, TIndex>(id);
|
|
|
|
|
|
2021-11-24 13:56:08 +01:00
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual Task<Result<T>> Copy(string id, string newParentId) => Operations.Copy<T>(id, newParentId, async (model, parentId) => await IsAllowedAsChild(model, parentId));
|
2021-11-24 13:56:08 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual Task<Result<T>> CopyWithDescendants(string id, string newParentId) => Operations.CopyWithDescendants<T>(id, newParentId, async (model, parentId) => await IsAllowedAsChild(model, parentId));
|
2021-11-24 13:56:08 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual Task<Result<T>> Move(string id, string newParentId) => Operations.Move<T>(id, newParentId, async (model, parentId) => await IsAllowedAsChild(model, parentId));
|
2021-11-24 13:56:08 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual Task<Result<string[]>> DeleteWithDescendants(T model) => Operations.DeleteWithDescendants(model);
|
2021-11-24 13:56:08 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual Task<Result<string[]>> DeleteWithDescendants(string id) => Operations.DeleteWithDescendants<T>(id);
|
2021-11-24 13:56:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-12-29 15:29:33 +01:00
|
|
|
public interface ITreeEntityStore<T> : IEntityStore<T> where T : ZeroIdEntity, ISupportsTrees, ISupportsFlavors, ISupportsSorting, new()
|
2021-11-24 13:56:08 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether a model is allowed as a child for a new parent.
|
|
|
|
|
/// This is primarily used by copy/move operations
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<bool> IsAllowedAsChild(T model, string parentId);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads all children for an entity
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Paged<T>> LoadChildren(string parentId, int pageNumber, int pageSize, Func<IRavenQueryable<T>, IQueryable<T>> querySelector = default);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get descendants by query (by using the specified index)
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Paged<T>> LoadChildren<TIndex>(string parentId, int pageNumber, int pageSize, Func<IRavenQueryable<T>, IQueryable<T>> querySelector = default) where TIndex : AbstractCommonApiForIndexes, new();
|
|
|
|
|
|
2021-12-16 13:55:09 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get tree hierarchy for an entity
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<T[]> GetHierarchy<TIndex>(string id) where TIndex : ZeroTreeHierarchyIndex<T>, new();
|
|
|
|
|
|
2021-11-24 13:56:08 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Move an entity to a new parent
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<T>> Move(string id, string newParentId);
|
2021-11-24 13:56:08 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies an entity to a new location
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<T>> Copy(string id, string newParentId);
|
2021-11-24 13:56:08 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies an entity with descendants to a new location
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<T>> CopyWithDescendants(string id, string newParentId);
|
2021-11-24 13:56:08 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes an entity with all descendents
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<string[]>> DeleteWithDescendants(T model);
|
2021-11-24 13:56:08 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes an entity by Id with all descendents
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<string[]>> DeleteWithDescendants(string id);
|
2021-11-24 13:56:08 +01:00
|
|
|
}
|