Skip to content

Commit

Permalink
[HER-153] Fix fuzzy search (#83)
Browse files Browse the repository at this point in the history
* [HER-153] Fix fuzzy search in CameraFilter

* [HER-153] Fix fuzzy search in DetectionFilter

* [HER-153] Add package for predicate
  • Loading branch information
Urzica-Platon-Casian authored Aug 17, 2021
1 parent e21018a commit a6b9a2b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/Data/Camera/Filtering/CameraFilter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace Data.Camera.Filtering
{
using System.Collections.Generic;
using System.Linq;
using Business.Camera.Fetching.Models;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using MySql.EntityFrameworkCore.Extensions;

public class CameraFilter : ICameraFilter
{
Expand All @@ -25,9 +26,20 @@ public IQueryable<ECamera> Execute(IQueryable<ECamera> input)
}

private IQueryable<ECamera> MatchId(IQueryable<ECamera> input) => _parameter.Ids != null
? input.Where(entity => EF.Functions.Like(_parameter.Ids.Contains(entity.Id), $"%{_parameter.Ids}%"))
? MatchByIds(input, _parameter.Ids)
: input;

private static IQueryable<ECamera> MatchByIds(IQueryable<ECamera> input, List<string> ids)
{
var predicate = PredicateBuilder.New<ECamera>();
foreach (var id in ids)
{
predicate = predicate.Or(entity => EF.Functions.Like(entity.Id, $"%{id}%"));
}

return input.Where(predicate);
}

private IQueryable<ECamera> MatchLesserLatitude(IQueryable<ECamera> input) =>
_parameter.Latitude.LesserEqualThan != null
? input.Where(camera => camera.Latitude <= _parameter.Latitude.LesserEqualThan)
Expand Down
1 change: 1 addition & 0 deletions src/Data/Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LinqKit" Version="1.1.26" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
15 changes: 14 additions & 1 deletion src/Data/Detection/Filtering/DetectionFilter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace Data.Detection.Filtering
{
using System.Collections.Generic;
using System.Linq;
using Business.Detection.Fetching.Models;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using MySql.EntityFrameworkCore.Extensions;

Expand Down Expand Up @@ -56,6 +58,17 @@ private IQueryable<EDetection> MatchTimestampGreaterThan(IQueryable<EDetection>
: input;

private IQueryable<EDetection> MatchCameraIds(IQueryable<EDetection> input) =>
_parameter.CameraIds == null ? input : input.Where(detection => EF.Functions.Like(_parameter.CameraIds.Contains(detection.CameraId), $"%{_parameter.CameraIds}%"));
_parameter.CameraIds == null ? input : MatchByCameraIds(input, _parameter.CameraIds);

private static IQueryable<EDetection> MatchByCameraIds(IQueryable<EDetection> input, List<string> ids)
{
var predicate = PredicateBuilder.New<EDetection>();
foreach (var id in ids)
{
predicate = predicate.Or(detections => EF.Functions.Like(detections.CameraId, $"%{id}%"));
}

return input.Where(predicate);
}
}
}

0 comments on commit a6b9a2b

Please sign in to comment.