// ========================================================================== // SchemaFieldsController.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using NSwag.Annotations; using Squidex.Areas.Api.Controllers.Schemas.Models; using Squidex.Domain.Apps.Write.Schemas.Commands; using Squidex.Infrastructure.CQRS.Commands; using Squidex.Pipeline; namespace Squidex.Areas.Api.Controllers.Schemas { /// /// Manages and retrieves information about schemas. /// [ApiAuthorize] [ApiExceptionFilter] [AppApi] [MustBeAppDeveloper] [SwaggerTag(nameof(Schemas))] public sealed class SchemaFieldsController : ApiController { public SchemaFieldsController(ICommandBus commandBus) : base(commandBus) { } /// /// Add a schema field. /// /// The name of the app. /// The name of the schema. /// The field object that needs to be added to the schema. /// /// 201 => Schema field created. /// 400 => Schema field properties not valid. /// 404 => Schema or app not found. /// 409 => Schema field name already in use. /// [HttpPost] [Route("apps/{app}/schemas/{name}/fields/")] [ProducesResponseType(typeof(EntityCreatedDto), 201)] [ProducesResponseType(typeof(ErrorDto), 409)] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task PostField(string app, string name, [FromBody] AddFieldDto request) { var command = new AddField { Name = request.Name, Partitioning = request.Partitioning, Properties = request.Properties.ToProperties() }; var context = await CommandBus.PublishAsync(command); var result = context.Result>(); var response = new EntityCreatedDto { Id = result.IdOrValue.ToString(), Version = result.Version }; return StatusCode(201, response); } /// /// Reorders the fields. /// /// The name of the app. /// The name of the schema. /// The request that contains the field ids. /// /// 204 => Schema fields reorderd. /// 400 => Schema field ids do not cover the fields of the schema. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/fields/ordering/")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task PutFieldOrdering(string app, string name, [FromBody] ReorderFields request) { var command = new ReorderFields { FieldIds = request.FieldIds }; await CommandBus.PublishAsync(command); return NoContent(); } /// /// Update a schema field. /// /// The name of the app. /// The name of the schema. /// The id of the field to update. /// The field object that needs to be added to the schema. /// /// 204 => Schema field updated. /// 400 => Schema field properties not valid or field is locked. /// 404 => Schema, field or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/fields/{id:long}/")] [ProducesResponseType(typeof(ErrorDto), 409)] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task PutField(string app, string name, long id, [FromBody] UpdateFieldDto request) { var command = new UpdateField { FieldId = id, Properties = request.Properties.ToProperties() }; await CommandBus.PublishAsync(command); return NoContent(); } /// /// Lock a schema field. /// /// The name of the app. /// The name of the schema. /// The id of the field to lock. /// /// 204 => Schema field shown. /// 400 => Schema field already locked. /// 404 => Schema, field or app not found. /// /// /// A hidden field is not part of the API response, but can still be edited in the portal. /// [HttpPut] [Route("apps/{app}/schemas/{name}/fields/{id:long}/lock/")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task LockField(string app, string name, long id) { var command = new LockField { FieldId = id }; await CommandBus.PublishAsync(command); return NoContent(); } /// /// Hide a schema field. /// /// The name of the app. /// The name of the schema. /// The id of the field to hide. /// /// 204 => Schema field hidden. /// 400 => Schema field already hidden. /// 404 => Schema, field or app not found. /// /// /// A locked field cannot be edited or deleted. /// [HttpPut] [Route("apps/{app}/schemas/{name}/fields/{id:long}/hide/")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task HideField(string app, string name, long id) { var command = new HideField { FieldId = id }; await CommandBus.PublishAsync(command); return NoContent(); } /// /// Show a schema field. /// /// The name of the app. /// The name of the schema. /// The id of the field to show. /// /// 204 => Schema field shown. /// 400 => Schema field already visible. /// 404 => Schema, field or app not found. /// /// /// A hidden field is not part of the API response, but can still be edited in the portal. /// [HttpPut] [Route("apps/{app}/schemas/{name}/fields/{id:long}/show/")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task ShowField(string app, string name, long id) { var command = new ShowField { FieldId = id }; await CommandBus.PublishAsync(command); return NoContent(); } /// /// Enable a schema field. /// /// The name of the app. /// The name of the schema. /// The id of the field to enable. /// /// 204 => Schema field enabled. /// 400 => Schema field already enabled. /// 404 => Schema, field or app not found. /// /// /// A disabled field cannot not be edited in the squidex portal anymore, /// but will be part of the API response. /// [HttpPut] [Route("apps/{app}/schemas/{name}/fields/{id:long}/enable/")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task EnableField(string app, string name, long id) { var command = new EnableField { FieldId = id }; await CommandBus.PublishAsync(command); return NoContent(); } /// /// Disable a schema field. /// /// The name of the app. /// The name of the schema. /// The id of the field to disable. /// /// 204 => Schema field disabled. /// 400 => Schema field already disabled. /// 404 => Schema, field or app not found. /// /// /// A disabled field cannot not be edited in the squidex portal anymore, /// but will be part of the API response. /// [HttpPut] [Route("apps/{app}/schemas/{name}/fields/{id:long}/disable/")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(1)] public async Task DisableField(string app, string name, long id) { var command = new DisableField { FieldId = id }; await CommandBus.PublishAsync(command); return NoContent(); } /// /// Delete a schema field. /// /// The name of the app. /// The name of the schema. /// The id of the field to disable. /// /// 204 => Schema field deleted. /// 400 => Field is locked. /// 404 => Schema, field or app not found. /// [HttpDelete] [Route("apps/{app}/schemas/{name}/fields/{id:long}/")] [ApiCosts(1)] public async Task DeleteField(string app, string name, long id) { var command = new DeleteField { FieldId = id }; await CommandBus.PublishAsync(command); return NoContent(); } } }