-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathGeoQueryVisitor.cs
More file actions
55 lines (41 loc) · 2.04 KB
/
GeoQueryVisitor.cs
File metadata and controls
55 lines (41 loc) · 2.04 KB
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
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Entities.Contents.Text;
using Squidex.Infrastructure.Queries;
#pragma warning disable SA1313 // Parameter names should begin with lower-case letter
namespace Squidex.Domain.Apps.Entities.Contents.Queries;
internal sealed class GeoQueryVisitor : AsyncTransformVisitor<ClrValue, GeoQueryVisitor.Args>
{
public static readonly GeoQueryVisitor Instance = new GeoQueryVisitor();
public record struct Args(Context Context, Schema Schema, ITextIndex TextIndex, CancellationToken CancellationToken);
private GeoQueryVisitor()
{
}
public static async Task<FilterNode<ClrValue>?> VisitAsync(FilterNode<ClrValue> filter, Context context, Schema schema, ITextIndex textIndex,
CancellationToken ct)
{
var args = new Args(context, schema, textIndex, ct);
return await filter.Accept(Instance, args);
}
public override async ValueTask<FilterNode<ClrValue>?> Visit(CompareFilter<ClrValue> nodeIn, Args args)
{
if (nodeIn.Value.Value is FilterSphere sphere)
{
var field = string.Join('.', nodeIn.Path.Skip(1));
var searchQuery = new GeoQuery(args.Schema.Id, field, sphere.Latitude, sphere.Longitude, sphere.Radius, 1000);
var searchScope = args.Context.Scope();
var ids = await args.TextIndex.SearchAsync(args.Context.App, searchQuery, searchScope, args.CancellationToken);
if (ids is not { Count: > 0 })
{
return ClrFilter.Eq("id", "__notfound__");
}
return ClrFilter.In("id", ids.Select(x => x.ToString()).ToList());
}
return nodeIn;
}
}