-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortingService.cs
85 lines (79 loc) · 5.34 KB
/
SortingService.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
83
84
85
using Microsoft.Extensions.DependencyInjection;
using Sitecore.ContentSearch.Data;
using Sitecore.ContentSearch.Linq;
using Sitecore.DependencyInjection;
using Sitecore.XA.Foundation.Search.Models;
using Sitecore.XA.Foundation.SitecoreExtensions.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Sitecore.XA.Foundation.Search.Services;
namespace CustomSXA.Foundation.Search.Services
{
public class SortingService : ISortingService
{
protected IFacetService FacetService { get; set; }
protected IOrderingParametersParserService SearchParametersParserService { get; set; }
public SortingService()
{
this.FacetService = ServiceLocator.ServiceProvider.GetService<IFacetService>();
this.SearchParametersParserService = ServiceLocator.ServiceProvider.GetService<IOrderingParametersParserService>();
}
public SortingDirection GetSortingDirection(IEnumerable<string> sortings) => this.SearchParametersParserService.GetDirection(sortings);
public IQueryable<ContentPage> Order(
IQueryable<ContentPage> query,
IEnumerable<string> sortings,
Coordinate center,
string siteName)
{
foreach (OrderingFacet orderingFacet in this.SearchParametersParserService.GetOrderingFacets(sortings, siteName).Reverse<OrderingFacet>())
{
OrderingFacet sortingFacet = orderingFacet;
if (sortingFacet.Direction == SortingDirection.Ascending)
{
if (sortingFacet.Facet.DoesItemInheritFrom(Sitecore.XA.Foundation.Search.Templates.DistanceFacet.ID))
{
if (center != null)
query = (IQueryable<ContentPage>)query.OrderByDistance<ContentPage, Coordinate>((Expression<Func<ContentPage, Coordinate>>)(i => i.Location), center);
else //This else part added to enhance the Sorting service with sort by distance for multiple coordinates i.e. POI items mapped for a non POI item.
{
center = Helper.GeoLocationHelper.GetLocationCentre();
if (center != null)
query = (IQueryable<ContentPage>)query.OrderByDistance<ContentPage, string>((Expression<Func<ContentPage, string>>)(i => i["multiplecoordinates"]), center);
return query;
}
}
else if (sortingFacet.Facet.DoesItemInheritFrom(Sitecore.XA.Foundation.Search.Templates.FloatFacet.ID))
query = (IQueryable<ContentPage>)query.OrderBy<ContentPage, double>((Expression<Func<ContentPage, double>>)(i => i.get_Item<double>((string)sortingFacet.Key)));
else if (sortingFacet.Facet.DoesItemInheritFrom(Sitecore.XA.Foundation.Search.Templates.IntegerFacet.ID))
query = (IQueryable<ContentPage>)query.OrderBy<ContentPage, long>((Expression<Func<ContentPage, long>>)(i => i.get_Item<long>((string)sortingFacet.Key)));
else
query = (IQueryable<ContentPage>)query.OrderBy<ContentPage, string>((Expression<Func<ContentPage, string>>)(i => i.get_Item<string>((string)sortingFacet.Key)));
}
else if (sortingFacet.Direction == SortingDirection.Descending)
{
if (sortingFacet.Facet.DoesItemInheritFrom(Sitecore.XA.Foundation.Search.Templates.DistanceFacet.ID))
{
if (center != null)
query = (IQueryable<ContentPage>)query.OrderByDistanceDescending<ContentPage, Coordinate>((Expression<Func<ContentPage, Coordinate>>)(i => i.Location), center);
else //This else part added to enhance the Sorting service with sort by distance for multiple coordinates i.e. POI items mapped for a non POI item. This is for descending case.
{
center = Helper.GeoLocationHelper.GetLocationCentre();
if (center != null)
query = (IQueryable<ContentPage>)query.OrderByDistanceDescending<ContentPage, string>((Expression<Func<ContentPage, string>>)(i => i["multiplecoordinates"]), center);
return query;
}
}
else if (sortingFacet.Facet.DoesItemInheritFrom(Sitecore.XA.Foundation.Search.Templates.FloatFacet.ID))
query = (IQueryable<ContentPage>)query.OrderByDescending<ContentPage, double>((Expression<Func<ContentPage, double>>)(i => i.get_Item<double>((string)sortingFacet.Key)));
else if (sortingFacet.Facet.DoesItemInheritFrom(Sitecore.XA.Foundation.Search.Templates.IntegerFacet.ID))
query = (IQueryable<ContentPage>)query.OrderByDescending<ContentPage, long>((Expression<Func<ContentPage, long>>)(i => i.get_Item<long>((string)sortingFacet.Key)));
else
query = (IQueryable<ContentPage>)query.OrderByDescending<ContentPage, string>((Expression<Func<ContentPage, string>>)(i => i.get_Item<string>((string)sortingFacet.Key)));
}
}
return query;
}
}
}