mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
6.2 KiB
182 lines
6.2 KiB
// ==========================================================================
|
|
// SchemasController.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
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.Pipeline;
|
|
using Squidex.Read.Schemas.Repositories;
|
|
using Squidex.Write.Schemas.Commands;
|
|
|
|
namespace Squidex.Controllers.Api.Schemas
|
|
{
|
|
/// <summary>
|
|
/// Manages and retrieves information about schemas.
|
|
/// </summary>
|
|
[Authorize(Roles = "app-owner,app-developer")]
|
|
[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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get app schemas.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// 200 => Schemas returned.
|
|
/// </returns>
|
|
[HttpGet]
|
|
[Route("apps/{app}/schemas/")]
|
|
[ProducesResponseType(typeof(SchemaDto[]), 200)]
|
|
public async Task<IActionResult> GetSchemas()
|
|
{
|
|
var schemas = await schemaRepository.QueryAllAsync(AppId);
|
|
|
|
var model = schemas.Select(s => SimpleMapper.Map(s, new SchemaDto())).ToList();
|
|
|
|
return Ok(model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a schema by name.
|
|
/// </summary>
|
|
/// <param name="name">The name of the schema to retrieve.</param>
|
|
/// <returns>
|
|
/// 200 => Schema found.
|
|
/// 404 => Schema not found.
|
|
/// </returns>
|
|
[HttpGet]
|
|
[Route("apps/{app}/schemas/{name}/")]
|
|
[ProducesResponseType(typeof(SchemaDetailsDto[]), 200)]
|
|
public async Task<IActionResult> GetSchema(string name)
|
|
{
|
|
var entity = await schemaRepository.FindSchemaAsync(AppId, name);
|
|
|
|
if (entity == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var model = entity.ToModel();
|
|
|
|
return Ok(model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new schema.
|
|
/// </summary>
|
|
/// <param name="request">The schema object that needs to be added to the app.</param>
|
|
/// <param name="app">The name of the app to create the schema to.</param>
|
|
/// <returns>
|
|
/// 201 => Schema created.
|
|
/// 400 => Schema name is not valid.
|
|
/// 409 => Schema name already in use.
|
|
/// </returns>
|
|
[HttpPost]
|
|
[Route("apps/{app}/schemas/")]
|
|
[ProducesResponseType(typeof(EntityCreatedDto), 201)]
|
|
[ProducesResponseType(typeof(ErrorDto), 400)]
|
|
[ProducesResponseType(typeof(ErrorDto), 409)]
|
|
public async Task<IActionResult> PostSchema(string app, [FromBody] CreateSchemaDto request)
|
|
{
|
|
var command = SimpleMapper.Map(request, new CreateSchema { AggregateId = Guid.NewGuid() });
|
|
|
|
await CommandBus.PublishAsync(command);
|
|
|
|
return CreatedAtAction(nameof(GetSchema), new { name = request.Name }, new EntityCreatedDto { Id = command.Name });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a schema.
|
|
/// </summary>
|
|
/// <param name="app">The app where the schema is a part of.</param>
|
|
/// <param name="name">The name of the schema.</param>
|
|
/// <param name="request">The schema object that needs to updated.</param>
|
|
/// <returns>
|
|
/// 204 => Schema updated.
|
|
/// </returns>
|
|
[HttpPut]
|
|
[Route("apps/{app}/schemas/{name}/")]
|
|
public async Task<IActionResult> PutSchema(string app, string name, [FromBody] UpdateSchemaDto request)
|
|
{
|
|
var command = SimpleMapper.Map(request, new UpdateSchema());
|
|
|
|
await CommandBus.PublishAsync(command);
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Publishs a schema.
|
|
/// </summary>
|
|
/// <param name="app">The app where the schema is a part of.</param>
|
|
/// <param name="name">The name of the schema to publish.</param>
|
|
/// <returns>
|
|
/// 400 => Schema is already published.
|
|
/// 204 => Schema has been deleted.
|
|
/// </returns>
|
|
[HttpPut]
|
|
[Route("apps/{app}/schemas/{name}/publish")]
|
|
public async Task<IActionResult> PublishSchema(string app, string name)
|
|
{
|
|
await CommandBus.PublishAsync(new PublishSchema());
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpublishs a schema.
|
|
/// </summary>
|
|
/// <param name="app">The app where the schema is a part of.</param>
|
|
/// <param name="name">The name of the schema to unpublish.</param>
|
|
/// <returns>
|
|
/// 400 => Schema is not published.
|
|
/// 204 => Schema has been deleted.
|
|
/// </returns>
|
|
[HttpPut]
|
|
[Route("apps/{app}/schemas/{name}/publish")]
|
|
public async Task<IActionResult> UnpublishSchema(string app, string name)
|
|
{
|
|
await CommandBus.PublishAsync(new UnpublishSchema());
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete a schema.
|
|
/// </summary>
|
|
/// <param name="app">The app where the schema is a part of.</param>
|
|
/// <param name="name">The name of the schema to delete.</param>
|
|
/// <returns>
|
|
/// 204 => Schema has been deleted.
|
|
/// </returns>
|
|
[HttpDelete]
|
|
[Route("apps/{app}/schemas/{name}/")]
|
|
public async Task<IActionResult> DeleteSchema(string app, string name)
|
|
{
|
|
await CommandBus.PublishAsync(new DeleteSchema());
|
|
|
|
return NoContent();
|
|
}
|
|
}
|
|
}
|