-
-
Notifications
You must be signed in to change notification settings - Fork 158
273 #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
273 #275
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace JsonApiDotNetCore.Internal.Query{ | ||
public static class QueryConstants { | ||
public const string FILTER = "filter"; | ||
public const string SORT = "sort"; | ||
public const string INCLUDE = "include"; | ||
public const string PAGE = "page"; | ||
public const string FIELDS = "fields"; | ||
public const char OPEN_BRACKET = '['; | ||
public const char CLOSE_BRACKET = ']'; | ||
public const char COMMA = ','; | ||
public const char COLON = ':'; | ||
public const string COLON_STR = ":"; | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using JsonApiDotNetCore.Internal.Query; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JsonApiDotNetCore.Services | ||
{ | ||
public interface IQueryComposer | ||
{ | ||
string Compose(IJsonApiContext jsonApiContext); | ||
} | ||
|
||
public class QueryComposer : IQueryComposer | ||
{ | ||
public string Compose(IJsonApiContext jsonApiContext) | ||
{ | ||
string result = ""; | ||
if(jsonApiContext != null && jsonApiContext.QuerySet != null) | ||
{ | ||
List<FilterQuery> filterQueries = jsonApiContext.QuerySet.Filters; | ||
if (filterQueries.Count > 0) | ||
{ | ||
foreach (FilterQuery filter in filterQueries) | ||
{ | ||
result += ComposeSingleFilter(filter); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private string ComposeSingleFilter(FilterQuery query) | ||
{ | ||
var result = "&filter"; | ||
result += QueryConstants.OPEN_BRACKET + query.Attribute + QueryConstants.CLOSE_BRACKET + query.Operation + query.Value; | ||
return result; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Collections.Generic; | ||
using JsonApiDotNetCore.Internal.Query; | ||
using JsonApiDotNetCore.Services; | ||
using Microsoft.AspNetCore.Http; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace UnitTests.Services | ||
{ | ||
public class QueryComposerTests | ||
{ | ||
private readonly Mock<IJsonApiContext> _jsonApiContext; | ||
|
||
public QueryComposerTests() | ||
{ | ||
_jsonApiContext = new Mock<IJsonApiContext>(); | ||
} | ||
|
||
[Fact] | ||
public void Can_Compose_FilterStringForUrl() | ||
{ | ||
// arrange | ||
var filter = new FilterQuery("attribute", "value", "="); | ||
var querySet = new QuerySet(); | ||
List<FilterQuery> filters = new List<FilterQuery>(); | ||
filters.Add(filter); | ||
querySet.Filters=filters; | ||
|
||
_jsonApiContext | ||
.Setup(m => m.QuerySet) | ||
.Returns(querySet); | ||
|
||
var queryComposer = new QueryComposer(); | ||
// act | ||
var filterString = queryComposer.Compose(_jsonApiContext.Object); | ||
// assert | ||
Assert.Equal("&filter[attribute]=value", filterString); | ||
} | ||
|
||
[Fact] | ||
public void NoFilter_Compose_EmptyStringReturned() | ||
{ | ||
// arrange | ||
var querySet = new QuerySet(); | ||
|
||
_jsonApiContext | ||
.Setup(m => m.QuerySet) | ||
.Returns(querySet); | ||
|
||
var queryComposer = new QueryComposer(); | ||
// act | ||
var filterString = queryComposer.Compose(_jsonApiContext.Object); | ||
// assert | ||
Assert.Equal("", filterString); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably also include sort parameters, right?