// ========================================================================== // 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.Infrastructure.Commands; using Squidex.Shared; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Apps { /// /// Manages and configures app patterns. /// [ApiExplorerSettings(GroupName = nameof(Apps))] public sealed class AppPatternsController : ApiController { public AppPatternsController(ICommandBus commandBus) : base(commandBus) { } /// /// Get app patterns. /// /// The name of the app. /// /// 200 => Patterns returned. /// 404 => App not found. /// /// /// Gets all configured regex patterns for the app with the specified name. /// [HttpGet] [Route("apps/{app}/patterns/")] [ProducesResponseType(typeof(PatternsDto), 200)] [ApiPermissionOrAnonymous(Permissions.AppPatternsRead)] [ApiCosts(0)] public IActionResult GetPatterns(string app) { var response = Deferred.Response(() => { return GetResponse(App); }); Response.Headers[HeaderNames.ETag] = App.ToEtag(); return Ok(response); } /// /// Create a new app pattern. /// /// The name of the app. /// Pattern to be added to the app. /// /// 201 => Pattern generated. /// 400 => Pattern request not valid. /// 404 => App not found. /// [HttpPost] [Route("apps/{app}/patterns/")] [ProducesResponseType(typeof(PatternsDto), 201)] [ApiPermissionOrAnonymous(Permissions.AppPatternsCreate)] [ApiCosts(1)] public async Task PostPattern(string app, [FromBody] UpdatePatternDto request) { var command = request.ToAddCommand(); var response = await InvokeCommandAsync(command); return CreatedAtAction(nameof(GetPatterns), new { app }, response); } /// /// Update an app pattern. /// /// The name of the app. /// The id of the pattern to be updated. /// Pattern to be updated for the app. /// /// 200 => Pattern updated. /// 400 => Pattern request not valid. /// 404 => Pattern or app not found. /// [HttpPut] [Route("apps/{app}/patterns/{id}/")] [ProducesResponseType(typeof(PatternsDto), 200)] [ApiPermissionOrAnonymous(Permissions.AppPatternsUpdate)] [ApiCosts(1)] public async Task PutPattern(string app, Guid id, [FromBody] UpdatePatternDto request) { var command = request.ToUpdateCommand(id); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Delete an app pattern. /// /// The name of the app. /// The id of the pattern to be deleted. /// /// 200 => Pattern removed. /// 404 => Pattern or app not found. /// /// /// Schemas using this pattern will still function using the same Regular Expression. /// [HttpDelete] [Route("apps/{app}/patterns/{id}/")] [ProducesResponseType(typeof(PatternsDto), 200)] [ApiPermissionOrAnonymous(Permissions.AppPatternsDelete)] [ApiCosts(1)] public async Task DeletePattern(string app, Guid id) { var command = new DeletePattern { PatternId = 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 = GetResponse(result); return response; } private PatternsDto GetResponse(IAppEntity result) { return PatternsDto.FromApp(result, Resources); } } }