Skip to content

Commit 8443e1c

Browse files
authored
Implement an AddSort to IFindRequest (#250)
* Implement an `AddSort` to IFindRequest * add extension helper for sort * clean up
1 parent 4dea81d commit 8443e1c

6 files changed

Lines changed: 101 additions & 47 deletions

File tree

.vscode/settings.json

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,55 @@
22
"dotnet-test-explorer.enableTelemetry": false,
33
"dotnet-test-explorer.testProjectPath": "**/*.Tests.csproj",
44
"cSpell.words": [
5-
"cref",
6-
"dbnames",
7-
"fmdata",
8-
"FMID",
9-
"FMPXMLLAYOUT",
10-
"fmresultset",
11-
"Fuzzerd",
12-
"fuzzzerd",
13-
"Giverny",
14-
"Myget",
15-
"newed",
16-
"Nuget",
17-
"preq",
18-
"psort",
19-
"recid",
20-
"reserialize",
21-
"stringly",
22-
"Szalay",
23-
"unencrypted",
24-
"Xalan",
25-
"Xerces",
26-
"Xunit"
5+
"cref",
6+
"dbnames",
7+
"fmdata",
8+
"FMID",
9+
"FMPXMLLAYOUT",
10+
"fmresultset",
11+
"Fuzzerd",
12+
"fuzzzerd",
13+
"Giverny",
14+
"Myget",
15+
"newed",
16+
"Newtonsoft",
17+
"Nuget",
18+
"preq",
19+
"prerequest",
20+
"psort",
21+
"recid",
22+
"reserialize",
23+
"stringly",
24+
"Szalay",
25+
"typeparam",
26+
"unencrypted",
27+
"Xalan",
28+
"Xerces",
29+
"Xunit"
2730
],
2831
"git.enableCommitSigning": true,
2932
"omnisharp.useModernNet": true,
3033
"omnisharp.enableEditorConfigSupport": true,
3134
"omnisharp.enableRoslynAnalyzers": true,
3235
"workbench.colorCustomizations": {
33-
"activityBar.activeBackground": "#fbed80",
34-
"activityBar.activeBorder": "#06b9a5",
35-
"activityBar.background": "#fbed80",
36-
"activityBar.foreground": "#15202b",
37-
"activityBar.inactiveForeground": "#15202b99",
38-
"activityBarBadge.background": "#06b9a5",
39-
"activityBarBadge.foreground": "#15202b",
40-
"sash.hoverBorder": "#fbed80",
41-
"statusBar.background": "#f9e64f",
42-
"statusBar.foreground": "#15202b",
43-
"statusBarItem.hoverBackground": "#f7df1e",
44-
"statusBarItem.remoteBackground": "#f9e64f",
45-
"statusBarItem.remoteForeground": "#15202b",
46-
"titleBar.activeBackground": "#f9e64f",
47-
"titleBar.activeForeground": "#15202b",
48-
"titleBar.inactiveBackground": "#f9e64f99",
49-
"titleBar.inactiveForeground": "#15202b99"
36+
"activityBar.activeBackground": "#fbed80",
37+
"activityBar.activeBorder": "#06b9a5",
38+
"activityBar.background": "#fbed80",
39+
"activityBar.foreground": "#15202b",
40+
"activityBar.inactiveForeground": "#15202b99",
41+
"activityBarBadge.background": "#06b9a5",
42+
"activityBarBadge.foreground": "#15202b",
43+
"sash.hoverBorder": "#fbed80",
44+
"statusBar.background": "#f9e64f",
45+
"statusBar.foreground": "#15202b",
46+
"statusBarItem.hoverBackground": "#f7df1e",
47+
"statusBarItem.remoteBackground": "#f9e64f",
48+
"statusBarItem.remoteForeground": "#15202b",
49+
"titleBar.activeBackground": "#f9e64f",
50+
"titleBar.activeForeground": "#15202b",
51+
"titleBar.inactiveBackground": "#f9e64f99",
52+
"titleBar.inactiveForeground": "#15202b99",
53+
"commandCenter.border": "#15202b99"
5054
},
5155
"peacock.color": "#f9e64f"
5256
}

src/FMData.Rest/Requests/FindRequest.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public partial class FindRequest<TRequestType> : RequestBase, IFindRequest<TRequ
3232
/// The sort fields and directions for this request.
3333
/// </summary>
3434
[JsonProperty("sort")]
35-
public IEnumerable<ISort> Sort { get; set; }
35+
public ICollection<ISort> Sort { get; set; }
3636

