// ========================================================================== // SchemasController.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; 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.Identity; 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. /// [Authorize(Roles = SquidexRoles.AppDeveloper)] [ApiExceptionFilter] [ServiceFilter(typeof(AppFilterAttribute))] [SwaggerTag("Schemas")] public class SchemasController : ControllerBase { private readonly ISchemaRepository schemaRepository; public SchemasController(ICommandBus commandBus, ISchemaRepository schemaRepository) : base(commandBus) { this.schemaRepository = schemaRepository; } /// /// Get app schemas. /// /// /// 200 => Schemas returned. /// [HttpGet] [Route("apps/{app}/schemas/")] [ProducesResponseType(typeof(SchemaDto[]), 200)] public async Task GetSchemas() { var schemas = await schemaRepository.QueryAllAsync(AppId); var model = schemas.Select(s => SimpleMapper.Map(s, new SchemaDto())).ToList(); return Ok(model); } /// /// Get a schema by name. /// /// The name of the schema to retrieve. /// /// 200 => Schema found. /// 404 => Schema not found. /// [HttpGet] [Route("apps/{app}/schemas/{name}/")] [ProducesResponseType(typeof(SchemaDetailsDto[]), 200)] public async Task GetSchema(string name) { var entity = await schemaRepository.FindSchemaAsync(AppId, name); if (entity == null) { return NotFound(); } var model = entity.ToModel(); return Ok(model); } /// /// Create a new schema. /// /// The schema object that needs to be added to the app. /// The name of the app to create the schema to. /// /// 201 => Schema created. /// 400 => Schema name is not valid. /// 409 => Schema name already in use. /// [HttpPost] [Route("apps/{app}/schemas/")] [ProducesResponseType(typeof(EntityCreatedDto), 201)] [ProducesResponseType(typeof(ErrorDto), 400)] [ProducesResponseType(typeof(ErrorDto), 409)] public async Task PostSchema(string app, [FromBody] CreateSchemaDto request) { var command = SimpleMapper.Map(request, new CreateSchema()); await CommandBus.PublishAsync(command); return CreatedAtAction(nameof(GetSchema), new { name = request.Name }, new EntityCreatedDto { Id = command.Name }); } /// /// Update a schema. /// /// The app where the schema is a part of. /// The name of the schema. /// The schema object that needs to updated. /// /// 204 => Schema updated. /// [HttpPut] [Route("apps/{app}/schemas/{name}/")] 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 app where the schema is a part of. /// The name of the schema to publish. /// /// 400 => Schema is already published. /// 204 => Schema has been deleted. /// [HttpPut] [Route("apps/{app}/schemas/{name}/publish")] [ProducesResponseType(typeof(ErrorDto), 400)] public async Task PublishSchema(string app, string name) { await CommandBus.PublishAsync(new PublishSchema()); return NoContent(); } /// /// Unpublish a schema. /// /// The app where the schema is a part of. /// The name of the schema to unpublish. /// /// 400 => Schema is not published. /// 204 => Schema has been deleted. /// [HttpPut] [Route("apps/{app}/schemas/{name}/unpublish")] [ProducesResponseType(typeof(ErrorDto), 400)] public async Task UnpublishSchema(string app, string name) { await CommandBus.PublishAsync(new UnpublishSchema()); return NoContent(); } /// /// Delete a schema. /// /// The app where the schema is a part of. /// The name of the schema to delete. /// /// 204 => Schema has been deleted. /// [HttpDelete] [Route("apps/{app}/schemas/{name}/")] public async Task DeleteSchema(string app, string name) { await CommandBus.PublishAsync(new DeleteSchema()); return NoContent(); } } }