Browse Source

Schemas cleaned.

pull/282/head
Sebastian Stehle 9 years ago
parent
commit
2af7fd69b2
  1. 7
      src/Squidex/Areas/Api/Controllers/Schemas/Models/AddFieldDto.cs
  2. 8
      src/Squidex/Areas/Api/Controllers/Schemas/Models/ConfigureScriptsDto.cs
  3. 85
      src/Squidex/Areas/Api/Controllers/Schemas/Models/Converters/SchemaConverter.cs
  4. 32
      src/Squidex/Areas/Api/Controllers/Schemas/Models/CreateSchemaDto.cs
  5. 6
      src/Squidex/Areas/Api/Controllers/Schemas/Models/ReorderFieldsDto.cs
  6. 29
      src/Squidex/Areas/Api/Controllers/Schemas/Models/SchemaDetailsDto.cs
  7. 13
      src/Squidex/Areas/Api/Controllers/Schemas/Models/SchemaDto.cs
  8. 6
      src/Squidex/Areas/Api/Controllers/Schemas/Models/UpdateFieldDto.cs
  9. 10
      src/Squidex/Areas/Api/Controllers/Schemas/Models/UpdateSchemaDto.cs
  10. 40
      src/Squidex/Areas/Api/Controllers/Schemas/SchemaFieldsController.cs
  11. 15
      src/Squidex/Areas/Api/Controllers/Schemas/SchemasController.cs
  12. 6
      src/Squidex/Pipeline/EntityCreatedDto.cs

7
src/Squidex/Areas/Api/Controllers/Schemas/Models/AddFieldDto.cs

@ -6,6 +6,8 @@
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
@ -28,5 +30,10 @@ namespace Squidex.Areas.Api.Controllers.Schemas.Models
/// </summary>
[Required]
public FieldPropertiesDto Properties { get; set; }
public AddField ToCommand()
{
return SimpleMapper.Map(this, new AddField { Properties = this.Properties.ToProperties() });
}
}
}

8
src/Squidex/Areas/Api/Controllers/Schemas/Models/ConfigureScriptsDto.cs

@ -5,6 +5,9 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Squidex.Domain.Apps.Entities.Schemas.Commands;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
public sealed class ConfigureScriptsDto
@ -33,5 +36,10 @@ namespace Squidex.Areas.Api.Controllers.Schemas.Models
/// The script that is executed when change a content status.
/// </summary>
public string ScriptChange { get; set; }
public ConfigureScripts ToCommand()
{
return SimpleMapper.Map(this, new ConfigureScripts());
}
}
}

85
src/Squidex/Areas/Api/Controllers/Schemas/Models/Converters/SchemaConverter.cs

@ -1,85 +0,0 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Entities.Schemas;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Schemas.Models.Converters
{
public static class SchemaConverter
{
public static SchemaDto ToModel(this ISchemaEntity entity)
{
var dto = new SchemaDto { Properties = new SchemaPropertiesDto() };
SimpleMapper.Map(entity, dto);
SimpleMapper.Map(entity.SchemaDef, dto);
SimpleMapper.Map(entity.SchemaDef.Properties, dto.Properties);
return dto;
}
public static SchemaDetailsDto ToDetailsModel(this ISchemaEntity entity)
{
var dto = new SchemaDetailsDto { Properties = new SchemaPropertiesDto() };
SimpleMapper.Map(entity, dto);
SimpleMapper.Map(entity.SchemaDef, dto);
SimpleMapper.Map(entity.SchemaDef.Properties, dto.Properties);
dto.Fields = new List<FieldDto>();
foreach (var field in entity.SchemaDef.Fields)
{
var fieldPropertiesDto = FieldPropertiesDtoFactory.Create(field.RawProperties);
var fieldInstanceDto = SimpleMapper.Map(field,
new FieldDto
{
FieldId = field.Id,
Properties = fieldPropertiesDto,
Partitioning = field.Partitioning.Key
});
dto.Fields.Add(fieldInstanceDto);
}
return dto;
}
public static CreateSchema ToCommand(this CreateSchemaDto dto)
{
var command = new CreateSchema();
SimpleMapper.Map(dto, command);
if (dto.Properties != null)
{
command.Properties = new SchemaProperties();
SimpleMapper.Map(dto.Properties, command.Properties);
}
if (dto.Fields != null)
{
command.Fields = new List<CreateSchemaField>();
foreach (var fieldDto in dto.Fields)
{
var fieldProperties = fieldDto?.Properties.ToProperties();
var fieldInstance = SimpleMapper.Map(fieldDto, new CreateSchemaField { Properties = fieldProperties });
command.Fields.Add(fieldInstance);
}
}
return command;
}
}
}