3737
/// <summary>
3838
/// Determines if container data attributes are processed and loaded.
@@ -69,5 +69,19 @@ public override string SerializeRequest() => JsonConvert.SerializeObject(this,
6969
/// <param name="query">The object to add to the query.</param>
7070
/// <param name="omit">Flag indicating if this instance represents a find or an omit.</param>
7171
public void AddQuery(TRequestType query, bool omit = false) => _query.Add(new RequestQueryInstance<TRequestType>(query, omit));
72+
73+
/// <summary>
74+
/// Adds a sort field with a direction to the sort collection.
75+
/// </summary>
76+
/// <param name="fieldName">The field to sort by.</param>
77+
/// <param name="sortDirection">The direction to sort.</param>
78+
public void AddSort(string fieldName, string sortDirection)
79+
{
80+
if (Sort == null)
81+
{
82+
Sort = new List<ISort>();
83+
}
84+
Sort.Add(new Sort { SortOrder = sortDirection, FieldName = fieldName });
85+
}
7286
}
7387
}

src/FMData.Xml/Requests/FindRequest.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ public class FindRequest<T> : RequestBase, IFindRequest<T>
2020
/// Offset amount (skip)
2121
/// </summary>
2222
public int Offset { get; set; }
23+
2324
/// <summary>
2425
/// Limit amount (take)
2526
/// </summary>
2627
public int Limit { get; set; }
28+
2729
/// <summary>
2830
/// Sort options for the results.
2931
/// </summary>
30-
public IEnumerable<ISort> Sort { get; set; }
32+
public ICollection<ISort> Sort { get; set; }
3133

3234
/// <summary>
3335
/// Determines if container data attributes are processed and loaded.
@@ -52,5 +54,15 @@ public override string SerializeRequest()
5254
/// <param name="query">The object to add to the query.</param>
5355
/// <param name="omit">Flag indicating if this instance represents a find or an omit.</param>
5456
public void AddQuery(T query, bool omit = false) => _query.Add(new RequestQueryInstance<T>(query, omit));
57+
58+
/// <summary>
59+
/// Adds a sort field with a direction to the sort collection.
60+
/// </summary>
61+
/// <param name="fieldName">The field to sort by.</param>
62+
/// <param name="sortDirection">The direction to sort.</param>
63+
public void AddSort(string fieldName, string sortDirection)
64+
{
65+
throw new NotImplementedException();
66+
}
5567
}
5668
}

src/FMData/Requests/FindRequestExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ public static IFindRequest<T> AddCriteria<T>(this IFindRequest<T> request, T cri
1919
return request;
2020
}
2121

22+
/// <summary>
23+
/// Adds a sort field and direction to the find request.
24+
/// </summary>
25+
/// <typeparam name="T">The type used for the find request/response.</typeparam>
26+
/// <param name="request">The request. This is the 'this' parameter.</param>
27+
/// <param name="field">The field to sort on.</param>
28+
/// <param name="direction">The direction to sort.</param>
29+
/// <returns>The request instanced that was implicitly passed in which is useful for method chaining.</returns>
30+
public static IFindRequest<T> AddSortFieldDirection<T>(this IFindRequest<T> request, string field, string direction)
31+
{
32+
request.AddSort(field, direction);
33+
return request;
34+
}
35+
2236
/// <summary>
2337
/// Specify the limit of records to be returned.
2438
/// </summary>
@@ -134,4 +148,4 @@ public static IFindRequest<T> SetScript<T>(
134148
return request;
135149
}
136150
}
137-
}
151+
}

src/FMData/Requests/IFindRequest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface IFindRequest<TRequestType> : IFileMakerRequest
2525
/// <summary>
2626
/// The sort options for this request.
2727
/// </summary>
28-
IEnumerable<ISort> Sort { get; set; }
28+
ICollection<ISort> Sort { get; set; }
2929

3030
/// <summary>
3131
/// Determines if container data attributes are processed and loaded.
@@ -36,5 +36,12 @@ public interface IFindRequest<TRequestType> : IFileMakerRequest
3636
/// Add query data to the find request.
3737
/// </summary>
3838
void AddQuery(TRequestType query, bool omit = false);
39+
40+
/// <summary>
41+
/// Adds a sort field with a direction to the sort collection.
42+
/// </summary>
43+
/// <param name="fieldName">The field to sort by.</param>
44+
/// <param name="sortDirection">The direction to sort.</param>
45+
void AddSort(string fieldName, string sortDirection);
3946
}
40-
}
47+
}

tests/FMData.Rest.Tests/GeneralTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ public void FindRequest_Numbers_ShouldNotSerialize_ToNumbers()
7575
public void Nullable_Model_Property_Should_Be_EmptyString_When_Serialized_With_NullValueHandling_Include()
7676
{
7777
//arrange
78-
var r = new EditRequest<User>() { Data = new User { Id = 94, ForeignKeyId = null } };
79-
r.IncludeDefaultValuesInSerializedOutput = true;
80-
r.IncludeNullValuesInSerializedOutput = true;
78+
var r = new EditRequest<User>
79+
{
80+
Data = new User { Id = 94, ForeignKeyId = null },
81+
IncludeDefaultValuesInSerializedOutput = true,
82+
IncludeNullValuesInSerializedOutput = true
83+
};
8184

8285
// act
8386
var json = r.SerializeRequest();

0 commit comments

Comments
 (0)