return IOrderedQueryable and order by id by default

This commit is contained in:
Pilzinsel64
2025-11-21 08:45:09 +01:00
parent 42bb1f884a
commit 7c6c216b02
3 changed files with 10 additions and 15 deletions

View File

@@ -111,9 +111,4 @@ public abstract class BaseChildItemHandler<TEntity, TParent, TUpdateMsg>(IApiSer
}
public abstract IList<TEntity> GetChilds(TParent parent);
protected virtual IQueryable<TEntity> SortEntities(IQueryable<TEntity> entities)
{
return entities;
}
}

View File

@@ -19,6 +19,8 @@ public abstract class BaseHandler<TEntity, TUpdateMsg>(IApiServer server)
public event EventHandler<TEntity>? OnDeleteItem;
protected readonly bool isNamedObjectType = typeof(TEntity).IsAssignableTo(typeof(INamedObject));
/// <summary>
/// Gets the base route (endpoint) for the most API calls.
/// </summary>
@@ -102,6 +104,13 @@ public abstract class BaseHandler<TEntity, TUpdateMsg>(IApiServer server)
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 IDataObject ToClient(TEntity entity);

View File

@@ -11,8 +11,6 @@ public abstract class BaseItemHandler<TEntity, TUpdateMsg>(IApiServer server)
where TEntity : class, IDataObject
where TUpdateMsg : ApiMessage
{
protected readonly bool isNamedObjectType = typeof(TEntity).IsAssignableTo(typeof(INamedObject));
protected virtual bool RegisterGetAll => 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)
{
var entities = SortEntities(FilterByIDs(server.Manager.Get<TEntity>(), ids));
var entities = (IQueryable<TEntity>)SortEntities(FilterByIDs(server.Manager.Get<TEntity>(), ids));
if (offset > 0)
entities = entities.Skip(offset);
if (amount > 0)
@@ -46,11 +44,4 @@ public abstract class BaseItemHandler<TEntity, TUpdateMsg>(IApiServer server)
return result2;
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;
}
}