Skip to content

feature(paging): support negative paging #180

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 2 commits into from
Oct 25, 2017
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
56 changes: 35 additions & 21 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace JsonApiDotNetCore.Data
{
public class DefaultEntityRepository<TEntity>
public class DefaultEntityRepository<TEntity>
: DefaultEntityRepository<TEntity, int>,
IEntityRepository<TEntity>
IEntityRepository<TEntity>
where TEntity : class, IIdentifiable<int>
{
public DefaultEntityRepository(
Expand All @@ -25,8 +25,8 @@ public DefaultEntityRepository(
{ }
}

public class DefaultEntityRepository<TEntity, TId>
: IEntityRepository<TEntity, TId>
public class DefaultEntityRepository<TEntity, TId>
: IEntityRepository<TEntity, TId>
where TEntity : class, IIdentifiable<TId>
{
private readonly DbContext _context;
Expand Down Expand Up @@ -62,33 +62,33 @@ public DefaultEntityRepository(

public virtual IQueryable<TEntity> Get()
{
if(_jsonApiContext.QuerySet?.Fields != null && _jsonApiContext.QuerySet.Fields.Any())
if (_jsonApiContext.QuerySet?.Fields != null && _jsonApiContext.QuerySet.Fields.Any())
return _dbSet.Select(_jsonApiContext.QuerySet?.Fields);

return _dbSet;
}

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

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

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

public virtual IQueryable<TEntity> Sort(IQueryable<TEntity> entities, List<SortQuery> sortQueries)
{
if(sortQueries == null || sortQueries.Count == 0)
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++)
for (var i = 1; i < sortQueries.Count; i++)
orderedEntities = orderedEntities.Sort(sortQueries[i]);

return orderedEntities;
Expand Down Expand Up @@ -124,10 +124,10 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
if (oldEntity == null)
return null;

foreach(var attr in _jsonApiContext.AttributesToUpdate)
foreach (var attr in _jsonApiContext.AttributesToUpdate)
attr.Key.SetValue(oldEntity, attr.Value);

foreach(var relationship in _jsonApiContext.RelationshipsToUpdate)
foreach (var relationship in _jsonApiContext.RelationshipsToUpdate)
relationship.Key.SetValue(oldEntity, relationship.Value);

await _context.SaveChangesAsync();
Expand Down Expand Up @@ -159,20 +159,34 @@ public virtual IQueryable<TEntity> Include(IQueryable<TEntity> entities, string
{
var entity = _jsonApiContext.RequestEntity;
var relationship = entity.Relationships.FirstOrDefault(r => r.PublicRelationshipName == relationshipName);
if(relationship != null)
if (relationship != null)
return entities.Include(relationship.InternalRelationshipName);

throw new JsonApiException(400, $"Invalid relationship {relationshipName} on {entity.EntityName}",
$"{entity.EntityName} does not have a relationship named {relationshipName}");
}

public virtual async Task<IEnumerable<TEntity>> PageAsync(IQueryable<TEntity> entities, int pageSize, int pageNumber)
{
if(pageSize > 0)
return await entities
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
if (pageSize > 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.ToListAsync();
}
Expand Down
22 changes: 12 additions & 10 deletions src/JsonApiDotNetCore/Internal/PageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,30 @@ public class PageManager
public int DefaultPageSize { get; set; }
public int CurrentPage { get; set; }
public bool IsPaginated => PageSize > 0;
public int TotalPages => (TotalRecords == 0) ? -1: (int)Math.Ceiling(decimal.Divide(TotalRecords, PageSize));
public int TotalPages => (TotalRecords == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords, PageSize));

public RootLinks GetPageLinks(LinkBuilder linkBuilder)
{
if(!IsPaginated || (CurrentPage == 1 && TotalPages <= 0))
{
if (ShouldIncludeLinksObject())
return null;

var rootLinks = new RootLinks();

if(CurrentPage > 1)
if (CurrentPage > 1)
rootLinks.First = linkBuilder.GetPageLink(1, PageSize);

if(CurrentPage > 1)
if (CurrentPage > 1)
rootLinks.Prev = linkBuilder.GetPageLink(CurrentPage - 1, PageSize);
if(CurrentPage < TotalPages)

if (CurrentPage < TotalPages)
rootLinks.Next = linkBuilder.GetPageLink(CurrentPage + 1, PageSize);
if(TotalPages > 0)

if (TotalPages > 0)
rootLinks.Last = linkBuilder.GetPageLink(TotalPages, PageSize);

return rootLinks;
}

private bool ShouldIncludeLinksObject() => (!IsPaginated || ((CurrentPage == 1 || CurrentPage == 0) && TotalPages <= 0));
}
}
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.1.6</VersionPrefix>
<VersionPrefix>2.1.7</VersionPrefix>
<TargetFrameworks>netstandard1.6</TargetFrameworks>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace JsonApiDotNetCore.Serialization
public interface IJsonApiDeSerializer
{
object Deserialize(string requestBody);
TEntity Deserialize<TEntity>(string requestBody);
object DeserializeRelationship(string requestBody);
List<TEntity> DeserializeList<TEntity>(string requestBody);
}
Expand Down
5 changes: 2 additions & 3 deletions src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public object Deserialize(string requestBody)
}
}

public TEntity Deserialize<TEntity>(string requestBody)
=> (TEntity)Deserialize(requestBody);
public TEntity Deserialize<TEntity>(string requestBody) => (TEntity)Deserialize(requestBody);

public object DeserializeRelationship(string requestBody)
{
Expand Down Expand Up @@ -117,7 +116,7 @@ private object SetEntityAttributes(
var convertedValue = ConvertAttrValue(newValue, entityProperty.PropertyType);
entityProperty.SetValue(entity, convertedValue);

if(attr.IsImmutable == false)
if (attr.IsImmutable == false)
_jsonApiContext.AttributesToUpdate[attr] = convertedValue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Services/JsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private PageManager GetPageManager()
return new PageManager
{
DefaultPageSize = Options.DefaultPageSize,
CurrentPage = query.PageOffset > 0 ? query.PageOffset : 1,
CurrentPage = query.PageOffset,
PageSize = query.PageSize > 0 ? query.PageSize : Options.DefaultPageSize
};
}
Expand Down
134 changes: 134 additions & 0 deletions test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/PagingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System.Collections.Generic;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Bogus;
using DotNetCoreDocs;
using DotNetCoreDocs.Models;
using DotNetCoreDocs.Writers;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Models;
using Xunit;
using Person = JsonApiDotNetCoreExample.Models.Person;

namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
{
public class PagingTests : TestFixture<Startup>
{
private readonly Faker<TodoItem> _todoItemFaker = new Faker<TodoItem>()
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
.RuleFor(t => t.Ordinal, f => f.Random.Number())
.RuleFor(t => t.CreatedDate, f => f.Date.Past());

[Fact]
public async Task Can_Paginate_TodoItems()
{
// Arrange
const int expectedEntitiesPerPage = 2;
var totalCount = expectedEntitiesPerPage * 2;
var person = new Person();
var todoItems = _todoItemFaker.Generate(totalCount);

foreach (var todoItem in todoItems)
todoItem.Owner = person;

Context.TodoItems.AddRange(todoItems);
Context.SaveChanges();

var route = $"/api/v1/todo-items?page[size]={expectedEntitiesPerPage}";

// Act
var response = await Client.GetAsync(route);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var body = await response.Content.ReadAsStringAsync();
var deserializedBody = GetService<IJsonApiDeSerializer>().DeserializeList<TodoItem>(body);

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

[Fact]
public async Task Can_Paginate_TodoItems_From_Start()
{
// Arrange
const int expectedEntitiesPerPage = 2;
var totalCount = expectedEntitiesPerPage * 2;
var person = new Person();
var todoItems = _todoItemFaker.Generate(totalCount);

foreach (var todoItem in todoItems)
todoItem.Owner = person;

Context.TodoItems.RemoveRange(Context.TodoItems);
Context.TodoItems.AddRange(todoItems);
Context.SaveChanges();

var route = $"/api/v1/todo-items?page[size]={expectedEntitiesPerPage}&page[number]=1";

// Act
var response = await Client.GetAsync(route);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

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));
}

[Fact]
public async Task Can_Paginate_TodoItems_From_End()
{
// Arrange
const int expectedEntitiesPerPage = 2;
var totalCount = expectedEntitiesPerPage * 2;
var person = new Person();
var todoItems = _todoItemFaker.Generate(totalCount);

foreach (var todoItem in todoItems)
todoItem.Owner = person;

Context.TodoItems.RemoveRange(Context.TodoItems);
Context.TodoItems.AddRange(todoItems);
Context.SaveChanges();

var route = $"/api/v1/todo-items?page[size]={expectedEntitiesPerPage}&page[number]=-1";

// Act
var response = await Client.GetAsync(route);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

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
.OrderByDescending(t => t.Id)
.Take(2)
.ToList()
.OrderBy(t => t.Id)
.ToList();

for (int i = 0; i < expectedEntitiesPerPage; i++)
Assert.Equal(expectedTodoItems[i].Id, deserializedBody[i].Id);
}
}
}
37 changes: 37 additions & 0 deletions test/JsonApiDotNetCoreExampleTests/Acceptance/TestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Net.Http;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCoreExample.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using JsonApiDotNetCore.Services;
using Newtonsoft.Json;

namespace JsonApiDotNetCoreExampleTests.Acceptance
{
public class TestFixture<TStartup> where TStartup : class
{
private readonly TestServer _server;
private IServiceProvider _services;

public TestFixture()
{
var builder = new WebHostBuilder()
.UseStartup<TStartup>();

_server = new TestServer(builder);
_services = _server.Host.Services;

Client = _server.CreateClient();
Context = GetService<AppDbContext>();
DeSerializer = GetService<IJsonApiDeSerializer>();
JsonApiContext = GetService<IJsonApiContext>();
}

public HttpClient Client { get; set; }
public AppDbContext Context { get; private set; }
public IJsonApiDeSerializer DeSerializer { get; private set; }
public IJsonApiContext JsonApiContext { get; private set; }
public T GetService<T>() => (T)_services.GetService(typeof(T));
}
}
Loading