-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationRecordBuilder.cs
52 lines (46 loc) · 1.63 KB
/
LocationRecordBuilder.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
using DemoUmbracoAlgoliaIntegration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Integrations.Search.Algolia.Builders;
using Umbraco.Cms.Integrations.Search.Algolia.Models;
using Umbraco.Cms.Web.Common.PublishedModels;
namespace DemoUmbracoAlgoliaIntegration.Builders;
public class LocationRecordBuilder : IRecordBuilder<Location>
{
public Record GetRecord(IContent content, Record record)
{
return new GeoRecord(record)
{
GeoLocation = GetCoordinates(content)
};
}
private GeoLocation? GetCoordinates(IContent content)
{
var propertyValue = content.Properties["coordinates"]?.Values.FirstOrDefault()?.PublishedValue?.ToString();
if (!string.IsNullOrEmpty(propertyValue))
{
var coordinates = propertyValue.Split(',', StringSplitOptions.RemoveEmptyEntries);
if (coordinates.Length == 2)
{
decimal? latitude = null;
decimal? longitude = null;
if (decimal.TryParse(coordinates[0], out var parsedLatitude))
{
latitude = parsedLatitude;
}
if (decimal.TryParse(coordinates[1], out var parsedLongitude))
{
longitude = parsedLongitude;
}
if (latitude.HasValue && longitude.HasValue)
{
return new GeoLocation
{
Lat = latitude.Value,
Lng = longitude.Value
};
}
}
}
return null;
}
}