Skip to content

Feat/#211 #220

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 5 commits into from
Jan 10, 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
64 changes: 29 additions & 35 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,12 @@ public virtual IQueryable<TEntity> Get()

public virtual IQueryable<TEntity> Filter(IQueryable<TEntity> entities, FilterQuery filterQuery)
{
if (filterQuery == null)
return entities;

if (filterQuery.IsAttributeOfRelationship)
return entities.Filter(new RelatedAttrFilterQuery(_jsonApiContext, filterQuery));

return entities.Filter(new AttrFilterQuery(_jsonApiContext, filterQuery));
return entities.Filter(_jsonApiContext, filterQuery);
}

public virtual IQueryable<TEntity> Sort(IQueryable<TEntity> entities, List<SortQuery> sortQueries)
{
if (sortQueries == null || sortQueries.Count == 0)
return entities;

var orderedEntities = entities.Sort(sortQueries[0]);

if (sortQueries.Count <= 1) return orderedEntities;

for (var i = 1; i < sortQueries.Count; i++)
orderedEntities = orderedEntities.Sort(sortQueries[i]);

return orderedEntities;
return entities.Sort(sortQueries);
}

public virtual async Task<TEntity> GetAsync(TId id)
Expand Down Expand Up @@ -156,26 +140,36 @@ public virtual IQueryable<TEntity> Include(IQueryable<TEntity> entities, string

public virtual async Task<IEnumerable<TEntity>> PageAsync(IQueryable<TEntity> entities, int pageSize, int pageNumber)
{
if (pageSize > 0)
if (pageNumber >= 0)
{
if (pageNumber == 0)
pageNumber = 1;

if (pageNumber > 0)
return await entities
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
else // page from the end of the set
return (await entities
.OrderByDescending(t => t.Id)
.Skip((Math.Abs(pageNumber) - 1) * pageSize)
.Take(pageSize)
.ToListAsync())
.OrderBy(t => t.Id)
.ToList();
return await entities.PageForward(pageSize, pageNumber).ToListAsync();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you calling the ToListAsync() extension method over your own this.ToListAsync(/*...*/) ?

Copy link
Author

@shuebner shuebner Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason. I thought, the extension methods are an implementation detail anyway. We can use the repo's own method if you like that better.

Copy link
Contributor

@jaredcnance jaredcnance Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, I suppose the query realization methods exposed here are really only required for the service layer's use. I'm fine with this.

}

// since EntityFramework does not support IQueryable.Reverse(), we need to know the number of queried entities
int numberOfEntities = await this.CountAsync(entities);

// may be negative
int virtualFirstIndex = numberOfEntities - pageSize * Math.Abs(pageNumber);
int numberOfElementsInPage = Math.Min(pageSize, virtualFirstIndex + pageSize);

return await entities
.Skip(virtualFirstIndex)
.Take(numberOfElementsInPage)
.ToListAsync();
}

public async Task<int> CountAsync(IQueryable<TEntity> entities)
{
return await entities.CountAsync();
}

public Task<TEntity> FirstOrDefaultAsync(IQueryable<TEntity> entities)
{
return entities.FirstOrDefaultAsync();
}

public async Task<IReadOnlyList<TEntity>> ToListAsync(IQueryable<TEntity> entities)
{
return await entities.ToListAsync();
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/JsonApiDotNetCore/Data/IEntityReadRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ public interface IEntityReadRepository<TEntity, in TId>
Task<TEntity> GetAsync(TId id);

Task<TEntity> GetAndIncludeAsync(TId id, string relationshipName);

Task<int> CountAsync(IQueryable<TEntity> entities);

Task<TEntity> FirstOrDefaultAsync(IQueryable<TEntity> entities);

Task<IReadOnlyList<TEntity>> ToListAsync(IQueryable<TEntity> entities);
}
}
44 changes: 43 additions & 1 deletion src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Internal.Query;
using JsonApiDotNetCore.Services;

namespace JsonApiDotNetCore.Extensions
{
// ReSharper disable once InconsistentNaming
public static class IQueryableExtensions
{
public static IQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, List<SortQuery> sortQueries)
{
if (sortQueries == null || sortQueries.Count == 0)
return source;

var orderedEntities = source.Sort(sortQueries[0]);

if (sortQueries.Count <= 1) return orderedEntities;

for (var i = 1; i < sortQueries.Count; i++)
orderedEntities = orderedEntities.Sort(sortQueries[i]);

return orderedEntities;
}

public static IOrderedQueryable<TSource> Sort<TSource>(this IQueryable<TSource> source, SortQuery sortQuery)
{
return sortQuery.Direction == SortDirection.Descending
Expand Down Expand Up @@ -62,6 +77,17 @@ private static IOrderedQueryable<TSource> CallGenericOrderMethod<TSource>(IQuery
return (IOrderedQueryable<TSource>)result;
}

public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> source, IJsonApiContext jsonApiContext, FilterQuery filterQuery)
{
if (filterQuery == null)
return source;

if (filterQuery.IsAttributeOfRelationship)
return source.Filter(new RelatedAttrFilterQuery(jsonApiContext, filterQuery));

return source.Filter(new AttrFilterQuery(jsonApiContext, filterQuery));
}

public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> source, AttrFilterQuery filterQuery)
{
if (filterQuery == null)
Expand Down Expand Up @@ -201,5 +227,21 @@ public static IQueryable<TSource> Select<TSource>(this IQueryable<TSource> sourc
Expression.Call(typeof(Queryable), "Select", new[] { sourceType, resultType },
source.Expression, Expression.Quote(selector)));
}

