return IOrderedQueryable and order by id by default
This commit is contained in:
@@ -111,9 +111,4 @@ public abstract class BaseChildItemHandler<TEntity, TParent, TUpdateMsg>(IApiSer
|
|||||||
}
|
}
|
||||||
|
|
||||||
public abstract IList<TEntity> GetChilds(TParent parent);
|
public abstract IList<TEntity> GetChilds(TParent parent);
|
||||||
|
|
||||||
protected virtual IQueryable<TEntity> SortEntities(IQueryable<TEntity> entities)
|
|
||||||
{
|
|
||||||
return entities;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ public abstract class BaseHandler<TEntity, TUpdateMsg>(IApiServer server)
|
|||||||
|
|
||||||
public event EventHandler<TEntity>? OnDeleteItem;
|
public event EventHandler<TEntity>? OnDeleteItem;
|
||||||
|
|
||||||
|
protected readonly bool isNamedObjectType = typeof(TEntity).IsAssignableTo(typeof(INamedObject));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the base route (endpoint) for the most API calls.
|
/// Gets the base route (endpoint) for the most API calls.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -102,6 +104,13 @@ public abstract class BaseHandler<TEntity, TUpdateMsg>(IApiServer server)
|
|||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual IOrderedQueryable<TEntity> SortEntities(IQueryable<TEntity> entities)
|
||||||
|
{
|
||||||
|
if (isNamedObjectType)
|
||||||
|
return entities.OrderBy(n => ((INamedObject)n).Name);
|
||||||
|
return entities.OrderBy(n => n.Id);
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract ApiResult? UpdateEntity(EntityUpdate update);
|
protected abstract ApiResult? UpdateEntity(EntityUpdate update);
|
||||||
|
|
||||||
protected abstract IDataObject ToClient(TEntity entity);
|
protected abstract IDataObject ToClient(TEntity entity);
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ public abstract class BaseItemHandler<TEntity, TUpdateMsg>(IApiServer server)
|
|||||||
where TEntity : class, IDataObject
|
where TEntity : class, IDataObject
|
||||||
where TUpdateMsg : ApiMessage
|
where TUpdateMsg : ApiMessage
|
||||||
{
|
{
|
||||||
protected readonly bool isNamedObjectType = typeof(TEntity).IsAssignableTo(typeof(INamedObject));
|
|
||||||
|
|
||||||
protected virtual bool RegisterGetAll => true;
|
protected virtual bool RegisterGetAll => true;
|
||||||
protected virtual bool RegisterPost => true;
|
protected virtual bool RegisterPost => true;
|
||||||
|
|
||||||
@@ -28,7 +26,7 @@ public abstract class BaseItemHandler<TEntity, TUpdateMsg>(IApiServer server)
|
|||||||
|
|
||||||
public virtual ApiResult GetAll(string? ids, int offset, int amount)
|
public virtual ApiResult GetAll(string? ids, int offset, int amount)
|
||||||
{
|
{
|
||||||
var entities = SortEntities(FilterByIDs(server.Manager.Get<TEntity>(), ids));
|
var entities = (IQueryable<TEntity>)SortEntities(FilterByIDs(server.Manager.Get<TEntity>(), ids));
|
||||||
if (offset > 0)
|
if (offset > 0)
|
||||||
entities = entities.Skip(offset);
|
entities = entities.Skip(offset);
|
||||||
if (amount > 0)
|
if (amount > 0)
|
||||||
@@ -46,11 +44,4 @@ public abstract class BaseItemHandler<TEntity, TUpdateMsg>(IApiServer server)
|
|||||||
return result2;
|
return result2;
|
||||||
return ToClient(entity).ToItemResult(HttpStatusCode.Created);
|
return ToClient(entity).ToItemResult(HttpStatusCode.Created);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual IQueryable<TEntity> SortEntities(IQueryable<TEntity> entities)
|
|
||||||
{
|
|
||||||
if (isNamedObjectType)
|
|
||||||
return entities.OrderBy(n => ((INamedObject)n).Name);
|
|
||||||
return entities;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user