// ========================================================================== // SchemasController.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using NSwag.Annotations; using Squidex.Infrastructure.CQRS.Commands; using Squidex.Infrastructure.Reflection; using Squidex.Controllers.Api.Schemas.Models; using Squidex.Controllers.Api.Schemas.Models.Converters; using Squidex.Core.Schemas; using Squidex.Pipeline; using Squidex.Read.Schemas.Repositories; using Squidex.Write.Schemas.Commands; namespace Squidex.Controllers.Api.Schemas { /// /// Manages and retrieves information about schemas. /// [ApiExceptionFilter] [AppApi] [SwaggerTag("Schemas")] public class SchemasController : ControllerBase { private readonly ISchemaRepository schemaRepository; public SchemasController(ICommandBus commandBus, ISchemaRepository schemaRepository) : base(commandBus) { this.schemaRepository = schemaRepository; } /// /// Get schemas. /// /// The name of the app to get the schemas for. /// /// 200 => Schemas returned. /// 404 => App not found. /// [MustBeAppEditor] [HttpGet] [Route("apps/{app}/schemas/")] [ProducesResponseType(typeof(SchemaDto[]), 200)] [ApiCosts(1)] public async Task GetSchemas(string app) { var schemas = await schemaRepository.QueryAllAsync(AppId); var response = schemas.Select(s => s.ToModel()).ToList(); return Ok(response); } /// /// Get a schema by name. /// /// The name of the app. /// The name of the schema to retrieve. /// /// 200 => Schema found. /// 404 => Schema or app not found. /// [MustBeAppEditor] [HttpGet] [Route("apps/{app}/schemas/{name}/")] [ProducesResponseType(typeof(SchemaDetailsDto[]), 200)] [ApiCosts(1)] public async Task GetSchema(string app, string name) { var entity = await schemaRepository.FindSchemaAsync(AppId, name); if (entity == null) { return NotFound(); } var response = entity.ToDetailsModel(); Response.Headers["ETag"] = new StringValues(entity.Version.ToString()); return Ok(response); } /// /// Create a new schema. /// /// The name of the app. /// The schema object that needs to be added to the app. /// /// 201 => Schema created. /// 400 => Schema name or properties are not valid. /// 409 => Schema name already in use. /// [MustBeAppDeveloper] [HttpPost] [Route("apps/{app}/schemas/")] [ProducesResponseType(typeof(EntityCreatedDto), 201)] [ProducesResponseType(typeof(ErrorDto), 400)] [ProducesResponseType(typeof(ErrorDto), 409)] [ApiCosts(1)] public async Task PostSchema(string app, [FromBody] CreateSchemaDto request) { var command = request.ToCommand(); await CommandBus.PublishAsync(command); return CreatedAtAction(nameof(GetSchema), new { name = request.Name }, new EntityCreatedDto { Id = command.Name }); } /// /// Update a schema. /// /// The name of the app. /// The name of the schema. /// The schema object that needs to updated. /// /// 204 => Schema has been updated. /// 400 => Schema properties are not valid. /// 404 => Schema or app not found. /// [MustBeAppDeveloper] [HttpPut] [Route("apps/{app}/schemas/{name}/")] [ApiCosts(1)] public async Task PutSchema(string app, string name, [FromBody] UpdateSchemaDto request) { var properties = SimpleMapper.Map(request, new SchemaProperties()); await CommandBus.PublishAsync(new UpdateSchema { Properties = properties }); return NoContent(); } /// /// Publish a schema. /// /// The name of the app. /// The name of the schema to publish. /// /// 204 => Schema has been published. /// 400 => Schema is already published. /// 404 => Schema or app not found. /// [MustBeAppDeveloper] [HttpPut] [Route("apps/{app}/schemas/{name}/publish")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task PublishSchema(string app, string name) { await CommandBus.PublishAsync(new PublishSchema()); return NoContent(); } /// /// Unpublish a schema. /// /// The name of the app. /// The name of the schema to unpublish. /// /// 204 => Schema has been unpublished. /// 400 => Schema is not published. /// 404 => Schema or app not found. /// [MustBeAppDeveloper] [HttpPut] [Route("apps/{app}/schemas/{name}/unpublish")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task UnpublishSchema(string app, string name) { await CommandBus.PublishAsync(new UnpublishSchema()); return NoContent(); } /// /// Delete a schema. /// /// The name of the app. /// The name of the schema to delete. /// /// 204 => Schema has been deleted. /// 404 => Schema or app not found. /// [MustBeAppDeveloper] [HttpDelete] [Route("apps/{app}/schemas/{name}/")] [ApiCosts(1)] public async Task DeleteSchema(string app, string name) { await CommandBus.PublishAsync(new DeleteSchema()); return NoContent(); } } }