Skip to content

Commit cba5c4f

Browse files
committed
Ability to filter GetAllForProperty()
1 parent 3b55184 commit cba5c4f

File tree

10 files changed

+139
-17
lines changed

10 files changed

+139
-17
lines changed

Build/Griddly.Core.nuspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@
1919
<dependency id="Dapper" version="2.0.90" />
2020
<dependency id="EPPlus" version="4.5.3" />
2121
<dependency id="Newtonsoft.Json" version="11.0.2" />
22+
<dependency id="System.Linq.Dynamic.Core" version="1.3.8" />
2223
</group>
2324
<group targetFramework="netcoreapp3.1">
2425
<dependency id="CsvHelper" version="27.1.1" />
2526
<dependency id="Dapper" version="2.0.90" />
2627
<dependency id="EPPlus" version="4.5.3" />
2728
<dependency id="Newtonsoft.Json" version="11.0.2" />
29+
<dependency id="System.Linq.Dynamic.Core" version="1.3.8" />
2830
</group>
2931
<group targetFramework="net5.0">
3032
<dependency id="CsvHelper" version="27.1.1" />
3133
<dependency id="Dapper" version="2.0.90" />
3234
<dependency id="EPPlus" version="4.5.3" />
3335
<dependency id="Newtonsoft.Json" version="11.0.2" />
36+
<dependency id="System.Linq.Dynamic.Core" version="1.3.8" />
3437
</group>
3538
</dependencies>
3639
</metadata>

Griddly.Mvc/Griddly.Mvc.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
<ItemGroup Condition="$(TargetFramework.StartsWith('netcoreapp'))">
1919
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />
2020
<PackageReference Include="PluralizeService.Core" Version="1.0.0.5" />
21-
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.8" />
2221
</ItemGroup>
2322

2423
<ItemGroup>
25-
<PackageReference Include="CsvHelper" Version="27.1.1" />
26-
<PackageReference Include="Dapper" Version="2.0.90" />
27-
<PackageReference Include="EPPlus" Version="4.5.3" />
28-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
24+
<PackageReference Include="CsvHelper" Version="27.1.1" />
25+
<PackageReference Include="Dapper" Version="2.0.90" />
26+
<PackageReference Include="EPPlus" Version="4.5.3" />
27+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
28+
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.8" />
2929
</ItemGroup>
3030

3131
<ItemGroup>

Griddly.Mvc/GriddlyResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static SortField[] GetSortFields(NameValueCollection items)
4343

4444
public abstract long GetCount();
4545

46-
public abstract IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields);
46+
public abstract IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields, P[] restriction = null);
4747
}
4848

4949
public abstract class GriddlyResult<T> : GriddlyResult
@@ -259,7 +259,7 @@ public override async Task ExecuteResultAsync(ActionContext context)
259259

260260
public abstract void PopulateSummaryValues(GriddlySettings<T> settings);
261261

262-
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields)
262+
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields, P[] restriction = null)
263263
{
264264
throw new NotImplementedException();
265265
}

Griddly.Mvc/Results/DapperResult.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,31 @@ public override void PopulateSummaryValues(GriddlySettings<T> settings)
8484
}
8585
}
8686

87-
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields)
87+
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields, P[] restriction = null)
8888
{
8989
if (propertyName.Contains("."))
9090
throw new ArgumentException($"Property name may not contain a period. \"{propertyName}\"", "propertyName");
9191

92-
string sql = $"SELECT {propertyName} as _val FROM ({_sql}) [_proj] {(_fixedSort ? "" : $"ORDER BY {BuildSortClause(sortFields) ?? "CURRENT_TIMESTAMP"}")}";
92+
string sql = @$"SELECT {propertyName} as _val
93+
FROM ({_sql}) [_proj]
94+
{(restriction != null ? $"WHERE {propertyName} in @GetAllForProperty_Ids" : null)}
95+
{(_fixedSort ? "" : $"ORDER BY {BuildSortClause(sortFields) ?? "CURRENT_TIMESTAMP"}")}";
9396

9497
try
9598
{
9699
IDbConnection cn = _getConnection();
97100
IDbTransaction tx = _getTransaction != null ? _getTransaction() : null;
98101

99-
return cn.Query<P>(sql, _param, tx, commandTimeout: _commandTimeout);
102+
object parameters = _param;
103+
104+
if (restriction != null)
105+
{
106+
var parameters2 = new DynamicParameters(parameters);
107+
parameters2.Add("GetAllForProperty_Ids", restriction);
108+
parameters = parameters2;
109+
}
110+
111+
return cn.Query<P>(sql, parameters, tx, commandTimeout: _commandTimeout);
100112
}
101113
catch (Exception ex)
102114
{

Griddly.Mvc/Results/MapQueryableResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public override void PopulateSummaryValues(GriddlySettings<TOut> settings)
4040
_result.PopulateSummaryValue(c);
4141
}
4242

43-
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields)
43+
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields, P[] restriction = null)
4444
{
45-
return _result.GetAllForProperty<P>(propertyName, sortFields);
45+
return _result.GetAllForProperty<P>(propertyName, sortFields, restriction);
4646
}
4747

4848
public override long GetCount()

Griddly.Mvc/Results/QueryableResult.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,19 @@ internal void PopulateSummaryValue(GriddlyColumn c)
127127
}
128128
}
129129

