Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

103 lines
3.6 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using Squidex.Areas.Api.Controllers.Plans.Models;
using Squidex.Domain.Apps.Entities.Apps.Commands;
using Squidex.Domain.Apps.Entities.Apps.Services;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.Reflection;
using Squidex.Pipeline;
namespace Squidex.Areas.Api.Controllers.Plans
{
/// <summary>
/// Manages and configures plans.
/// </summary>
[ApiAuthorize]
[ApiExceptionFilter]
[AppApi]
[SwaggerTag(nameof(Plans))]
public sealed class AppPlansController : ApiController
{
private readonly IAppPlansProvider appPlansProvider;
private readonly IAppPlanBillingManager appPlansBillingManager;
public AppPlansController(ICommandBus commandBus,
IAppPlansProvider appPlansProvider,
IAppPlanBillingManager appPlansBillingManager)
: base(commandBus)
{
this.appPlansProvider = appPlansProvider;
this.appPlansBillingManager = appPlansBillingManager;
}
/// <summary>
/// Get app plan information.
/// </summary>
/// <param name="app">The name of the app.</param>
/// <returns>
/// 200 => App plan information returned.
/// 404 => App not found.
/// </returns>
[MustBeAppOwner]
[HttpGet]
[Route("apps/{app}/plans/")]
[ProducesResponseType(typeof(AppPlansDto), 200)]
[ApiCosts(0)]
public IActionResult GetPlans(string app)
{
var planId = appPlansProvider.GetPlanForApp(App).Id;
var plans = appPlansProvider.GetAvailablePlans().Select(x => SimpleMapper.Map(x, new PlanDto())).ToList();
var response = new AppPlansDto
{
CurrentPlanId = planId,
Plans = plans,
PlanOwner = App.Plan?.Owner.Identifier,
HasPortal = appPlansBillingManager.HasPortal
};
Response.Headers["ETag"] = App.Version.ToString();
return Ok(response);
}
/// <summary>
/// Change the app plan.
/// </summary>
/// <param name="app">The name of the app.</param>
/// <param name="request">Plan object that needs to be changed.</param>
/// <returns>
/// 201 => Redirected to checkout page.
/// 204 => Plan changed.
/// 400 => Plan not owned by user.
/// 404 => App not found.
/// </returns>
[MustBeAppOwner]
[HttpPut]
[Route("apps/{app}/plan/")]
[ProducesResponseType(typeof(PlanChangedDto), 200)]
[ProducesResponseType(typeof(ErrorDto), 400)]
[ApiCosts(0)]
public async Task<IActionResult> ChangePlanAsync(string app, [FromBody] ChangePlanDto request)
{
var redirectUri = (string)null;
var context = await CommandBus.PublishAsync(SimpleMapper.Map(request, new ChangePlan()));
if (context.Result<object>() is RedirectToCheckoutResult result)
{
redirectUri = result.Url.ToString();
}
return Ok(new PlanChangedDto { RedirectUri = redirectUri });
}
}
}