Skip to content

[WIP] Feat: Queryable resolver refs #121 #124

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Id,Text,Created,PostId,AuthorId
"101","Comment 1","2015-01-31T14:30Z","201","403"
"102","efgComment","2015-01-31T14:35Z","201","402"
"103","Comment 3","2015-01-31T14:41Z","201","403"
"104","Comment 4","2015-02-05T09:08Z","202","403"
"105","Comment 5","2015-02-06T14:19Z","203","401"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Id,Title,Content,Created,AuthorId
"201","Post abc","Post def content","2015-01-31T14:00Z","401"
"202","Post efg","Post cde content","2015-02-05T08:10Z","401"
"203","Post hij","Post efg content","2015-02-07T11:11Z","401"
"204","Post klm","Post hij content","2015-02-08T06:59Z","402"
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests
{
[TestClass]
public class FetchingResourcesQueryResolverTests : AcceptanceTestsBase
{
[TestMethod]
[DeploymentItem(@"Data\CommentSearch.csv", @"Data")]
[DeploymentItem(@"Data\PostSearch.csv", @"Data")]
[DeploymentItem(@"Data\User.csv", @"Data")]
public async Task GetAll()
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitGet(effortConnection, "post-searchs");

await AssertResponseContent(response, @"Fixtures\FetchingResourcesQueryResolver\GetAllResponse.json", HttpStatusCode.OK);
}
}

[TestMethod]
[DeploymentItem(@"Data\CommentSearch.csv", @"Data")]
[DeploymentItem(@"Data\PostSearch.csv", @"Data")]
[DeploymentItem(@"Data\User.csv", @"Data")]
public async Task GetWithFilter()
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitGet(effortConnection, "post-searchs?filter[title]=Post efg");

await AssertResponseContent(response, @"Fixtures\FetchingResourcesQueryResolver\GetWithFilterResponse.json", HttpStatusCode.OK);
}
}


[TestMethod]
[DeploymentItem(@"Data\CommentSearch.csv", @"Data")]
[DeploymentItem(@"Data\PostSearch.csv", @"Data")]
[DeploymentItem(@"Data\User.csv", @"Data")]
public async Task GetWithSearchFilter() // this enables logic in Query resolver
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitGet(effortConnection, "post-searchs?searchterm=efg");

await AssertResponseContent(response, @"Fixtures\FetchingResourcesQueryResolver\GetWithSearchFilterResponse.json", HttpStatusCode.OK);
}
}


[TestMethod]
[DeploymentItem(@"Data\CommentSearch.csv", @"Data")]
[DeploymentItem(@"Data\PostSearch.csv", @"Data")]
[DeploymentItem(@"Data\User.csv", @"Data")]
public async Task GetWithSearchFilter_related_to_many() // this enables logic in Query resolver for related
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitGet(effortConnection, "post-searchs/201/comments?searchterm=efg");