32
src/Squidex/Areas/Api/Controllers/Schemas/Models/CreateSchemaDto.cs

@ -7,6 +7,9 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
@ -33,5 +36,34 @@ namespace Squidex.Areas.Api.Controllers.Schemas.Models
/// Set it to true to autopublish the schema.
/// </summary>
public bool Publish { get; set; }
public CreateSchema ToCommand()
{
var command = new CreateSchema();
SimpleMapper.Map(this, command);
if (Properties != null)
{
command.Properties = new SchemaProperties();
SimpleMapper.Map(Properties, command.Properties);
}
if (Fields != null)
{
command.Fields = new List<CreateSchemaField>();
foreach (var fieldDto in Fields)
{
var fieldProperties = fieldDto?.Properties.ToProperties();
var fieldInstance = SimpleMapper.Map(fieldDto, new CreateSchemaField { Properties = fieldProperties });
command.Fields.Add(fieldInstance);
}
}
return command;
}
}
}

6
src/Squidex/Areas/Api/Controllers/Schemas/Models/ReorderFieldsDto.cs

@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
@ -17,5 +18,10 @@ namespace Squidex.Areas.Api.Controllers.Schemas.Models
/// </summary>
[Required]
public List<long> FieldIds { get; set; }
public ReorderFields ToCommand()
{
return new ReorderFields { FieldIds = FieldIds };
}
}
}

29
src/Squidex/Areas/Api/Controllers/Schemas/Models/SchemaDetailsDto.cs

@ -9,7 +9,9 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using NodaTime;
using Squidex.Domain.Apps.Entities.Schemas;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
@ -95,5 +97,32 @@ namespace Squidex.Areas.Api.Controllers.Schemas.Models
/// The version of the schema.
/// </summary>
public int Version { get; set; }
public static SchemaDetailsDto FromSchema(ISchemaEntity schema)
{
var response = new SchemaDetailsDto { Properties = new SchemaPropertiesDto() };
SimpleMapper.Map(schema, response);
SimpleMapper.Map(schema.SchemaDef, response);
SimpleMapper.Map(schema.SchemaDef.Properties, response.Properties);
response.Fields = new List<FieldDto>();
foreach (var field in schema.SchemaDef.Fields)
{
var fieldPropertiesDto = FieldPropertiesDtoFactory.Create(field.RawProperties);
var fieldInstanceDto = SimpleMapper.Map(field,
new FieldDto
{
FieldId = field.Id,
Properties = fieldPropertiesDto,
Partitioning = field.Partitioning.Key
});
response.Fields.Add(fieldInstanceDto);
}
return response;
}
}
}

13
src/Squidex/Areas/Api/Controllers/Schemas/Models/SchemaDto.cs

@ -8,7 +8,9 @@
using System;
using System.ComponentModel.DataAnnotations;
using NodaTime;
using Squidex.Domain.Apps.Entities.Schemas;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
@ -63,5 +65,16 @@ namespace Squidex.Areas.Api.Controllers.Schemas.Models
/// The version of the schema.
/// </summary>
public int Version { get; set; }
public static SchemaDto FromSchema(ISchemaEntity schema)
{
var response = new SchemaDto { Properties = new SchemaPropertiesDto() };
SimpleMapper.Map(schema, response);
SimpleMapper.Map(schema.SchemaDef, response);
SimpleMapper.Map(schema.SchemaDef.Properties, response.Properties);
return response;
}
}
}

6
src/Squidex/Areas/Api/Controllers/Schemas/Models/UpdateFieldDto.cs

@ -6,6 +6,7 @@
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
@ -16,5 +17,10 @@ namespace Squidex.Areas.Api.Controllers.Schemas.Models
/// </summary>
[Required]
public FieldPropertiesDto Properties { get; set; }
public UpdateField ToCommand(long id)
{
return new UpdateField { FieldId = id, Properties = Properties?.ToProperties() };
}
}
}

10
src/Squidex/Areas/Api/Controllers/Schemas/Models/UpdateSchemaDto.cs

@ -6,6 +6,9 @@
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
@ -22,5 +25,12 @@ namespace Squidex.Areas.Api.Controllers.Schemas.Models
/// </summary>
[StringLength(1000)]
public string Hints { get; set; }
public UpdateSchema ToCommand()
{
var properties = SimpleMapper.Map(this, new SchemaProperties());
return new UpdateSchema { Properties = properties };
}
}
}

40
src/Squidex/Areas/Api/Controllers/Schemas/SchemaFieldsController.cs

