add more virtual methods for base handler
This commit is contained in:
@@ -39,7 +39,7 @@ public abstract class BaseChildItemHandler<TEntity, TParent, TUpdateMsg>(IApiSer
|
||||
else
|
||||
list = server.Manager.Get<TEntity>();
|
||||
|
||||
return list.ToList().Select(ToClient).ToItemsResult();
|
||||
return SortEntities(list).ToList().Select(ToClient).ToItemsResult();
|
||||
}
|
||||
|
||||
public virtual ApiResult Post(int parent, TUpdateMsg msg, ApiRequestInfo req)
|
||||
@@ -47,10 +47,12 @@ public abstract class BaseChildItemHandler<TEntity, TParent, TUpdateMsg>(IApiSer
|
||||
if (!server.Manager.Find(parent, out TParent? parentEntity))
|
||||
return ApiResult.NotFound();
|
||||
var entity = CreateNewEntity(msg, parentEntity);
|
||||
if (UpdateEntity(new ChildEntityUpdate(entity, parentEntity, msg, req)) is ApiResult result)
|
||||
var update = new ChildEntityUpdate(entity, parentEntity, msg, req);
|
||||
if (UpdateEntity(update) is ApiResult result)
|
||||
return result;
|
||||
GetChilds(parentEntity).Add(entity);
|
||||
server.Manager.Save(parentEntity, true);
|
||||
if (OnSave(update) is ApiResult result2)
|
||||
return result2;
|
||||
return ToClient(entity).ToItemResult(HttpStatusCode.Created);
|
||||
}
|
||||
|
||||
@@ -60,9 +62,11 @@ public abstract class BaseChildItemHandler<TEntity, TParent, TUpdateMsg>(IApiSer
|
||||
return ApiResult.NotFound();
|
||||
if (!server.Manager.Find(id, out TEntity? entity))
|
||||
return ApiResult.NotFound();
|
||||
if (UpdateEntity(new ChildEntityUpdate(entity, parentEntity, msg, req)) is ApiResult result)
|
||||
var update = new ChildEntityUpdate(entity, parentEntity, msg, req);
|
||||
if (UpdateEntity(update) is ApiResult result)
|
||||
return result;
|
||||
server.Manager.Save(entity, true);
|
||||
if (OnSave(update) is ApiResult result2)
|
||||
return result2;
|
||||
return ToClient(entity).ToItemResult();
|
||||
}
|
||||
|
||||
@@ -71,5 +75,21 @@ public abstract class BaseChildItemHandler<TEntity, TParent, TUpdateMsg>(IApiSer
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user