// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; using Squidex.Areas.Api.Controllers.Apps.Models; using Squidex.Domain.Apps.Entities; using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Domain.Apps.Entities.Apps.Services; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Security; using Squidex.Shared; using Squidex.Shared.Identity; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Apps { /// /// Manages and configures apps. /// [ApiExplorerSettings(GroupName = nameof(Apps))] public sealed class AppsController : ApiController { private readonly IAppProvider appProvider; private readonly IAppPlansProvider appPlansProvider; public AppsController(ICommandBus commandBus, IAppProvider appProvider, IAppPlansProvider appPlansProvider) : base(commandBus) { this.appProvider = appProvider; this.appPlansProvider = appPlansProvider; } /// /// Get your apps. /// /// /// 200 => Apps returned. /// /// /// You can only retrieve the list of apps when you are authenticated as a user (OpenID implicit flow). /// You will retrieve all apps, where you are assigned as a contributor. /// [HttpGet] [Route("apps/")] [ProducesResponseType(typeof(AppDto[]), 200)] [ApiPermission] [ApiCosts(0)] public async Task GetApps() { var userOrClientId = HttpContext.User.UserOrClientId(); var userPermissions = HttpContext.Permissions(); var entities = await appProvider.GetUserApps(userOrClientId, userPermissions); var response = entities.ToArray(a => AppDto.FromApp(a, userOrClientId, userPermissions, appPlansProvider, this)); Response.Headers[HeaderNames.ETag] = response.ToManyEtag(); return Ok(response); } /// /// Create a new app. /// /// The app object that needs to be added to squidex. /// /// 201 => App created. /// 400 => App request not valid. /// 409 => App name is already in use. /// /// /// You can only create an app when you are authenticated as a user (OpenID implicit flow). /// You will be assigned as owner of the new app automatically. /// [HttpPost] [Route("apps/")] [ProducesResponseType(typeof(AppCreatedDto), 201)] [ProducesResponseType(typeof(ErrorDto), 400)] [ProducesResponseType(typeof(ErrorDto), 409)] [ApiPermission] [ApiCosts(1)] public async Task PostApp([FromBody] CreateAppDto request) { var context = await CommandBus.PublishAsync(request.ToCommand()); var result = context.Result>(); var response = AppCreatedDto.FromResult(request.Name, result, appPlansProvider); return CreatedAtAction(nameof(GetApps), response); } /// /// Archive the app. /// /// The name of the app to archive. /// /// 204 => App archived. /// 404 => App not found. /// [HttpDelete] [Route("apps/{app}/")] [ApiPermission(Permissions.AppDelete)] [ApiCosts(1)] public async Task DeleteApp(string app) { await CommandBus.PublishAsync(new ArchiveApp()); return NoContent(); } } }