mirror of https://github.com/Squidex/squidex.git
39 changed files with 583 additions and 143 deletions
@ -0,0 +1,21 @@ |
|||||
|
// ==========================================================================
|
||||
|
// IAppLimitsPlan.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Read.Apps.Services |
||||
|
{ |
||||
|
public interface IAppLimitsPlan |
||||
|
{ |
||||
|
string Name { get; } |
||||
|
|
||||
|
long MaxApiCalls { get; } |
||||
|
|
||||
|
long MaxAssetSize { get; } |
||||
|
|
||||
|
int MaxContributors { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
// ==========================================================================
|
||||
|
// IAppLimitsProvider.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Squidex.Read.Apps.Services |
||||
|
{ |
||||
|
public interface IAppLimitsProvider |
||||
|
{ |
||||
|
IEnumerable<IAppLimitsPlan> GetAvailablePlans(); |
||||
|
|
||||
|
IAppLimitsPlan GetPlanForApp(IAppEntity entity); |
||||
|
|
||||
|
IAppLimitsPlan GetPlan(int planId); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ConfigAppLimitsPlan.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Read.Apps.Services.Implementations |
||||
|
{ |
||||
|
public sealed class ConfigAppLimitsPlan : IAppLimitsPlan |
||||
|
{ |
||||
|
public string Name { get; set; } |
||||
|
|
||||
|
public long MaxApiCalls { get; set; } |
||||
|
|
||||
|
public long MaxAssetSize { get; set; } |
||||
|
|
||||
|
public int MaxContributors { get; set; } |
||||
|
|
||||
|
public ConfigAppLimitsPlan Clone() |
||||
|
{ |
||||
|
return (ConfigAppLimitsPlan)MemberwiseClone(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,56 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ConfigAppLimitsProvider.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Squidex.Infrastructure; |
||||
|
|
||||
|
namespace Squidex.Read.Apps.Services.Implementations |
||||
|
{ |
||||
|
public sealed class ConfigAppLimitsProvider : IAppLimitsProvider |
||||
|
{ |
||||
|
private static readonly ConfigAppLimitsPlan Infinite = new ConfigAppLimitsPlan |
||||
|
{ |
||||
|
Name = "Infinite", |
||||
|
MaxApiCalls = -1, |
||||
|
MaxAssetSize = -1, |
||||
|
MaxContributors = -1 |
||||
|
}; |
||||
|
|
||||
|
private readonly List<ConfigAppLimitsPlan> config; |
||||
|
|
||||
|
public ConfigAppLimitsProvider(IEnumerable<ConfigAppLimitsPlan> config) |
||||
|
{ |
||||
|
Guard.NotNull(config, nameof(config)); |
||||
|
|
||||
|
this.config = config.Select(c => c.Clone()).OrderBy(x => x.MaxApiCalls).ToList(); |
||||
|
} |
||||
|
|
||||
|
public IEnumerable<IAppLimitsPlan> GetAvailablePlans() |
||||
|
{ |
||||
|
return config; |
||||
|
} |
||||
|
|
||||
|
public IAppLimitsPlan GetPlanForApp(IAppEntity app) |
||||
|
{ |
||||
|
Guard.NotNull(app, nameof(app)); |
||||
|
|
||||
|
return GetPlan(app.PlanId); |
||||
|
} |
||||
|
|
||||
|
public IAppLimitsPlan GetPlan(int planId) |
||||
|
{ |
||||
|
if (planId >= 0 && planId < config.Count) |
||||
|
{ |
||||
|
return config[planId]; |
||||
|
} |
||||
|
|
||||
|
return config.FirstOrDefault() ?? Infinite; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
// ==========================================================================
|
||||
|
// MyUsageOptions.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Squidex.Read.Apps.Services.Implementations; |
||||
|
|
||||
|
namespace Squidex.Config |
||||
|
{ |
||||
|
public class MyUsageOptions |
||||
|
{ |
||||
|
public ConfigAppLimitsPlan[] Plans { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ContributorsDto.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
|
||||
|
namespace Squidex.Controllers.Api.Apps.Models |
||||
|
{ |
||||
|
public class ContributorsDto |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The contributors.
|
||||
|
/// </summary>
|
||||
|
[Required] |
||||
|
public ContributorDto[] Contributors { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The maximum number of allowed contributors.
|
||||
|
/// </summary>
|
||||
|
public int MaxContributors { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,102 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ConfigAppLimitsProviderTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using Moq; |
||||
|
using Squidex.Read.Apps.Services.Implementations; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Read.Apps |
||||
|
{ |
||||
|
public class ConfigAppLimitsProviderTests |
||||
|
{ |
||||
|
private static readonly ConfigAppLimitsPlan[] Plans = |
||||
|
{ |
||||
|
new ConfigAppLimitsPlan |
||||
|
{ |
||||
|
Name = "Basic", |
||||
|
MaxApiCalls = 150000, |
||||
|
MaxAssetSize = 1024 * 1024 * 2, |
||||
|
MaxContributors = 5 |
||||
|
}, |
||||
|
new ConfigAppLimitsPlan |
||||
|
{ |
||||
|
Name = "Free", |
||||
|
MaxApiCalls = 50000, |
||||
|
MaxAssetSize = 1024 * 1024 * 10, |
||||
|
MaxContributors = 2 |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_return_plans() |
||||
|
{ |
||||
|
var sut = new ConfigAppLimitsProvider(Plans); |
||||
|
|
||||
|
Plans.OrderBy(x => x.MaxApiCalls).ShouldBeEquivalentTo(sut.GetAvailablePlans()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_return_infinite_if_nothing_configured() |
||||
|
{ |
||||
|
var sut = new ConfigAppLimitsProvider(Enumerable.Empty<ConfigAppLimitsPlan>()); |
||||
|
|
||||
|
var plan = sut.GetPlanForApp(CreateApp(0)); |
||||
|
|
||||
|
plan.ShouldBeEquivalentTo(new ConfigAppLimitsPlan |
||||
|
{ |
||||
|
Name = "Infinite", |
||||
|
MaxApiCalls = -1, |
||||
|
MaxAssetSize = -1, |
||||
|
MaxContributors = -1 |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_return_fitting_app_plan() |
||||
|
{ |
||||
|
var sut = new ConfigAppLimitsProvider(Plans); |
||||
|
|
||||
|
var plan = sut.GetPlanForApp(CreateApp(1)); |
||||
|
|
||||
|
plan.ShouldBeEquivalentTo(new ConfigAppLimitsPlan |
||||
|
{ |
||||
|
Name = "Basic", |
||||
|
MaxApiCalls = 150000, |
||||
|
MaxAssetSize = 1024 * 1024 * 2, |
||||
|
MaxContributors = 5 |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_smallest_plan_if_none_fits() |
||||
|
{ |
||||
|
var sut = new ConfigAppLimitsProvider(Plans); |
||||
|
|
||||
|
var plan = sut.GetPlanForApp(CreateApp(4)); |
||||
|
|
||||
|
plan.ShouldBeEquivalentTo(new ConfigAppLimitsPlan |
||||
|
{ |
||||
|
Name = "Free", |
||||
|
MaxApiCalls = 50000, |
||||
|
MaxAssetSize = 1024 * 1024 * 10, |
||||
|
MaxContributors = 2 |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private static IAppEntity CreateApp(int plan) |
||||
|
{ |
||||
|
var app = new Mock<IAppEntity>(); |
||||
|
|
||||
|
app.Setup(x => x.PlanId).Returns(plan); |
||||
|
|
||||
|
return app.Object; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue