Add sort handlers for create and update date (#14178)

* Add sort handlers for create and update date

* Make things sealed

* Update the field names

---------

Co-authored-by: Elitsa <elm@umbraco.dk>
This commit is contained in:
Kenn Jacobsen
2023-04-28 11:14:50 +02:00
committed by GitHub
parent 376c0bf7ba
commit fc3f342906
5 changed files with 86 additions and 1 deletions

View File

@@ -0,0 +1,15 @@
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Api.Delivery.Indexing.Sorts;
public sealed class CreateDateSortIndexer : IContentIndexHandler
{
internal const string FieldName = "createDate";
public IEnumerable<IndexFieldValue> GetFieldValues(IContent content)
=> new[] { new IndexFieldValue { FieldName = FieldName, Value = content.CreateDate } };
public IEnumerable<IndexField> GetFields()
=> new[] { new IndexField { FieldName = FieldName, FieldType = FieldType.Date } };
}

View File

@@ -0,0 +1,15 @@
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Api.Delivery.Indexing.Sorts;
public sealed class UpdateDateSortIndexer : IContentIndexHandler
{
internal const string FieldName = "updateDate";
public IEnumerable<IndexFieldValue> GetFieldValues(IContent content)
=> new[] { new IndexFieldValue { FieldName = FieldName, Value = content.UpdateDate } };
public IEnumerable<IndexField> GetFields()
=> new[] { new IndexField { FieldName = FieldName, FieldType = FieldType.Date } };
}

View File

@@ -1,3 +1,4 @@
using Umbraco.Cms.Api.Delivery.Indexing.Sorts;
using Umbraco.Cms.Core.DeliveryApi;
namespace Umbraco.Cms.Api.Delivery.Querying.Filters;
@@ -17,7 +18,7 @@ public sealed class NameFilter : IFilterHandler
var filterOption = new FilterOption
{
FieldName = "name",
FieldName = NameSortIndexer.FieldName,
Value = string.Empty
};

View File

@@ -0,0 +1,27 @@
using Umbraco.Cms.Api.Delivery.Indexing.Sorts;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DeliveryApi;
namespace Umbraco.Cms.Api.Delivery.Querying.Sorts;
public sealed class CreateDateSort : ISortHandler
{
private const string SortOptionSpecifier = "createDate:";
/// <inheritdoc />
public bool CanHandle(string query)
=> query.StartsWith(SortOptionSpecifier, StringComparison.OrdinalIgnoreCase);
/// <inheritdoc/>
public SortOption BuildSortOption(string sort)
{
var sortDirection = sort.Substring(SortOptionSpecifier.Length);
return new SortOption
{
FieldName = CreateDateSortIndexer.FieldName,
Direction = sortDirection.StartsWith("asc") ? Direction.Ascending : Direction.Descending,
FieldType = FieldType.Date
};
}
}

View File

@@ -0,0 +1,27 @@
using Umbraco.Cms.Api.Delivery.Indexing.Sorts;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DeliveryApi;
namespace Umbraco.Cms.Api.Delivery.Querying.Sorts;
public sealed class UpdateDateSort : ISortHandler
{
private const string SortOptionSpecifier = "updateDate:";
/// <inheritdoc />
public bool CanHandle(string query)
=> query.StartsWith(SortOptionSpecifier, StringComparison.OrdinalIgnoreCase);
/// <inheritdoc/>
public SortOption BuildSortOption(string sort)
{
var sortDirection = sort.Substring(SortOptionSpecifier.Length);
return new SortOption
{
FieldName = UpdateDateSortIndexer.FieldName,
Direction = sortDirection.StartsWith("asc") ? Direction.Ascending : Direction.Descending,
FieldType = FieldType.Date
};
}
}