forked from Ziad17/meilisearch-querying
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDependencyInjection.cs
82 lines (67 loc) · 3.6 KB
/
DependencyInjection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using FluentSearchEngine.Attributes;
using FluentSearchEngine.Extensions;
using FluentSearchEngine.Services.Implementations;
using System.Reflection;
using System.Text.Json.Serialization;
using Humanizer;
using Meilisearch;
using Microsoft.Extensions.DependencyInjection;
using FluentSearchEngine.Configurations;
using FluentSearchEngine.Services.Abstractions;
using FluentSearchEngine.Model;
namespace FluentSearchEngine
{
//TODO:: enable options based pattern to configure collective index
//TODO:: enable pagination max hit modification
public static class DependencyInjection
{
public static void ResolveFluentFiltersFromAssembly(this MeilisearchClient client, Assembly[] assemblies, SearchServiceOptions options = null)
{
if (options != null)
client.CreateIndexAsync(options.CollectiveHubName).Wait();
var types = assemblies.SelectMany(s => s.GetTypes())
.Where(x => x.IsClass && !x.IsAbstract && x.BaseType is { IsGenericType: true } &&
(x.BaseType.GetGenericTypeDefinition() == typeof(SearchModel<>) || x.BaseType.GetGenericTypeDefinition() == typeof(GeoSearchModel<>)));
EnrichAttributes(types, client, options);
}
public static void ResolveFluentFiltersFromAssembly(this MeilisearchClient client, SearchServiceOptions options = null)
{
var types = Assembly.GetCallingAssembly().GetTypes()
.Where(x => x.IsClass && !x.IsAbstract && x.BaseType is { IsGenericType: true } &&
(x.BaseType.GetGenericTypeDefinition() == typeof(SearchModel<>) || x.BaseType.GetGenericTypeDefinition() == typeof(GeoSearchModel<>)));
EnrichAttributes(types, client, options);
}
public static void AddGenericSearchService(this IServiceCollection services, MeilisearchClient client)
{
services.AddSingleton(client);
services.AddScoped(typeof(ISearchService<,>), typeof(SearchService<,>));
}
private static void EnrichAttributes(IEnumerable<Type> types, MeilisearchClient client, SearchServiceOptions options = null)
{
foreach (var type in types)
{
var sortableProperties = type.GetProperties()
.Where(x => x.IsDefined(typeof(Sortable), true))
.Select(x => x.IsDefined(typeof(JsonPropertyNameAttribute), true) ? x.GetCustomAttribute<JsonPropertyNameAttribute>()!.Name : x.Name.FirstCharToLowerCase()).ToList();
var filterableProperties = type.GetProperties()
.Where(x => x.IsDefined(typeof(SearchFilter), true))
.Select(x => x.IsDefined(typeof(JsonPropertyNameAttribute), true) ? x.GetCustomAttribute<JsonPropertyNameAttribute>()!.Name : x.Name.FirstCharToLowerCase()).ToList();
var indexName = type.Name.Pluralize();
client.CreateIndexAsync(indexName).Wait();
var index = client.Index(indexName);
if (options != null)
{
var paginationSettings = new Pagination()
{
MaxTotalHits = options.MaxTotalHits
};
index.UpdatePaginationAsync(paginationSettings).Wait();
}
if (sortableProperties.Any())
index.UpdateSortableAttributesAsync(sortableProperties).Wait();
if (filterableProperties.Any())
index.UpdateFilterableAttributesAsync(filterableProperties).Wait();
}
}
}
}