-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: or-2038 add sorting on beheer zoeken
- Loading branch information
1 parent
4daee6d
commit 300d863
Showing
22 changed files
with
751 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...istry.Admin.Api/Verenigingen/Search/Exceptions/ZoekOpdrachtBevatOnbekendeSorteerVelden.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace AssociationRegistry.Admin.Api.Verenigingen.Search.Exceptions; | ||
|
||
using Be.Vlaanderen.Basisregisters.AggregateSource; | ||
using System; | ||
|
||
[Serializable] | ||
public class ZoekOpdrachtBevatOnbekendeSorteerVelden : DomainException | ||
{ | ||
public ZoekOpdrachtBevatOnbekendeSorteerVelden(string onbekendVeld) : | ||
base(string.Format(ExceptionMessages.ZoekOpdrachtBevatOnbekendeSorteerVelden, onbekendVeld)) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/AssociationRegistry.Admin.Api/Verenigingen/Search/Exceptions/ZoekOpdrachtWasIncorrect.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace AssociationRegistry.Admin.Api.Verenigingen.Search.Exceptions; | ||
|
||
using Be.Vlaanderen.Basisregisters.AggregateSource; | ||
using System; | ||
|
||
[Serializable] | ||
public class ZoekOpdrachtWasIncorrect : DomainException | ||
{ | ||
public ZoekOpdrachtWasIncorrect() : base(ExceptionMessages.ZoekOpdrachtWasIncorrect) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/AssociationRegistry.Admin.Api/Verenigingen/Search/SearchVerenigingenExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
namespace AssociationRegistry.Admin.Api.Verenigingen.Search; | ||
|
||
using Nest; | ||
using System; | ||
using System.Linq; | ||
|
||
public static class SearchVerenigingenExtensions | ||
{ | ||
public static SearchDescriptor<T> ParseSort<T>( | ||
this SearchDescriptor<T> source, | ||
string? sort, | ||
Func<SortDescriptor<T>, SortDescriptor<T>> defaultSort, | ||
TypeMapping mapping) where T : class | ||
{ | ||
if (string.IsNullOrWhiteSpace(sort)) | ||
return source.Sort(defaultSort); | ||
|
||
return source.Sort(_ => SortDescriptor<T>(sort, mapping).ThenBy(defaultSort)); | ||
} | ||
|
||
private static SortDescriptor<T> SortDescriptor<T>(string sort, TypeMapping mapping) where T : class | ||
{ | ||
var sortParts = sort.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()) | ||
.ToArray(); | ||
|
||
var sortDescriptor = new SortDescriptor<T>(); | ||
|
||
foreach (var sortPart in sortParts) | ||
{ | ||
var descending = sortPart.StartsWith("-"); | ||
var part = descending ? sortPart.Substring(1) : sortPart; | ||
var isKeyword = IsKeyword(mapping, part); | ||
sortDescriptor.Field($"{part}{(isKeyword ? "" : ".keyword")}", descending ? SortOrder.Descending : SortOrder.Ascending); | ||
} | ||
|
||
return sortDescriptor; | ||
} | ||
|
||
private static bool IsKeyword(ITypeMapping mapping, string field) | ||
=> InspectPropertyType(mapping.Properties, field.Split('.'), currentIndex: 0) == "keyword"; | ||
|
||
private static string InspectPropertyType(IProperties properties, string[] pathSegments, int currentIndex) | ||
{ | ||
if (currentIndex < pathSegments.Length && properties.ContainsKey(pathSegments[currentIndex])) | ||
{ | ||
var currentProperty = properties[pathSegments[currentIndex]]; | ||
|
||
if (currentIndex == pathSegments.Length - 1) | ||
// We've reached the desired property | ||
return currentProperty.Type; | ||
|
||
if (currentProperty is ObjectProperty objectProperty) | ||
// We need to delve deeper into the object properties | ||
return InspectPropertyType(objectProperty.Properties, pathSegments, currentIndex + 1); | ||
} | ||
|
||
// The desired property or path wasn't found | ||
return null; | ||
} | ||
|
||
private static SortDescriptor<T> ThenBy<T>(this SortDescriptor<T> first, Func<SortDescriptor<T>, SortDescriptor<T>> second) | ||
where T : class | ||
=> second(first); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/AssociationRegistry.Admin.Schema/Search/PropertyDescriptorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace AssociationRegistry.Public.Schema.Search; | ||
|
||
using Nest; | ||
|
||
public static class PropertyDescriptorExtensions | ||
{ | ||
public static TDescriptor WithKeyword<TDescriptor, TInterface, T>( | ||
this CorePropertyDescriptorBase<TDescriptor, TInterface, T> source, | ||
string? normalizer = null) | ||
where TDescriptor : CorePropertyDescriptorBase<TDescriptor, TInterface, T>, TInterface | ||
where TInterface : class, ICoreProperty | ||
where T : class | ||
{ | ||
return source.Fields(x => | ||
x.Keyword( | ||
y => | ||
normalizer is null | ||
? y.Name("keyword") | ||
: y.Name("keyword").Normalizer(normalizer))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.