Files
mixtape/zero.Api/Abstractions/ZeroApiTreeEntityStoreController.cs
T

131 lines
4.1 KiB
C#
Raw Normal View History

2021-12-03 14:45:49 +01:00
using Microsoft.AspNetCore.Mvc;
using Raven.Client.Documents.Indexes;
2021-12-12 15:41:51 +01:00
namespace zero.Api.Abstractions;
2021-12-03 14:45:49 +01:00
public abstract class ZeroApiTreeEntityStoreController<TModel, TStore> : ZeroApiEntityStoreController<TModel, TStore>
where TModel : ZeroEntity, ISupportsTrees, new()
where TStore : ITreeEntityStore<TModel>
{
public ZeroApiTreeEntityStoreController(TStore store) : base(store) { }
2021-12-16 00:19:51 +01:00
protected async Task<ActionResult<Paged>> GetChildModels<T>(string parentId, ListQuery<TModel> query)
2021-12-03 14:45:49 +01:00
{
query.OrderQuery ??= q => q.OrderByDescending(x => x.CreatedDate);
2021-12-29 01:25:35 +01:00
Paged<TModel> result = await Store.LoadChildren(NormalizeParentId(parentId), query.Page, query.PageSize, q => q.Filter(query));
2021-12-03 14:45:49 +01:00
2021-12-14 01:23:03 +01:00
return Mapper.Map<TModel, T>(result);
2021-12-03 14:45:49 +01:00
}
2021-12-15 21:44:06 +01:00
protected async Task<ActionResult<Paged>> GetChildModels(string parentId, ListQuery<TModel> query)
{
query.OrderQuery ??= q => q.OrderByDescending(x => x.CreatedDate);
2021-12-29 01:25:35 +01:00
Paged<TModel> result = await Store.LoadChildren(NormalizeParentId(parentId), query.Page, query.PageSize, q => q.Filter(query));
2021-12-15 21:44:06 +01:00
return result;
}
2021-12-16 00:19:51 +01:00
protected async Task<ActionResult<Paged>> GetChildModelsByIndex<T, TIndex>(string parentId, ListQuery<TModel> query) where TIndex : AbstractCommonApiForIndexes, new()
2021-12-03 14:45:49 +01:00
{
query.OrderQuery ??= q => q.OrderByDescending(x => x.CreatedDate);
2021-12-29 01:25:35 +01:00
Paged<TModel> result = await Store.LoadChildren<TIndex>(NormalizeParentId(parentId), query.Page, query.PageSize, q => q.Filter(query));
2021-12-03 14:45:49 +01:00
2021-12-14 01:23:03 +01:00
return Mapper.Map<TModel, T>(result);
2021-12-03 14:45:49 +01:00
}
2021-12-15 21:44:06 +01:00
protected async Task<ActionResult<Paged>> GetChildModelsByIndex<TIndex>(string parentId, ListQuery<TModel> query) where TIndex : AbstractCommonApiForIndexes, new()
{
query.OrderQuery ??= q => q.OrderByDescending(x => x.CreatedDate);
2021-12-29 01:25:35 +01:00
Paged<TModel> result = await Store.LoadChildren<TIndex>(NormalizeParentId(parentId), query.Page, query.PageSize, q => q.Filter(query));
2021-12-15 21:44:06 +01:00
return result;
}
2021-12-16 16:59:27 +01:00
protected async Task<ActionResult<Result>> MoveModel<TEdit>(string id, string newParentId)
2021-12-03 14:45:49 +01:00
{
2021-12-29 01:25:35 +01:00
return await PutOperation<TEdit>(async () => await Store.Move(id, NormalizeParentId(newParentId)));
2021-12-03 14:45:49 +01:00
}
2021-12-16 16:59:27 +01:00
protected async Task<ActionResult<Result>> MoveModel(string id, string newParentId)
2021-12-15 21:44:06 +01:00
{
2021-12-29 01:25:35 +01:00
return await PutOperation(async () => await Store.Move(id, NormalizeParentId(newParentId)));
2021-12-15 21:44:06 +01:00
}
2021-12-16 16:59:27 +01:00
protected async Task<ActionResult<Result>> CopyModel<TEdit>(string id, string newParentId)
2021-12-03 14:45:49 +01:00
{
2021-12-29 01:25:35 +01:00
return await PutOperation<TEdit>(async () => await Store.Copy(id, NormalizeParentId(newParentId)));
2021-12-03 14:45:49 +01:00
}
2021-12-16 16:59:27 +01:00
protected async Task<ActionResult<Result>> CopyModel(string id, string newParentId)
2021-12-15 21:44:06 +01:00
{
2021-12-29 01:25:35 +01:00
return await PutOperation(async () => await Store.Copy(id, NormalizeParentId(newParentId)));
2021-12-15 21:44:06 +01:00
}
2021-12-16 16:59:27 +01:00
protected async Task<ActionResult<Result>> CopyModelWithDescendants<TEdit>(string id, string newParentId)
2021-12-03 14:45:49 +01:00
{
2021-12-29 01:25:35 +01:00
return await PutOperation<TEdit>(async () => await Store.CopyWithDescendants(id, NormalizeParentId(newParentId)));
2021-12-03 14:45:49 +01:00
}
2021-12-16 16:59:27 +01:00
protected async Task<ActionResult<Result>> CopyModelWithDescendants(string id, string newParentId)
2021-12-15 21:44:06 +01:00
{
2021-12-29 01:25:35 +01:00
return await PutOperation(async () => await Store.CopyWithDescendants(id, NormalizeParentId(newParentId)));
2021-12-15 21:44:06 +01:00
}
protected async Task<ActionResult<Result<string[]>>> DeleteModelWithDescendants(string id)
2021-12-03 14:45:49 +01:00
{
TModel model = await Store.Load(id);
if (model == null)
{
return NotFound();
}
Result<string[]> result = await Store.DeleteWithDescendants(model);
return result;
2021-12-03 14:45:49 +01:00
}
2021-12-16 00:19:51 +01:00
async Task<ActionResult<Result>> PutOperation<TEdit>(Func<Task<Result<TModel>>> action)
2021-12-03 14:45:49 +01:00
{
Result<TModel> result = await action();
if (Hints.ResponsePreference == ApiResponsePreference.Minimal)
{
return result.WithoutModel();
}
return Mapper.Map<TModel, TEdit>(result);
}
2021-12-15 21:44:06 +01:00
async Task<ActionResult<Result>> PutOperation(Func<Task<Result<TModel>>> action)
{
Result<TModel> result = await action();
if (Hints.ResponsePreference == ApiResponsePreference.Minimal)
{
return result.WithoutModel();
}
return result;
}
2021-12-29 01:25:35 +01:00
protected string NormalizeParentId(string id)
{
return id == "root" ? null : id;
}
2021-12-03 14:45:49 +01:00
}