public static IQueryable<T> PageForward<T>(this IQueryable<T> source, int pageSize, int pageNumber)
{
if (pageSize > 0)
{
if (pageNumber == 0)
pageNumber = 1;

if (pageNumber > 0)
return source
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize);
}

return source;
}
}
}
9 changes: 4 additions & 5 deletions src/JsonApiDotNetCore/Services/EntityResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using JsonApiDotNetCore.Extensions;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCore.Services
Expand Down Expand Up @@ -50,7 +49,7 @@ public virtual async Task<IEnumerable<T>> GetAsync()
entities = IncludeRelationships(entities, _jsonApiContext.QuerySet.IncludedRelationships);

if (_jsonApiContext.Options.IncludeTotalRecordCount)
_jsonApiContext.PageManager.TotalRecords = await entities.CountAsync();
_jsonApiContext.PageManager.TotalRecords = await _entities.CountAsync(entities);

// pagination should be done last since it will execute the query
var pagedEntities = await ApplyPageQueryAsync(entities);
Expand All @@ -72,12 +71,12 @@ private bool ShouldIncludeRelationships()

private async Task<T> GetWithRelationshipsAsync(TId id)
{
var query = _entities.Get();
var query = _entities.Get().Where(e => e.Id.Equals(id));
_jsonApiContext.QuerySet.IncludedRelationships.ForEach(r =>
{
query = _entities.Include(query, r);
});
return await query.FirstOrDefaultAsync(e => e.Id.Equals(id));
return await _entities.FirstOrDefaultAsync(query);
}

public virtual async Task<object> GetRelationshipsAsync(TId id, string relationshipName)
Expand Down Expand Up @@ -166,7 +165,7 @@ private async Task<IEnumerable<T>> ApplyPageQueryAsync(IQueryable<T> entities)
{
var pageManager = _jsonApiContext.PageManager;
if (!pageManager.IsPaginated)
return entities;
return await _entities.ToListAsync(entities);

_logger?.LogInformation($"Applying paging query. Fetching page {pageManager.CurrentPage} with {pageManager.PageSize} entities");

Expand Down
34 changes: 15 additions & 19 deletions test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/PagingTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Bogus;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCoreExample;
using JsonApiDotNetCoreExample.Models;
Expand Down Expand Up @@ -50,7 +52,7 @@ public async Task Can_Paginate_TodoItems_From_Start() {
const int expectedEntitiesPerPage = 2;
var totalCount = expectedEntitiesPerPage * 2;
var person = new Person();
var todoItems = _todoItemFaker.Generate(totalCount);
var todoItems = _todoItemFaker.Generate(totalCount).ToList();

foreach (var todoItem in todoItems)
todoItem.Owner = person;
Expand All @@ -70,12 +72,8 @@ public async Task Can_Paginate_TodoItems_From_Start() {
var body = await response.Content.ReadAsStringAsync();
var deserializedBody = GetService<IJsonApiDeSerializer>().DeserializeList<TodoItem>(body);

Assert.NotEmpty(deserializedBody);
Assert.Equal(expectedEntitiesPerPage, deserializedBody.Count);

var expectedTodoItems = Context.TodoItems.Take(2);
foreach (var todoItem in expectedTodoItems)
Assert.NotNull(deserializedBody.SingleOrDefault(t => t.Id == todoItem.Id));
var expectedTodoItems = new[] { todoItems[0], todoItems[1] };
Assert.Equal(expectedTodoItems, deserializedBody, new IdComparer<TodoItem>());
}

[Fact]
Expand All @@ -84,7 +82,7 @@ public async Task Can_Paginate_TodoItems_From_End() {
const int expectedEntitiesPerPage = 2;
var totalCount = expectedEntitiesPerPage * 2;
var person = new Person();
var todoItems = _todoItemFaker.Generate(totalCount);
var todoItems = _todoItemFaker.Generate(totalCount).ToList();

foreach (var todoItem in todoItems)
todoItem.Owner = person;
Expand All @@ -104,18 +102,16 @@ public async Task Can_Paginate_TodoItems_From_End() {
var body = await response.Content.ReadAsStringAsync();
var deserializedBody = GetService<IJsonApiDeSerializer>().DeserializeList<TodoItem>(body);

Assert.NotEmpty(deserializedBody);
Assert.Equal(expectedEntitiesPerPage, deserializedBody.Count);
var expectedTodoItems = new[] { todoItems[totalCount - 2], todoItems[totalCount - 1] };
Assert.Equal(expectedTodoItems, deserializedBody, new IdComparer<TodoItem>());
}

var expectedTodoItems = Context.TodoItems
.OrderByDescending(t => t.Id)
.Take(2)
.ToList()
.OrderBy(t => t.Id)
.ToList();
private class IdComparer<T> : IEqualityComparer<T>
where T : IIdentifiable
{
public bool Equals(T x, T y) => x?.StringId == y?.StringId;

for (int i = 0; i < expectedEntitiesPerPage; i++)
Assert.Equal(expectedTodoItems[i].Id, deserializedBody[i].Id);
public int GetHashCode(T obj) => obj?.StringId?.GetHashCode() ?? 0;
}
}
}
}
80 changes: 79 additions & 1 deletion test/UnitTests/Data/DefaultEntityRepository_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
using Microsoft.Extensions.Logging;
using JsonApiDotNetCore.Services;
using System.Threading.Tasks;