@ -11,7 +11,6 @@ using NSwag.Annotations;
using Squidex.Areas.Api.Controllers.Schemas.Models;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.Reflection;
using Squidex.Pipeline;
namespace Squidex.Areas.Api.Controllers.Schemas
@ -51,11 +50,10 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> PostField(string app, string name, [FromBody] AddFieldDto request)
{
var command = SimpleMapper.Map(request, new AddField { Properties = request.Properties.ToProperties() });
var context = await CommandBus.PublishAsync(command);
var context = await CommandBus.PublishAsync(request.ToCommand());
var result = context.Result<EntityCreatedResult<long>>();
var response = new EntityCreatedDto { Id = result.IdOrValue.ToString(), Version = result.Version };
var response = EntityCreatedDto.FromResult(result);
return StatusCode(201, response);
}
@ -75,11 +73,9 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[Route("apps/{app}/schemas/{name}/fields/ordering/")]
[ProducesResponseType(typeof(ErrorDto), 400)]
[ApiCosts(1)]
public async Task<IActionResult> PutFieldOrdering(string app, string name, [FromBody] ReorderFields request)
public async Task<IActionResult> PutFieldOrdering(string app, string name, [FromBody] ReorderFieldsDto request)
{
var command = new ReorderFields { FieldIds = request.FieldIds };
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(request.ToCommand());
return NoContent();
}
@ -103,9 +99,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> PutField(string app, string name, long id, [FromBody] UpdateFieldDto request)
{
var command = new UpdateField { FieldId = id, Properties = request.Properties.ToProperties() };
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(request.ToCommand(id));
return NoContent();
}
@ -130,9 +124,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> LockField(string app, string name, long id)
{
var command = new LockField { FieldId = id };
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(new LockField { FieldId = id });
return NoContent();
}
@ -157,9 +149,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> HideField(string app, string name, long id)
{
var command = new HideField { FieldId = id };
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(new HideField { FieldId = id });
return NoContent();
}
@ -184,9 +174,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> ShowField(string app, string name, long id)
{
var command = new ShowField { FieldId = id };
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(new ShowField { FieldId = id });
return NoContent();
}
@ -212,9 +200,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> EnableField(string app, string name, long id)
{
var command = new EnableField { FieldId = id };
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(new EnableField { FieldId = id });
return NoContent();
}
@ -240,9 +226,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> DisableField(string app, string name, long id)
{
var command = new DisableField { FieldId = id };
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(new DisableField { FieldId = id });
return NoContent();
}
@ -263,9 +247,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> DeleteField(string app, string name, long id)
{
var command = new DeleteField { FieldId = id };
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(new DeleteField { FieldId = id });
return NoContent();
}

15
src/Squidex/Areas/Api/Controllers/Schemas/SchemasController.cs

@ -11,13 +11,10 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using Squidex.Areas.Api.Controllers.Schemas.Models;
using Squidex.Areas.Api.Controllers.Schemas.Models.Converters;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Entities;
using Squidex.Domain.Apps.Entities.Schemas;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.Reflection;
using Squidex.Pipeline;
namespace Squidex.Areas.Api.Controllers.Schemas
@ -56,7 +53,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
{
var schemas = await appProvider.GetSchemasAsync(AppId);
var response = schemas.Select(s => s.ToModel()).ToList();
var response = schemas.Select(SchemaDto.FromSchema).ToList();
return Ok(response);
}
@ -93,7 +90,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
return NotFound();
}
var response = entity.ToDetailsModel();
var response = SchemaDetailsDto.FromSchema(entity);
Response.Headers["ETag"] = entity.Version.ToString();
@ -145,9 +142,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> PutSchema(string app, string name, [FromBody] UpdateSchemaDto request)
{
var properties = SimpleMapper.Map(request, new SchemaProperties());
await CommandBus.PublishAsync(new UpdateSchema { Properties = properties });
await CommandBus.PublishAsync(request.ToCommand());
return NoContent();
}
@ -169,9 +164,7 @@ namespace Squidex.Areas.Api.Controllers.Schemas
[ApiCosts(1)]
public async Task<IActionResult> PutSchemaScripts(string app, string name, [FromBody] ConfigureScriptsDto request)
{
var command = SimpleMapper.Map(request, new ConfigureScripts());
await CommandBus.PublishAsync(command);
await CommandBus.PublishAsync(request.ToCommand());
return NoContent();
}

6
src/Squidex/Pipeline/EntityCreatedDto.cs

@ -6,6 +6,7 @@
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using Squidex.Infrastructure.Commands;
namespace Squidex.Pipeline
{
@ -21,5 +22,10 @@ namespace Squidex.Pipeline
/// The new version of the entity.
/// </summary>
public long Version { get; set; }
public static EntityCreatedDto FromResult<T>(EntityCreatedResult<T> result)
{
return new EntityCreatedDto { Id = result.ToString(), Version = result.Version };
}
}
}

Loading…
Cancel
Save