// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Microsoft.AspNetCore.Mvc;
using Squidex.Areas.Api.Controllers.Apps.Models;
using Squidex.Domain.Apps.Core.Apps;
using Squidex.Infrastructure.Commands;
using Squidex.Shared;
using Squidex.Web;
namespace Squidex.Areas.Api.Controllers.Apps;
///
/// Update and query apps.
///
[ApiExplorerSettings(GroupName = nameof(Apps))]
public sealed class AppSettingsController : ApiController
{
public AppSettingsController(ICommandBus commandBus)
: base(commandBus)
{
}
///
/// Get the app settings.
///
/// The name of the app to get the settings for.
/// App settings returned.
/// App not found.
[HttpGet]
[Route("apps/{app}/settings")]
[ProducesResponseType(typeof(AppSettingsDto), StatusCodes.Status200OK)]
[ApiPermissionOrAnonymous]
[ApiCosts(0)]
public IActionResult GetSettings(string app)
{
var response = Deferred.Response(() =>
{
return GetResponse(App);
});
return Ok(response);
}
///
/// Update the settings.
///
/// The name of the app to update.
/// The values to update.
/// App updated.
/// App request not valid.
/// App not found.
[HttpPut]
[Route("apps/{app}/settings")]
[ProducesResponseType(typeof(AppSettingsDto), StatusCodes.Status200OK)]
[ApiPermissionOrAnonymous(PermissionIds.AppUpdateSettings)]
[ApiCosts(0)]
public async Task PutSettings(string app, [FromBody] UpdateAppSettingsDto request)
{
var command = request.ToCommand();
var response = await InvokeCommandAsync(command);
return Ok(response);
}
private async Task InvokeCommandAsync(ICommand command)
{
var context = await CommandBus.PublishAsync(command, HttpContext.RequestAborted);
var result = context.Result();
var response = GetResponse(result);
return response;
}
private AppSettingsDto GetResponse(App result)
{
return AppSettingsDto.FromDomain(result, Resources);
}
}