// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Linq; 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.Commands; using Squidex.Infrastructure.Commands; using Squidex.Pipeline; using Squidex.Shared; 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(AppPatternDto[]), 200)] [ApiPermission(Permissions.AppPatternsRead)] [ApiCosts(0)] public IActionResult GetPatterns(string app) { var response = App.Patterns.Select(AppPatternDto.FromKvp).OrderBy(x => x.Name).ToArray(); Response.Headers[HeaderNames.ETag] = App.Version.ToString(); 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(AppPatternDto), 201)] [ApiPermission(Permissions.AppPatternsCreate)] [ApiCosts(1)] public async Task PostPattern(string app, [FromBody] UpdatePatternDto request) { var command = request.ToAddCommand(); await CommandBus.PublishAsync(command); var response = AppPatternDto.FromCommand(command); return CreatedAtAction(nameof(GetPatterns), new { app }, response); } /// /// Update an existing app pattern. /// /// The name of the app. /// The id of the pattern to be updated. /// Pattern to be updated for the app. /// /// 204 => Pattern updated. /// 400 => Pattern request not valid. /// 404 => Pattern or app not found. /// [HttpPut] [Route("apps/{app}/patterns/{id}/")] [ProducesResponseType(typeof(AppPatternDto), 201)] [ApiPermission(Permissions.AppPatternsUpdate)] [ApiCosts(1)] public async Task UpdatePattern(string app, Guid id, [FromBody] UpdatePatternDto request) { await CommandBus.PublishAsync(request.ToUpdateCommand(id)); return NoContent(); } /// /// Delete an existing app pattern. /// /// The name of the app. /// The id of the pattern to be deleted. /// /// 204 => 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}/")] [ApiPermission(Permissions.AppPatternsDelete)] [ApiCosts(1)] public async Task DeletePattern(string app, Guid id) { await CommandBus.PublishAsync(new DeletePattern { PatternId = id }); return NoContent(); } } }