using Microsoft.AspNetCore.Mvc; using Raven.Client.Documents.Indexes; namespace zero.Api.Abstractions; public abstract class ZeroApiTreeEntityStoreController : ZeroApiEntityStoreController where TModel : ZeroEntity, ISupportsTrees, new() where TStore : ITreeEntityStore { public ZeroApiTreeEntityStoreController(TStore store) : base(store) { } protected async Task> GetChildModels(string parentId, ListQuery query) { query.OrderQuery ??= q => q.OrderByDescending(x => x.CreatedDate); Paged result = await Store.LoadChildren(NormalizeParentId(parentId), query.Page, query.PageSize, q => q.Filter(query)); return Mapper.Map(result); } protected async Task> GetChildModels(string parentId, ListQuery query) { query.OrderQuery ??= q => q.OrderByDescending(x => x.CreatedDate); Paged result = await Store.LoadChildren(NormalizeParentId(parentId), query.Page, query.PageSize, q => q.Filter(query)); return result; } protected async Task> GetChildModelsByIndex(string parentId, ListQuery query) where TIndex : AbstractCommonApiForIndexes, new() { query.OrderQuery ??= q => q.OrderByDescending(x => x.CreatedDate); Paged result = await Store.LoadChildren(NormalizeParentId(parentId), query.Page, query.PageSize, q => q.Filter(query)); return Mapper.Map(result); } protected async Task> GetChildModelsByIndex(string parentId, ListQuery query) where TIndex : AbstractCommonApiForIndexes, new() { query.OrderQuery ??= q => q.OrderByDescending(x => x.CreatedDate); Paged result = await Store.LoadChildren(NormalizeParentId(parentId), query.Page, query.PageSize, q => q.Filter(query)); return result; } protected async Task> MoveModel(string id, string newParentId) { return await PutOperation(async () => await Store.Move(id, NormalizeParentId(newParentId))); } protected async Task> MoveModel(string id, string newParentId) { return await PutOperation(async () => await Store.Move(id, NormalizeParentId(newParentId))); } protected async Task> CopyModel(string id, string newParentId) { return await PutOperation(async () => await Store.Copy(id, NormalizeParentId(newParentId))); } protected async Task> CopyModel(string id, string newParentId) { return await PutOperation(async () => await Store.Copy(id, NormalizeParentId(newParentId))); } protected async Task> CopyModelWithDescendants(string id, string newParentId) { return await PutOperation(async () => await Store.CopyWithDescendants(id, NormalizeParentId(newParentId))); } protected async Task> CopyModelWithDescendants(string id, string newParentId) { return await PutOperation(async () => await Store.CopyWithDescendants(id, NormalizeParentId(newParentId))); } protected async Task>> DeleteModelWithDescendants(string id) { TModel model = await Store.Load(id); if (model == null) { return NotFound(); } Result result = await Store.DeleteWithDescendants(model); return result; } async Task> PutOperation(Func>> action) { Result result = await action(); if (Hints.ResponsePreference == ApiResponsePreference.Minimal) { return result.WithoutModel(); } return Mapper.Map(result); } async Task> PutOperation(Func>> action) { Result result = await action(); if (Hints.ResponsePreference == ApiResponsePreference.Minimal) { return result.WithoutModel(); } return result; } protected string NormalizeParentId(string id) { return id == "root" ? null : id; } }