await AssertResponseContent(response, @"Fixtures\FetchingResourcesQueryResolver\GetWithSearchFilter_related_to_many_Response.json", HttpStatusCode.OK);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"data": [
{
"type": "post-searchs",
"id": "201",
"attributes": {
"content": "Post def content",
"created": "2015-01-31T14:00:00.0000000+00:00",
"title": "Post abc"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/post-searchs/201/relationships/author",
"related": "https://www.example.com/post-searchs/201/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/post-searchs/201/relationships/comments",
"related": "https://www.example.com/post-searchs/201/comments"
}
}
}
},
{
"type": "post-searchs",
"id": "202",
"attributes": {
"content": "Post cde content",
"created": "2015-02-05T08:10:00.0000000+00:00",
"title": "Post efg"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/post-searchs/202/relationships/author",
"related": "https://www.example.com/post-searchs/202/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/post-searchs/202/relationships/comments",
"related": "https://www.example.com/post-searchs/202/comments"
}
}
}
},
{
"type": "post-searchs",
"id": "203",
"attributes": {
"content": "Post efg content",
"created": "2015-02-07T11:11:00.0000000+00:00",
"title": "Post hij"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/post-searchs/203/relationships/author",
"related": "https://www.example.com/post-searchs/203/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/post-searchs/203/relationships/comments",
"related": "https://www.example.com/post-searchs/203/comments"
}
}
}
},
{
"type": "post-searchs",
"id": "204",
"attributes": {
"content": "Post hij content",
"created": "2015-02-08T06:59:00.0000000+00:00",
"title": "Post klm"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/post-searchs/204/relationships/author",
"related": "https://www.example.com/post-searchs/204/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/post-searchs/204/relationships/comments",
"related": "https://www.example.com/post-searchs/204/comments"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"data": [
{
"type": "post-searchs",
"id": "202",
"attributes": {
"content": "Post cde content",
"created": "2015-02-05T08:10:00.0000000+00:00",
"title": "Post efg"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/post-searchs/202/relationships/author",
"related": "https://www.example.com/post-searchs/202/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/post-searchs/202/relationships/comments",
"related": "https://www.example.com/post-searchs/202/comments"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"data": [
{
"type": "post-searchs",
"id": "201",
"attributes": {
"content": "Post def content",
"created": "2015-01-31T14:00:00.0000000+00:00",
"title": "Post abc"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/post-searchs/201/relationships/author",
"related": "https://www.example.com/post-searchs/201/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/post-searchs/201/relationships/comments",
"related": "https://www.example.com/post-searchs/201/comments"
}
}
}
},
{
"type": "post-searchs",
"id": "202",
"attributes": {
"content": "Post cde content",
"created": "2015-02-05T08:10:00.0000000+00:00",
"title": "Post efg"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/post-searchs/202/relationships/author",
"related": "https://www.example.com/post-searchs/202/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/post-searchs/202/relationships/comments",
"related": "https://www.example.com/post-searchs/202/comments"
}
}
}
},
{
"type": "post-searchs",
"id": "203",
"attributes": {
"content": "Post efg content",
"created": "2015-02-07T11:11:00.0000000+00:00",
"title": "Post hij"
},
"relationships": {
"author": {
"links": {
"self": "https://www.example.com/post-searchs/203/relationships/author",
"related": "https://www.example.com/post-searchs/203/author"
}
},
"comments": {
"links": {
"self": "https://www.example.com/post-searchs/203/relationships/comments",
"related": "https://www.example.com/post-searchs/203/comments"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": [
{
"type": "comment-searchs",
"id": "102",
"attributes": {
"created": "2015-01-31T14:35:00.0000000+00:00",
"text": "efgComment"
},
"relationships": {
"post": {
"links": {
"self": "https://www.example.com/comment-searchs/102/relationships/post",
"related": "https://www.example.com/comment-searchs/102/post"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="CreatingResourcesTests.cs" />
<Compile Include="DeletingResourcesTests.cs" />
<Compile Include="ErrorsTests.cs" />
<Compile Include="FetchingResourcesQueryResolverTests.cs" />
<Compile Include="PaginationTests.cs" />
<Compile Include="FetchingResourcesTests.cs" />
<Compile Include="HeterogeneousTests.cs" />
Expand Down Expand Up @@ -144,12 +145,18 @@
<None Include="Data\Child.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\CommentSearch.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\Master.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\Officer.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\PostSearch.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\PostLongId.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -276,11 +283,15 @@
<EmbeddedResource Include="Fixtures\FetchingResources\Get_related_to_many_include_response.json" />
<EmbeddedResource Include="Fixtures\CreatingResources\Responses\Post_with_empty_id_and_include_author_Response.json" />
<EmbeddedResource Include="Fixtures\UpdatingResources\Responses\PatchWithAttributeUpdateWithIncludeResponse.json" />
<EmbeddedResource Include="Fixtures\FetchingResourcesQueryResolver\GetAllResponse.json" />
<EmbeddedResource Include="Fixtures\FetchingResourcesQueryResolver\GetWithFilterResponse.json" />
<EmbeddedResource Include="Fixtures\FetchingResourcesQueryResolver\GetWithSearchFilterResponse.json" />
<EmbeddedResource Include="Fixtures\Pagination\GetAllResponsePaged-2-2.json" />
<EmbeddedResource Include="Fixtures\Pagination\GetFilterPaged-2-1.json" />
<EmbeddedResource Include="Fixtures\Pagination\GetFilterPaged-1-2-sorted.json" />
<EmbeddedResource Include="Fixtures\Pagination\GetFilterPaged-1-2-sorted-desc.json" />
<EmbeddedResource Include="Fixtures\FetchingResources\Get_related_to_many_include_external_response.json" />
<EmbeddedResource Include="Fixtures\FetchingResourcesQueryResolver\GetWithSearchFilter_related_to_many_Response.json" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Helper;
using JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Models;
using JSONAPI.QueryableResolvers;

namespace JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp
{
public class CommentSearchResourceQueryResolver : IResourceCollectionResolver<CommentSearch>
{
public Task<IQueryable<CommentSearch>> GetQueryForResourceCollection(IQueryable<CommentSearch> queryable, HttpRequestMessage request, CancellationToken cancellationToken)
{
var queryPairs = request.GetQueryNameValuePairs();
foreach (var queryPair in queryPairs)
{
if (String.IsNullOrWhiteSpace(queryPair.Key))
continue;

if (!queryPair.Key.StartsWith("searchterm"))
continue;

var searchTerm = queryPair.Value;
var predicate = PredicateBuilder.False<CommentSearch>();

foreach (var str in Regex.Split(searchTerm, "\\s+"))
{
predicate = predicate.Or(y => y.Text.Contains(str));
}
queryable= queryable.Where(predicate);
}
return Task.FromResult(queryable);
}
}
}
Loading