// ========================================================================== // AppPatternsController.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using NSwag.Annotations; using Squidex.Areas.Api.Controllers.Apps.Models; using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Reflection; using Squidex.Pipeline; namespace Squidex.Areas.Api.Controllers.Apps { /// /// Manages and configures app patterns. /// [ApiAuthorize] [MustBeAppDeveloper] [ApiExceptionFilter] [AppApi] [SwaggerTag(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)] [ApiCosts(0)] public IActionResult GetPatterns(string app) { var response = App.Patterns.Select(x => SimpleMapper.Map(x.Value, new AppPatternDto { PatternId = x.Key })) .OrderBy(x => x.Name).ToList(); return Ok(response); } /// /// Create a new app patterm. /// /// The name of the app. /// Pattern to be added to the app. /// /// 201 => Pattern generated. /// 404 => App not found. /// [HttpPost] [Route("apps/{app}/patterns/")] [ProducesResponseType(typeof(AppPatternDto), 201)] [ApiCosts(1)] public async Task PostPattern(string app, [FromBody] UpdatePatternDto request) { var command = SimpleMapper.Map(request, new AddPattern()); await CommandBus.PublishAsync(command); var response = SimpleMapper.Map(request, new AppPatternDto { PatternId = command.PatternId }); return CreatedAtAction(nameof(GetPatterns), new { app }, request); } /// /// Update an existing app patterm. /// /// The name of the app. /// The id of the pattern to be updated. /// Pattern to be updated for the app. /// /// 204 => Pattern updated. /// 404 => App not found or pattern not found. /// [HttpPut] [Route("apps/{app}/patterns/{id}/")] [ProducesResponseType(typeof(AppPatternDto), 201)] [ApiCosts(1)] public async Task UpdatePattern(string app, Guid id, [FromBody] UpdatePatternDto request) { var command = SimpleMapper.Map(request, new UpdatePattern { PatternId = id }); await CommandBus.PublishAsync(command); return NoContent(); } /// /// Revoke an app client /// /// The name of the app. /// The id of the pattern to be deleted. /// /// 204 => Pattern removed. /// 404 => App or pattern not found. /// /// /// Schemas using this pattern will still function using the same Regular Expression /// [HttpDelete] [Route("apps/{app}/patterns/{id}/")] [ApiCosts(1)] public async Task DeletePattern(string app, Guid id) { await CommandBus.PublishAsync(new DeletePattern { PatternId = id }); return NoContent(); } } }