From fc3f342906ea69e09852a2cfeb195e6f6339e423 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Fri, 28 Apr 2023 11:14:50 +0200 Subject: [PATCH] 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 --- .../Indexing/Sorts/CreateDateSortIndexer.cs | 15 +++++++++++ .../Indexing/Sorts/UpdateDateSortIndexer.cs | 15 +++++++++++ .../Querying/Filters/NameFilter.cs | 3 ++- .../Querying/Sorts/CreateDateSort.cs | 27 +++++++++++++++++++ .../Querying/Sorts/UpdateDateSort.cs | 27 +++++++++++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/CreateDateSortIndexer.cs create mode 100644 src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/UpdateDateSortIndexer.cs create mode 100644 src/Umbraco.Cms.Api.Delivery/Querying/Sorts/CreateDateSort.cs create mode 100644 src/Umbraco.Cms.Api.Delivery/Querying/Sorts/UpdateDateSort.cs diff --git a/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/CreateDateSortIndexer.cs b/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/CreateDateSortIndexer.cs new file mode 100644 index 0000000000..2d16ccb66e --- /dev/null +++ b/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/CreateDateSortIndexer.cs @@ -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 GetFieldValues(IContent content) + => new[] { new IndexFieldValue { FieldName = FieldName, Value = content.CreateDate } }; + + public IEnumerable GetFields() + => new[] { new IndexField { FieldName = FieldName, FieldType = FieldType.Date } }; +} diff --git a/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/UpdateDateSortIndexer.cs b/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/UpdateDateSortIndexer.cs new file mode 100644 index 0000000000..073543cd5f --- /dev/null +++ b/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/UpdateDateSortIndexer.cs @@ -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 GetFieldValues(IContent content) + => new[] { new IndexFieldValue { FieldName = FieldName, Value = content.UpdateDate } }; + + public IEnumerable GetFields() + => new[] { new IndexField { FieldName = FieldName, FieldType = FieldType.Date } }; +} diff --git a/src/Umbraco.Cms.Api.Delivery/Querying/Filters/NameFilter.cs b/src/Umbraco.Cms.Api.Delivery/Querying/Filters/NameFilter.cs index fc9676d2b2..40733de7f0 100644 --- a/src/Umbraco.Cms.Api.Delivery/Querying/Filters/NameFilter.cs +++ b/src/Umbraco.Cms.Api.Delivery/Querying/Filters/NameFilter.cs @@ -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 }; diff --git a/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/CreateDateSort.cs b/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/CreateDateSort.cs new file mode 100644 index 0000000000..f04b060810 --- /dev/null +++ b/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/CreateDateSort.cs @@ -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:"; + + /// + public bool CanHandle(string query) + => query.StartsWith(SortOptionSpecifier, StringComparison.OrdinalIgnoreCase); + + /// + 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 + }; + } +} diff --git a/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/UpdateDateSort.cs b/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/UpdateDateSort.cs new file mode 100644 index 0000000000..dd5c8262fd --- /dev/null +++ b/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/UpdateDateSort.cs @@ -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:"; + + /// + public bool CanHandle(string query) + => query.StartsWith(SortOptionSpecifier, StringComparison.OrdinalIgnoreCase); + + /// + 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 + }; + } +}