// ========================================================================== // 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.Apps; using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Domain.Apps.Entities.Contents; using Squidex.Infrastructure.Commands; using Squidex.Shared; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Apps { /// /// Manages and configures apps. /// [ApiExplorerSettings(GroupName = nameof(Apps))] public sealed class AppWorkflowsController : ApiController { private readonly IWorkflowsValidator workflowsValidator; public AppWorkflowsController(ICommandBus commandBus, IWorkflowsValidator workflowsValidator) : base(commandBus) { this.workflowsValidator = workflowsValidator; } /// /// Get app workflow. /// /// The name of the app. /// /// 200 => App workflows returned. /// 404 => App not found. /// [HttpGet] [Route("apps/{app}/workflows/")] [ProducesResponseType(typeof(WorkflowsDto), 200)] [ApiPermissionOrAnonymous(Permissions.AppWorkflowsRead)] [ApiCosts(0)] public IActionResult GetWorkflows(string app) { var response = Deferred.AsyncResponse(() => { return GetResponse(App); }); Response.Headers[HeaderNames.ETag] = App.ToEtag(); return Ok(response); } /// /// Create a workflow. /// /// The name of the app. /// The new workflow. /// /// 200 => Workflow updated. /// 400 => Workflow request is not valid. /// 404 => Workflow or app not found. /// [HttpPost] [Route("apps/{app}/workflows/")] [ProducesResponseType(typeof(WorkflowsDto), 200)] [ApiPermissionOrAnonymous(Permissions.AppWorkflowsUpdate)] [ApiCosts(1)] public async Task PostWorkflow(string app, [FromBody] AddWorkflowDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Update a workflow. /// /// The name of the app. /// The new workflow. /// The id of the workflow to update. /// /// 200 => Workflow updated. /// 400 => Workflow request is not valid. /// 404 => Workflow or app not found. /// [HttpPut] [Route("apps/{app}/workflows/{id}")] [ProducesResponseType(typeof(WorkflowsDto), 200)] [ApiPermissionOrAnonymous(Permissions.AppWorkflowsUpdate)] [ApiCosts(1)] public async Task PutWorkflow(string app, Guid id, [FromBody] UpdateWorkflowDto request) { var command = request.ToCommand(id); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Delete a workflow. /// /// The name of the app. /// The id of the workflow to update. /// /// 200 => Workflow deleted. /// 404 => Workflow or app not found. /// [HttpDelete] [Route("apps/{app}/workflows/{id}")] [ProducesResponseType(typeof(WorkflowsDto), 200)] [ApiPermissionOrAnonymous(Permissions.AppWorkflowsUpdate)] [ApiCosts(1)] public async Task DeleteWorkflow(string app, Guid id) { var command = new DeleteWorkflow { WorkflowId = id }; var response = await InvokeCommandAsync(command); return Ok(response); } private async Task InvokeCommandAsync(ICommand command) { var context = await CommandBus.PublishAsync(command); var result = context.Result(); var response = await GetResponse(result); return response; } private async Task GetResponse(IAppEntity result) { return await WorkflowsDto.FromAppAsync(workflowsValidator, result, Resources); } } }