96 lines
3.3 KiB
C#
96 lines
3.3 KiB
C#
using Pilz.Data;
|
|
using Pilz.Extensions.Reflection;
|
|
using Pilz.Net.Extensions;
|
|
using System.Diagnostics;
|
|
using System.Net;
|
|
|
|
namespace Pilz.Net.Api.Server;
|
|
|
|
public abstract class BaseChildItemHandler<TEntity, TParent, TUpdateMsg>(IApiServer server)
|
|
: BaseHandler<TEntity, TUpdateMsg>(server)
|
|
where TEntity : class, IDataObject
|
|
where TParent : class, IDataObject
|
|
where TUpdateMsg : ApiMessage
|
|
{
|
|
public class ChildEntityUpdate(TEntity entity, TParent parent, TUpdateMsg message, ApiRequestInfo reqest) : EntityUpdate(entity, message, reqest)
|
|
{
|
|
public TParent Parent { get; } = parent;
|
|
}
|
|
|
|
protected virtual bool RegisterGetAll => true;
|
|
protected virtual bool RegisterPost => true;
|
|
|
|
public override void Initialize(IApiServer server)
|
|
{
|
|
base.Initialize(server);
|
|
var t = GetType();
|
|
if (RegisterGetAll)
|
|
server.RegisterHandler(t.GetMethod(nameof(GetAll))!.CreateDelegate(this), new(Route, "GET"), Debugger.IsAttached);
|
|
if (RegisterPost)
|
|
server.RegisterHandler(t.GetMethod(nameof(Post))!.CreateDelegate(this), new(Route, "POST"), Debugger.IsAttached);
|
|
}
|
|
|
|
public virtual ApiResult GetAll(int parent)
|
|
{
|
|
IQueryable<TEntity> list;
|
|
|
|
if (parent != 0 && server.Manager.Find(parent, out TParent? parentEntity))
|
|
list = GetChilds(parentEntity).AsQueryable();
|
|
else
|
|
list = server.Manager.Get<TEntity>();
|
|
|
|
return SortEntities(list).ToList().Select(ToClient).ToItemsResult();
|
|
}
|
|
|
|
public virtual ApiResult Post(int parent, TUpdateMsg msg, ApiRequestInfo req)
|
|
{
|
|
if (!server.Manager.Find(parent, out TParent? parentEntity))
|
|
return ApiResult.NotFound();
|
|
var entity = CreateNewEntity(msg, parentEntity);
|
|
var update = new ChildEntityUpdate(entity, parentEntity, msg, req);
|
|
if (UpdateEntity(update) is ApiResult result)
|
|
return result;
|
|
GetChilds(parentEntity).Add(entity);
|
|
if (OnSave(update) is ApiResult result2)
|
|
return result2;
|
|
return ToClient(entity).ToItemResult(HttpStatusCode.Created);
|
|
}
|
|
|
|
public override ApiResult Put(int id, TUpdateMsg msg, ApiRequestInfo req)
|
|
{
|
|
if (server.Manager.Get<TParent>().FirstOrDefault(n => GetChilds(n).Any(n => n.Id == id)) is not TParent parentEntity)
|
|
return ApiResult.NotFound();
|
|
if (!server.Manager.Find(id, out TEntity? entity))
|
|
return ApiResult.NotFound();
|
|
var update = new ChildEntityUpdate(entity, parentEntity, msg, req);
|
|
if (UpdateEntity(update) is ApiResult result)
|
|
return result;
|
|
if (OnSave(update) is ApiResult result2)
|
|
return result2;
|
|
return ToClient(entity).ToItemResult();
|
|
}
|
|
|
|
protected virtual TEntity CreateNewEntity(TUpdateMsg msg, TParent parent)
|
|
{
|
|
return CreateNewEntity(msg);
|
|
}
|
|
|
|
protected override ApiResult? OnSave(EntityUpdate update)
|
|
{
|
|
if (update.Entity.Id == 0 && update is ChildEntityUpdate updateEx)
|
|
{
|
|
server.Manager.Save(updateEx.Parent, true);
|
|
return null;
|
|
}
|
|
else
|
|
return base.OnSave(update);
|
|
}
|
|
|
|
public abstract IList<TEntity> GetChilds(TParent parent);
|
|
|
|
protected virtual IQueryable<TEntity> SortEntities(IQueryable<TEntity> entities)
|
|
{
|
|
return entities;
|
|
}
|
|
}
|