// ========================================================================== // SchemasController.cs // PinkParrot Headless CMS // ========================================================================== // Copyright (c) PinkParrot Group // All rights reserved. // ========================================================================== using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using PinkParrot.Core.Schemas; using PinkParrot.Infrastructure.CQRS.Commands; using PinkParrot.Infrastructure.Reflection; using PinkParrot.Modules.Api.Schemas.Models; using PinkParrot.Pipeline; using PinkParrot.Read.Models; using PinkParrot.Read.Repositories; using PinkParrot.Write.Schemas.Commands; namespace PinkParrot.Modules.Api.Schemas { [ApiExceptionFilter] public class SchemasController : ControllerBase { private readonly ISchemaRepository schemaRepository; public SchemasController(ICommandBus commandBus, ISchemaRepository schemaRepository) : base(commandBus) { this.schemaRepository = schemaRepository; } [HttpGet] [Route("api/schemas/")] public async Task> Query() { var schemas = await schemaRepository.QueryAllAsync(TenantId); return schemas.Select(s => SimpleMapper.Map(s, new ListSchemaDto())).ToList(); } [HttpGet] [Route("api/schemas/{name}/")] public async Task Get(string name) { var entity = await schemaRepository.FindSchemaAsync(TenantId, name); if (entity == null) { return NotFound(); } return Ok(SchemaDto.Create(entity.Schema)); } [HttpPost] [Route("api/schemas/")] public async Task Create([FromBody] CreateSchemaDto model) { var command = SimpleMapper.Map(model, new CreateSchema { AggregateId = Guid.NewGuid() }); command.Properties = command.Properties ?? new SchemaProperties(null, null); await CommandBus.PublishAsync(command); return CreatedAtAction("Query", new EntityCreatedDto { Id = command.AggregateId }); } [HttpPut] [Route("api/schemas/{name}/")] public async Task Update(string name, [FromBody] UpdateSchemaDto model) { var command = SimpleMapper.Map(model, new UpdateSchema()); await CommandBus.PublishAsync(command); return NoContent(); } [HttpDelete] [Route("api/schemas/{name}/")] public async Task Delete(string name) { await CommandBus.PublishAsync(new DeleteSchema()); return NoContent(); } } }