using System.Linq;

namespace UnitTests.Data
{
public class DefaultEntityRepository_Tests : JsonApiControllerMixin
Expand Down Expand Up @@ -93,6 +94,83 @@ private DefaultEntityRepository<TodoItem> GetRepository()
_loggFactoryMock.Object,
_jsonApiContextMock.Object,
_contextResolverMock.Object);
}

[Theory]
[InlineData(0)]
[InlineData(-1)]
[InlineData(-10)]
public async Task Page_When_PageSize_Is_NonPositive_Does_Nothing(int pageSize)
{
var todoItems = DbSetMock.Create(TodoItems(2, 3, 1)).Object;
var repository = GetRepository();

var result = await repository.PageAsync(todoItems, pageSize, 3);

Assert.Equal(TodoItems(2, 3, 1), result, new IdComparer<TodoItem>());
}

[Fact]
public async Task Page_When_PageNumber_Is_Zero_Pretends_PageNumber_Is_One()
{
var todoItems = DbSetMock.Create(TodoItems(2, 3, 1)).Object;
var repository = GetRepository();

var result = await repository.PageAsync(todoItems, 1, 0);

Assert.Equal(TodoItems(2), result, new IdComparer<TodoItem>());
}

[Fact]
public async Task Page_When_PageNumber_Of_PageSize_Does_Not_Exist_Return_Empty_Queryable()
{
var todoItems = DbSetMock.Create(TodoItems(2, 3, 1)).Object;
var repository = GetRepository();

var result = await repository.PageAsync(todoItems, 2, 3);

Assert.Empty(result);
}

[Theory]
[InlineData(3, 2, new[] { 4, 5, 6 })]
[InlineData(8, 2, new[] { 9 })]
[InlineData(20, 1, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })]
public async Task Page_When_PageNumber_Is_Positive_Returns_PageNumberTh_Page_Of_Size_PageSize(int pageSize, int pageNumber, int[] expectedResult)
{
var todoItems = DbSetMock.Create(TodoItems(1, 2, 3, 4, 5, 6, 7, 8, 9)).Object;
var repository = GetRepository();

var result = await repository.PageAsync(todoItems, pageSize, pageNumber);

Assert.Equal(TodoItems(expectedResult), result, new IdComparer<TodoItem>());
}

[Theory]
[InlineData(6, -1, new[] { 4, 5, 6, 7, 8, 9 })]
[InlineData(6, -2, new[] { 1, 2, 3 })]
[InlineData(20, -1, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })]
public async Task Page_When_PageNumber_Is_Negative_Returns_PageNumberTh_Page_From_End(int pageSize, int pageNumber, int[] expectedIds)
{
var todoItems = DbSetMock.Create(TodoItems(1, 2, 3, 4, 5, 6, 7, 8, 9)).Object;
var repository = GetRepository();

var result = await repository.PageAsync(todoItems, pageSize, pageNumber);

Assert.Equal(TodoItems(expectedIds), result, new IdComparer<TodoItem>());
}

private static TodoItem[] TodoItems(params int[] ids)
{
return ids.Select(id => new TodoItem { Id = id }).ToArray();
}

private class IdComparer<T> : IEqualityComparer<T>
where T : IIdentifiable
{
public bool Equals(T x, T y) => x?.StringId == y?.StringId;

public int GetHashCode(T obj) => obj?.StringId?.GetHashCode() ?? 0;
}
}
}