mirror of https://github.com/Squidex/squidex.git
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.
148 lines
5.0 KiB
148 lines
5.0 KiB
// ==========================================================================
|
|
// 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
|
|
{
|
|
/// <summary>
|
|
/// Manages and configures apps.
|
|
/// </summary>
|
|
[ApiExplorerSettings(GroupName = nameof(Apps))]
|
|
public sealed class AppWorkflowsController : ApiController
|
|
{
|
|
private readonly IWorkflowsValidator workflowsValidator;
|
|
|
|
public AppWorkflowsController(ICommandBus commandBus, IWorkflowsValidator workflowsValidator)
|
|
: base(commandBus)
|
|
{
|
|
this.workflowsValidator = workflowsValidator;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get app workflow.
|
|
/// </summary>
|
|
/// <param name="app">The name of the app.</param>
|
|
/// <returns>
|
|
/// 200 => App workflows returned.
|
|
/// 404 => App not found.
|
|
/// </returns>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a workflow.
|
|
/// </summary>
|
|
/// <param name="app">The name of the app.</param>
|
|
/// <param name="request">The new workflow.</param>
|
|
/// <returns>
|
|
/// 200 => Workflow updated.
|
|
/// 400 => Workflow request is not valid.
|
|
/// 404 => Workflow or app not found.
|
|
/// </returns>
|
|
[HttpPost]
|
|
[Route("apps/{app}/workflows/")]
|
|
[ProducesResponseType(typeof(WorkflowsDto), 200)]
|
|
[ApiPermissionOrAnonymous(Permissions.AppWorkflowsUpdate)]
|
|
[ApiCosts(1)]
|
|
public async Task<IActionResult> PostWorkflow(string app, [FromBody] AddWorkflowDto request)
|
|
{
|
|
var command = request.ToCommand();
|
|
|
|
var response = await InvokeCommandAsync(command);
|
|
|
|
return Ok(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a workflow.
|
|
/// </summary>
|
|
/// <param name="app">The name of the app.</param>
|
|
/// <param name="request">The new workflow.</param>
|
|
/// <param name="id">The id of the workflow to update.</param>
|
|
/// <returns>
|
|
/// 200 => Workflow updated.
|
|
/// 400 => Workflow request is not valid.
|
|
/// 404 => Workflow or app not found.
|
|
/// </returns>
|
|
[HttpPut]
|
|
[Route("apps/{app}/workflows/{id}")]
|
|
[ProducesResponseType(typeof(WorkflowsDto), 200)]
|
|
[ApiPermissionOrAnonymous(Permissions.AppWorkflowsUpdate)]
|
|
[ApiCosts(1)]
|
|
public async Task<IActionResult> PutWorkflow(string app, Guid id, [FromBody] UpdateWorkflowDto request)
|
|
{
|
|
var command = request.ToCommand(id);
|
|
|
|
var response = await InvokeCommandAsync(command);
|
|
|
|
return Ok(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete a workflow.
|
|
/// </summary>
|
|
/// <param name="app">The name of the app.</param>
|
|
/// <param name="id">The id of the workflow to update.</param>
|
|
/// <returns>
|
|
/// 200 => Workflow deleted.
|
|
/// 404 => Workflow or app not found.
|
|
/// </returns>
|
|
[HttpDelete]
|
|
[Route("apps/{app}/workflows/{id}")]
|
|
[ProducesResponseType(typeof(WorkflowsDto), 200)]
|
|
[ApiPermissionOrAnonymous(Permissions.AppWorkflowsUpdate)]
|
|
[ApiCosts(1)]
|
|
public async Task<IActionResult> DeleteWorkflow(string app, Guid id)
|
|
{
|
|
var command = new DeleteWorkflow { WorkflowId = id };
|
|
|
|
var response = await InvokeCommandAsync(command);
|
|
|
|
return Ok(response);
|
|
}
|
|
|
|
private async Task<WorkflowsDto> InvokeCommandAsync(ICommand command)
|
|
{
|
|
var context = await CommandBus.PublishAsync(command);
|
|
|
|
var result = context.Result<IAppEntity>();
|
|
var response = await GetResponse(result);
|
|
|
|
return response;
|
|
}
|
|
|
|
private async Task<WorkflowsDto> GetResponse(IAppEntity result)
|
|
{
|
|
return await WorkflowsDto.FromAppAsync(workflowsValidator, result, Resources);
|
|
}
|
|
}
|
|
}
|
|
|