// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; using Squidex.Areas.Api.Controllers.Schemas.Models; using Squidex.Domain.Apps.Entities; using Squidex.Domain.Apps.Entities.Schemas; using Squidex.Domain.Apps.Entities.Schemas.Commands; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Shared; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Schemas { /// /// Manages and retrieves information about schemas. /// [ApiExplorerSettings(GroupName = nameof(Schemas))] public sealed class SchemasController : ApiController { private readonly IAppProvider appProvider; public SchemasController(ICommandBus commandBus, IAppProvider appProvider) : base(commandBus) { this.appProvider = appProvider; } /// /// Get schemas. /// /// The name of the app. /// /// 200 => Schemas returned. /// 404 => App not found. /// [HttpGet] [Route("apps/{app}/schemas/")] [ProducesResponseType(typeof(SchemasDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasRead)] [ApiCosts(0)] public async Task GetSchemas(string app) { var schemas = await appProvider.GetSchemasAsync(AppId); var response = Deferred.Response(() => { return SchemasDto.FromSchemas(schemas, Resources); }); Response.Headers[HeaderNames.ETag] = schemas.ToEtag(); 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. /// [HttpGet] [Route("apps/{app}/schemas/{name}/")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasRead)] [ApiCosts(0)] public async Task GetSchema(string app, string name) { ISchemaEntity? schema; if (Guid.TryParse(name, out var guid)) { var schemaId = DomainId.Create(guid); schema = await appProvider.GetSchemaAsync(AppId, schemaId, false); } else { schema = await appProvider.GetSchemaAsync(AppId, name); } if (schema == null || schema.IsDeleted) { return NotFound(); } var response = Deferred.Response(() => { return SchemaDetailsDto.FromSchemaWithDetails(schema, Resources); }); Response.Headers[HeaderNames.ETag] = schema.ToEtag(); 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 request not valid. /// 409 => Schema name already in use. /// [HttpPost] [Route("apps/{app}/schemas/")] [ProducesResponseType(typeof(SchemaDetailsDto), 201)] [ApiPermissionOrAnonymous(Permissions.AppSchemasCreate)] [ApiCosts(1)] public async Task PostSchema(string app, [FromBody] CreateSchemaDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return CreatedAtAction(nameof(GetSchema), new { app, name = request.Name }, response); } /// /// Update a schema. /// /// The name of the app. /// The name of the schema. /// The schema object that needs to updated. /// /// 200 => Schema updated. /// 400 => Schema request not valid. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasUpdate)] [ApiCosts(1)] public async Task PutSchema(string app, string name, [FromBody] UpdateSchemaDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Synchronize a schema. /// /// The name of the app. /// The name of the schema. /// The schema object that needs to updated. /// /// 200 => Schema updated. /// 400 => Schema request not valid. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/sync")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasUpdate)] [ApiCosts(1)] public async Task PutSchemaSync(string app, string name, [FromBody] SynchronizeSchemaDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Update a schema category. /// /// The name of the app. /// The name of the schema. /// The schema object that needs to updated. /// /// 200 => Schema updated. /// 400 => Schema request not valid. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/category")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasUpdate)] [ApiCosts(1)] public async Task PutCategory(string app, string name, [FromBody] ChangeCategoryDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Update the preview urls. /// /// The name of the app. /// The name of the schema. /// The preview urls for the schema. /// /// 200 => Schema updated. /// 400 => Schema request not valid. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/preview-urls")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasUpdate)] [ApiCosts(1)] public async Task PutPreviewUrls(string app, string name, [FromBody] ConfigurePreviewUrlsDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Update the scripts. /// /// The name of the app. /// The name of the schema. /// The schema scripts object that needs to updated. /// /// 200 => Schema updated. /// 400 => Schema request not valid. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/scripts/")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasScripts)] [ApiCosts(1)] public async Task PutScripts(string app, string name, [FromBody] SchemaScriptsDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Update the rules. /// /// The name of the app. /// The name of the schema. /// The schema rules object that needs to updated. /// /// 200 => Schema updated. /// 400 => Schema request not valid. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/rules/")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasUpdate)] [ApiCosts(1)] public async Task PutRules(string app, string name, [FromBody] ConfigureFieldRulesDto request) { var command = request.ToCommand(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Publish a schema. /// /// The name of the app. /// The name of the schema to publish. /// /// 200 => Schema published. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/publish/")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasPublish)] [ApiCosts(1)] public async Task PublishSchema(string app, string name) { var command = new PublishSchema(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Unpublish a schema. /// /// The name of the app. /// The name of the schema to unpublish. /// /// 200 => Schema unpublished. /// 404 => Schema or app not found. /// [HttpPut] [Route("apps/{app}/schemas/{name}/unpublish/")] [ProducesResponseType(typeof(SchemaDetailsDto), StatusCodes.Status200OK)] [ApiPermissionOrAnonymous(Permissions.AppSchemasPublish)] [ApiCosts(1)] public async Task UnpublishSchema(string app, string name) { var command = new UnpublishSchema(); var response = await InvokeCommandAsync(command); return Ok(response); } /// /// Delete a schema. /// /// The name of the app. /// The name of the schema to delete. /// /// 204 => Schema deleted. /// 404 => Schema or app not found. /// [HttpDelete] [Route("apps/{app}/schemas/{name}/")] [ApiPermissionOrAnonymous(Permissions.AppSchemasDelete)] [ApiCosts(1)] public async Task DeleteSchema(string app, string name) { await CommandBus.PublishAsync(new DeleteSchema()); return NoContent(); } private async Task InvokeCommandAsync(ICommand command) { var context = await CommandBus.PublishAsync(command); var result = context.Result(); var response = SchemaDetailsDto.FromSchemaWithDetails(result, Resources); return response; } } }