Skip to content

Fix/pagesize #601

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 6 commits into from
Oct 31, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace JsonApiDotNetCoreExample
public class ClientGeneratedIdsStartup : Startup
{
public ClientGeneratedIdsStartup(IWebHostEnvironment env)
: base (env)
{ }
: base(env)
{ }

public override void ConfigureServices(IServiceCollection services)
{
Expand All @@ -41,4 +41,4 @@ public override void ConfigureServices(IServiceCollection services)
mvcBuilder: mvcBuilder);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using JsonApiDotNetCoreExample.Data;
using Microsoft.EntityFrameworkCore;
using JsonApiDotNetCore.Extensions;
using System.Reflection;

namespace JsonApiDotNetCoreExample
{
/// <summary>
/// This should be in JsonApiDotNetCoreExampleTests project but changes in .net core 3.0
/// do no longer allow that. See https://github.com/aspnet/AspNetCore/issues/15373.
/// </summary>
public class NoDefaultPageSizeStartup : Startup
{
public NoDefaultPageSizeStartup(IWebHostEnvironment env)
: base(env)
{ }

public override void ConfigureServices(IServiceCollection services)
{
var loggerFactory = new LoggerFactory();
var mvcBuilder = services.AddMvcCore();
services
.AddSingleton<ILoggerFactory>(loggerFactory)
.AddLogging(builder =>
{
builder.AddConsole();
})
.AddDbContext<AppDbContext>(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Transient)
.AddJsonApi(options => {
options.Namespace = "api/v1";
options.IncludeTotalRecordCount = true;
options.EnableResourceHooks = true;
options.LoaDatabaseValues = true;
options.AllowClientGeneratedIds = true;
},
discovery => discovery.AddAssembly(Assembly.Load(nameof(JsonApiDotNetCoreExample))),
mvcBuilder: mvcBuilder);
}
}
}
2 changes: 2 additions & 0 deletions src/Examples/NoEntityFrameworkExample/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ bld/
[Bb]in/
[Oo]bj/

Properties/launchSettings.json

# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ public interface IPageService : IQueryParameterService
/// </summary>
int PageSize { get; set; }
/// <summary>
/// What is the default page size
/// </summary>
int DefaultPageSize { get; set; }
/// <summary>
/// What page are we currently on
/// </summary>
int CurrentPage { get; set; }

/// <summary>
/// Total amount of pages for request
/// </summary>
int TotalPages { get; }

/// <summary>
/// Checks if pagination is enabled
/// </summary>
Expand Down
5 changes: 1 addition & 4 deletions src/JsonApiDotNetCore/QueryParameterServices/PageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ public class PageService : QueryParameterService, IPageService
public PageService(IJsonApiOptions options)
{
_options = options;
DefaultPageSize = _options.DefaultPageSize;
PageSize = _options.DefaultPageSize;
}
/// <inheritdoc/>
public int? TotalRecords { get; set; }
/// <inheritdoc/>
public int PageSize { get; set; }
/// <inheritdoc/>
public int DefaultPageSize { get; set; } // I think we shouldnt expose this
/// <inheritdoc/>
public int CurrentPage { get; set; }
/// <inheritdoc/>
public int TotalPages => (TotalRecords == null) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize));
public int TotalPages => (TotalRecords == null || PageSize == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize));

/// <inheritdoc/>
public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Bogus;
Expand Down Expand Up @@ -96,5 +97,31 @@ public async Task Included_Records_Contain_Relationship_Links()
Assert.Equal($"http://localhost/api/v1/people/{person.Id}/relationships/todo-items", deserializedBody.Included[0].Relationships["todo-items"].Links.Self);
context.Dispose();
}

[Fact]
public async Task GetResources_NoDefaultPageSize_ReturnsResources()
{
// Arrange
var context = _fixture.GetService<AppDbContext>();
var todoItems = _todoItemFaker.Generate(20).ToList();
context.TodoItems.AddRange(todoItems);
await context.SaveChangesAsync();

var builder = new WebHostBuilder()
.UseStartup<NoDefaultPageSizeStartup>();
var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items";
var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// Act
var response = await client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
var result = _fixture.GetDeserializer().DeserializeList<TodoItem>(body);

// Assert
Assert.True(result.Data.Count >= 20);
}
}
}