Skip to content

Commit

Permalink
Options to specify the number of items to return.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Oct 14, 2018
1 parent e3c07fb commit e880a22
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/Squidex.Domain.Apps.Entities/Assets/AssetOptions.cs
@@ -0,0 +1,14 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

namespace Squidex.Domain.Apps.Entities.Assets
{
public sealed class AssetOptions
{
public int MaxResults { get; set; } = 100;
}
}
13 changes: 7 additions & 6 deletions src/Squidex.Domain.Apps.Entities/Assets/AssetQueryService.cs
Expand Up @@ -22,18 +22,19 @@ namespace Squidex.Domain.Apps.Entities.Assets
{
public sealed class AssetQueryService : IAssetQueryService
{
private const int MaxResults = 200;
private readonly ITagService tagService;
private readonly IAssetRepository assetRepository;
private readonly AssetOptions options;

public AssetQueryService(ITagService tagService, IAssetRepository assetRepository)
public AssetQueryService(ITagService tagService, IAssetRepository assetRepository, AssetOptions options)
{
Guard.NotNull(tagService, nameof(tagService));
Guard.NotNull(options, nameof(options));
Guard.NotNull(assetRepository, nameof(assetRepository));

this.tagService = tagService;

this.assetRepository = assetRepository;
this.options = options;
this.tagService = tagService;
}

public async Task<IAssetEntity> FindAssetAsync(QueryContext context, Guid id)
Expand Down Expand Up @@ -97,9 +98,9 @@ private Query ParseQuery(QueryContext context, string query)
result.Sort.Add(new SortNode(new List<string> { "lastModified" }, SortOrder.Descending));
}

if (result.Take > MaxResults)
if (result.Take > options.MaxResults)
{
result.Take = MaxResults;
result.Take = options.MaxResults;
}

return result;
Expand Down
14 changes: 14 additions & 0 deletions src/Squidex.Domain.Apps.Entities/Contents/ContentOptions.cs
@@ -0,0 +1,14 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

namespace Squidex.Domain.Apps.Entities.Contents
{
public sealed class ContentOptions
{
public int MaxResults { get; set; } = 100;
}
}
Expand Up @@ -28,7 +28,6 @@ namespace Squidex.Domain.Apps.Entities.Contents
{
public sealed class ContentQueryService : IContentQueryService
{
private const int MaxResults = 200;
private static readonly Status[] StatusAll = { Status.Archived, Status.Draft, Status.Published };
private static readonly Status[] StatusArchived = { Status.Archived };
private static readonly Status[] StatusPublished = { Status.Published };
Expand All @@ -37,25 +36,29 @@ public sealed class ContentQueryService : IContentQueryService
private readonly IContentVersionLoader contentVersionLoader;
private readonly IAppProvider appProvider;
private readonly IScriptEngine scriptEngine;
private readonly ContentOptions options;
private readonly EdmModelBuilder modelBuilder;

public ContentQueryService(
IAppProvider appProvider,
IContentRepository contentRepository,
IContentVersionLoader contentVersionLoader,
IScriptEngine scriptEngine,
ContentOptions options,
EdmModelBuilder modelBuilder)
{
Guard.NotNull(appProvider, nameof(appProvider));
Guard.NotNull(contentRepository, nameof(contentRepository));
Guard.NotNull(contentVersionLoader, nameof(contentVersionLoader));
Guard.NotNull(modelBuilder, nameof(modelBuilder));
Guard.NotNull(options, nameof(options));
Guard.NotNull(scriptEngine, nameof(scriptEngine));

this.appProvider = appProvider;
this.contentRepository = contentRepository;
this.contentVersionLoader = contentVersionLoader;
this.modelBuilder = modelBuilder;
this.options = options;
this.scriptEngine = scriptEngine;
}

Expand Down Expand Up @@ -214,9 +217,9 @@ private Query ParseQuery(QueryContext context, string query, ISchemaEntity schem
result.Sort.Add(new SortNode(new List<string> { "lastModified" }, SortOrder.Descending));
}

if (result.Take > MaxResults)
if (result.Take > options.MaxResults)
{
result.Take = MaxResults;
result.Take = options.MaxResults;
}

return result;
Expand Down
7 changes: 6 additions & 1 deletion src/Squidex/AppServices.cs
Expand Up @@ -14,6 +14,8 @@
using Squidex.Config.Authentication;
using Squidex.Config.Domain;
using Squidex.Config.Web;
using Squidex.Domain.Apps.Entities.Assets;
using Squidex.Domain.Apps.Entities.Contents;
using Squidex.Extensions.Actions.Twitter;
using Squidex.Infrastructure.Commands;

Expand Down Expand Up @@ -44,9 +46,12 @@ public static void AddAppServices(this IServiceCollection services, IConfigurati
services.AddMySwaggerSettings();
services.AddMySubscriptionServices(config);

services.Configure<ContentOptions>(
config.GetSection("contents"));
services.Configure<AssetOptions>(
config.GetSection("assets"));
services.Configure<ReadonlyOptions>(
config.GetSection("mode"));

services.Configure<TwitterOptions>(
config.GetSection("twitter"));

Expand Down
6 changes: 6 additions & 0 deletions src/Squidex/Config/Domain/EntitiesServices.cs
Expand Up @@ -65,9 +65,15 @@ public static void AddMyEntitiesServices(this IServiceCollection services, IConf
services.AddSingletonAs<AppProvider>()
.As<IAppProvider>();

services.AddSingletonAs(c => c.GetRequiredService<IOptions<AssetOptions>>().Value)
.AsSelf();

services.AddSingletonAs<AssetQueryService>()
.As<IAssetQueryService>();

services.AddSingletonAs(c => c.GetRequiredService<IOptions<ContentOptions>>().Value)
.AsSelf();

services.AddSingletonAs<ContentQueryService>()
.As<IContentQueryService>();

Expand Down
18 changes: 18 additions & 0 deletions src/Squidex/appsettings.json
Expand Up @@ -63,6 +63,24 @@
"maxItemsForSurrogateKeys": 200
},

"content": {
/*
* The maximum number of items to return for each query.
*
* Warning: Use pagination and not large number of items.
*/
"maxResults": 200
},

"assets": {
/*
* The maximum number of items to return for each query.
*
* Warning: Use pagination and not large number of items.
*/
"maxResults": 200
},

"logging": {
/*
* Setting the flag to true, enables well formatteds json logs.
Expand Down
Expand Up @@ -49,7 +49,7 @@ public AssetQueryServiceTests()
["id3"] = "name3"
});

sut = new AssetQueryService(tagService, assetRepository);
sut = new AssetQueryService(tagService, assetRepository, new AssetOptions());
}

[Fact]
Expand Down
Expand Up @@ -60,7 +60,7 @@ public ContentQueryServiceTests()

context = new ContentQueryContext(QueryContext.Create(app, user));

sut = new ContentQueryService(appProvider, contentRepository, contentVersionLoader, scriptEngine, modelBuilder);
sut = new ContentQueryService(appProvider, contentRepository, contentVersionLoader, scriptEngine, new ContentOptions(), modelBuilder);
}

[Fact]
Expand Down

2 comments on commit e880a22

@humanbeinc
Copy link

Choose a reason for hiding this comment

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

@SebastianStehle Seems like this feature/option never made it to the .NET SDK?

@SebastianStehle
Copy link
Contributor Author

@SebastianStehle SebastianStehle commented on e880a22 Sep 27, 2020

Choose a reason for hiding this comment

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

This is a pure server side feature, I don't see how it can be ported to the SDK.

This feature just makes the hard limit of 200 items configurable.

Please sign in to comment.