130-
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields)
130+
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields, P[] restriction = null)
131131
{
132-
return ApplySortFields(GetQuery(), sortFields, _finalSortField)
133-
.Select<P>(propertyName, null);
132+
var query = GetQuery();
133+
134+
if (restriction != null)
135+
{
136+
query = System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where(query, $"@0.Contains({propertyName})", restriction);
137+
// query = query.Where($"@0.Contains({propertyName})", restriction);
138+
}
139+
140+
query = ApplySortFields(query, sortFields, _finalSortField);
141+
142+
return query.Select<P>(propertyName, null);
134143
}
135144

136145
public override long GetCount()

Griddly.Tests/Griddly.Tests.csproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp31;net472</TargetFrameworks>
5+
<LangVersion>latest</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
12+
<PackageReference Include="MSTest" Version="3.6.1" />
13+
<PackageReference Include="SQLite" Version="3.13.0" />
14+
<PackageReference Include="System.Data.SQLite" Version="1.0.119" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\Griddly.Mvc\Griddly.Mvc.csproj" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Griddly.Mvc.Results;
2+
using System.Data.SQLite;
3+
using Dapper;
4+
5+
namespace Griddly.Tests
6+
{
7+
[TestClass]
8+
public sealed class GriddlyResultTests
9+
{
10+
private SQLiteConnection CreateTestDatabase(string databaseName)
11+
{
12+
SQLiteConnection.CreateFile($"{databaseName}.sqlite");
13+
var conn = new SQLiteConnection($"Data Source={databaseName}.sqlite;Version=3;");
14+
conn.Open();
15+
16+
conn.Execute("CREATE TABLE TestTable (Id bigint, Name varchar(50))");
17+
conn.Execute("INSERT INTO TestTable (Id, Name) values (1, 'Test1')");
18+
conn.Execute("INSERT INTO TestTable (Id, Name) values (2, 'Test2')");
19+
conn.Execute("INSERT INTO TestTable (Id, Name) values (3, 'Test3')");
20+
21+
return conn;
22+
}
23+
24+
[TestMethod]
25+
public void DapperResult_GetAllForProperty_Filters()
26+
{
27+
using (var conn = CreateTestDatabase("DapperResult_GetAllForProperty_Filters"))
28+
{
29+
//Arrange
30+
var dapperResult = new DapperSql2012Result<object>(() => conn, "SELECT Id, Name from TestTable", null, null);
31+
32+
//Act
33+
var results = dapperResult.GetAllForProperty<long>("Id", null, new long[] { 1, 3 }).ToList();
34+
35+
//Assert
36+
Assert.AreEqual(2, results.Count);
37+
Assert.IsTrue(results.Contains(1));
38+
Assert.IsTrue(results.Contains(3));
39+
}
40+
}
41+
42+
43+
[TestMethod]
44+
public void QueryableResult_GetAllForProperty_Filters()
45+
{
46+
//Arrange
47+
var data = new Model[] { new Model { Id = 1, Name = "Test1" }, new Model { Id = 2, Name = "Test2" }, new Model { Id = 3, Name = "Test3" } };
48+
var query = data.AsQueryable();
49+
var queryableResult = new QueryableResult<object>(query, null);
50+
51+
//Act
52+
var results = queryableResult.GetAllForProperty<long>("Id", null, new long[] { 1, 3 }).ToList();
53+
54+
//Assert
55+
Assert.AreEqual(2, results.Count);
56+
Assert.IsTrue(results.Contains(1));
57+
Assert.IsTrue(results.Contains(3));
58+
}
59+
60+
private class Model
61+
{
62+
public long Id { get; set; }
63+
public string Name { get; set; }
64+
}
65+
}
66+
}

Griddly.Tests/MSTestSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]

Griddly.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30002.166
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Griddly", "Griddly\Griddly.csproj", "{EF4F0CB9-56A7-47D6-89C5-561F2DADB54F}"
77
EndProject
@@ -32,6 +32,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Griddly.NetCore", "Griddly.
3232
EndProject
3333
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Griddly.NetCore.Razor", "Griddly.NetCore.Razor\Griddly.NetCore.Razor.csproj", "{6D58C544-6AB0-4318-9563-092859F4959A}"
3434
EndProject
35+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Griddly.Tests", "Griddly.Tests\Griddly.Tests.csproj", "{9A3733DF-F83B-4DFD-970B-12C3B548C90E}"
36+
EndProject
3537
Global
3638
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3739
Debug|Any CPU = Debug|Any CPU
@@ -54,6 +56,10 @@ Global
5456
{6D58C544-6AB0-4318-9563-092859F4959A}.Debug|Any CPU.Build.0 = Debug|Any CPU
5557
{6D58C544-6AB0-4318-9563-092859F4959A}.Release|Any CPU.ActiveCfg = Release|Any CPU
5658
{6D58C544-6AB0-4318-9563-092859F4959A}.Release|Any CPU.Build.0 = Release|Any CPU
59+
{9A3733DF-F83B-4DFD-970B-12C3B548C90E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
60+
{9A3733DF-F83B-4DFD-970B-12C3B548C90E}.Debug|Any CPU.Build.0 = Debug|Any CPU
61+
{9A3733DF-F83B-4DFD-970B-12C3B548C90E}.Release|Any CPU.ActiveCfg = Release|Any CPU
62+
{9A3733DF-F83B-4DFD-970B-12C3B548C90E}.Release|Any CPU.Build.0 = Release|Any CPU
5763
EndGlobalSection
5864
GlobalSection(SolutionProperties) = preSolution
5965
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)