Skip to content

Add Negative filter #287

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 4 commits into from
May 28, 2018
Merged
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
4 changes: 4 additions & 0 deletions src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ private static Expression GetFilterExpressionLambda(Expression left, Expression
case FilterOperations.like:
body = Expression.Call(left, "Contains", null, right);
break;
// {model.Id != 1}
case FilterOperations.ne:
body = Expression.NotEqual(left, right);
break;
default:
throw new JsonApiException(500, $"Unknown filter operation {operation}");
}
Expand Down
5 changes: 3 additions & 2 deletions src/JsonApiDotNetCore/Internal/Query/FilterOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum FilterOperations
gt = 2,
le = 3,
ge = 4,
like = 5
like = 5,
ne = 6
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,31 @@ public async Task Cannot_Filter_If_Explicitly_Forbidden()
// assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}

[Fact]
public async Task Can_Filter_On_Not_Equal_Values()
{
// arrange
var context = _fixture.GetService<AppDbContext>();
var todoItem = _todoItemFaker.Generate();
context.TodoItems.Add(todoItem);
await context.SaveChangesAsync();

var totalCount = context.TodoItems.Count();
var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?page[size]={totalCount}&filter[ordinal]=ne:{todoItem.Ordinal}";
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await _fixture.Client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
var deserializedTodoItems = _fixture
.GetService<IJsonApiDeSerializer>()
.DeserializeList<TodoItem>(body);

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.False(deserializedTodoItems.Any(i => i.Ordinal == todoItem.Ordinal));
}
}
}