Browse Source

API support and tags support.

pull/141/head
Sebastian Stehle 8 years ago
parent
commit
807301d1bb
  1. 10
      src/Squidex.Domain.Apps.Write/Schemas/Guards/FieldPropertiesValidator.cs
  2. 87
      src/Squidex/Controllers/Api/Schemas/Models/Converters/FieldPropertiesDtoFactory.cs
  3. 108
      src/Squidex/Controllers/Api/Schemas/Models/Converters/SchemaConverter.cs
  4. 1
      src/Squidex/Controllers/Api/Schemas/Models/FieldPropertiesDto.cs
  5. 33
      src/Squidex/Controllers/Api/Schemas/Models/TagsFieldPropertiesDto.cs
  6. 34
      tests/Squidex.Domain.Apps.Write.Tests/Schemas/Guards/FieldProperties/TagsFieldPropertiesTests.cs

10
src/Squidex.Domain.Apps.Write/Schemas/Guards/FieldPropertiesValidator.cs

@ -189,5 +189,15 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards
nameof(properties.MaxLength));
}
}
public IEnumerable<ValidationError> Visit(TagsFieldProperties properties)
{
if (properties.MaxItems.HasValue && properties.MinItems.HasValue && properties.MinItems.Value >= properties.MaxItems.Value)
{
yield return new ValidationError("Max items must be greater than min items.",
nameof(properties.MinItems),
nameof(properties.MaxItems));
}
}
}
}

87
src/Squidex/Controllers/Api/Schemas/Models/Converters/FieldPropertiesDtoFactory.cs

@ -0,0 +1,87 @@
// ==========================================================================
// FieldPropertiesDtoFactory.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System.Linq;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Controllers.Api.Schemas.Models.Converters
{
public class FieldPropertiesDtoFactory : IFieldPropertiesVisitor<FieldPropertiesDto>
{
private static readonly FieldPropertiesDtoFactory Instance = new FieldPropertiesDtoFactory();
private FieldPropertiesDtoFactory()
{
}
public static FieldPropertiesDto Create(FieldProperties properties)
{
return properties.Accept(Instance);
}
public FieldPropertiesDto Visit(AssetsFieldProperties properties)
{
return SimpleMapper.Map(properties, new AssetsFieldPropertiesDto());
}
public FieldPropertiesDto Visit(BooleanFieldProperties properties)
{
return SimpleMapper.Map(properties, new BooleanFieldPropertiesDto());
}
public FieldPropertiesDto Visit(DateTimeFieldProperties properties)
{
return SimpleMapper.Map(properties, new DateTimeFieldPropertiesDto());
}
public FieldPropertiesDto Visit(GeolocationFieldProperties properties)
{
return SimpleMapper.Map(properties, new GeolocationFieldPropertiesDto());
}
public FieldPropertiesDto Visit(JsonFieldProperties properties)
{
return SimpleMapper.Map(properties, new JsonFieldPropertiesDto());
}
public FieldPropertiesDto Visit(ReferencesFieldProperties properties)
{
return SimpleMapper.Map(properties, new ReferencesFieldPropertiesDto());
}
public FieldPropertiesDto Visit(TagsFieldProperties properties)
{
return SimpleMapper.Map(properties, new TagsFieldPropertiesDto());
}
public FieldPropertiesDto Visit(NumberFieldProperties properties)
{
var result = SimpleMapper.Map(properties, new NumberFieldPropertiesDto());
if (properties.AllowedValues != null)
{
result.AllowedValues = properties.AllowedValues.ToArray();
}
return result;
}
public FieldPropertiesDto Visit(StringFieldProperties properties)
{
var result = SimpleMapper.Map(properties, new StringFieldPropertiesDto());
if (properties.AllowedValues != null)
{
result.AllowedValues = properties.AllowedValues.ToArray();
}
return result;
}
}
}

108
src/Squidex/Controllers/Api/Schemas/Models/Converters/SchemaConverter.cs

@ -6,10 +6,7 @@
// All rights reserved.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Read.Schemas;
using Squidex.Domain.Apps.Write.Schemas.Commands;
using Squidex.Infrastructure.Reflection;
@ -18,43 +15,6 @@ namespace Squidex.Controllers.Api.Schemas.Models.Converters
{
public static class SchemaConverter
{
private static readonly Dictionary<Type, Func<FieldProperties, FieldPropertiesDto>> Factories =
new Dictionary<Type, Func<FieldProperties, FieldPropertiesDto>>
{
{
typeof(NumberFieldProperties),
p => Convert((NumberFieldProperties)p)
},
{
typeof(DateTimeFieldProperties),
p => Convert((DateTimeFieldProperties)p)
},
{
typeof(JsonFieldProperties),
p => Convert((JsonFieldProperties)p)
},
{
typeof(StringFieldProperties),
p => Convert((StringFieldProperties)p)
},
{
typeof(BooleanFieldProperties),
p => Convert((BooleanFieldProperties)p)
},
{
typeof(GeolocationFieldProperties),
p => Convert((GeolocationFieldProperties)p)
},
{
typeof(AssetsFieldProperties),
p => Convert((AssetsFieldProperties)p)
},
{
typeof(ReferencesFieldProperties),
p => Convert((ReferencesFieldProperties)p)
}
};
public static SchemaDto ToModel(this ISchemaEntity entity)
{
var dto = new SchemaDto { Properties = new SchemaPropertiesDto() };
@ -78,7 +38,7 @@ namespace Squidex.Controllers.Api.Schemas.Models.Converters
foreach (var field in entity.SchemaDef.Fields)
{
var fieldPropertiesDto = Factories[field.RawProperties.GetType()](field.RawProperties);
var fieldPropertiesDto = FieldPropertiesDtoFactory.Create(field.RawProperties);
var fieldInstanceDto = SimpleMapper.Map(field,
new FieldDto
{
@ -117,71 +77,5 @@ namespace Squidex.Controllers.Api.Schemas.Models.Converters
return command;
}
private static FieldPropertiesDto Convert(BooleanFieldProperties source)
{
var result = SimpleMapper.Map(source, new BooleanFieldPropertiesDto());
return result;
}
private static FieldPropertiesDto Convert(DateTimeFieldProperties source)
{
var result = SimpleMapper.Map(source, new DateTimeFieldPropertiesDto());
return result;
}
private static FieldPropertiesDto Convert(JsonFieldProperties source)
{
var result = SimpleMapper.Map(source, new JsonFieldPropertiesDto());
return result;
}
private static FieldPropertiesDto Convert(GeolocationFieldProperties source)
{
var result = SimpleMapper.Map(source, new GeolocationFieldPropertiesDto());
return result;
}
private static FieldPropertiesDto Convert(AssetsFieldProperties source)
{
var result = SimpleMapper.Map(source, new AssetsFieldPropertiesDto());
return result;
}
private static FieldPropertiesDto Convert(ReferencesFieldProperties source)
{
var result = SimpleMapper.Map(source, new ReferencesFieldPropertiesDto());
return result;
}
private static FieldPropertiesDto Convert(StringFieldProperties source)
{
var result = SimpleMapper.Map(source, new StringFieldPropertiesDto());
if (source.AllowedValues != null)
{
result.AllowedValues = source.AllowedValues.ToArray();
}
return result;
}
private static FieldPropertiesDto Convert(NumberFieldProperties source)
{
var result = SimpleMapper.Map(source, new NumberFieldPropertiesDto());
if (source.AllowedValues != null)
{
result.AllowedValues = source.AllowedValues.ToArray();
}
return result;
}
}
}

1
src/Squidex/Controllers/Api/Schemas/Models/FieldPropertiesDto.cs

@ -23,6 +23,7 @@ namespace Squidex.Controllers.Api.Schemas.Models
[KnownType(typeof(NumberFieldPropertiesDto))]
[KnownType(typeof(ReferencesFieldPropertiesDto))]
[KnownType(typeof(StringFieldPropertiesDto))]
[KnownType(typeof(TagsFieldPropertiesDto))]
public abstract class FieldPropertiesDto
{
/// <summary>

33
src/Squidex/Controllers/Api/Schemas/Models/TagsFieldPropertiesDto.cs

@ -0,0 +1,33 @@
// ==========================================================================
// TagsFieldPropertiesDto.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using NJsonSchema.Annotations;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Controllers.Api.Schemas.Models
{
[JsonSchema("Tags")]
public sealed class TagsFieldPropertiesDto : FieldPropertiesDto
{
/// <summary>
/// The minimum allowed items for the field value.
/// </summary>
public int? MinItems { get; set; }
/// <summary>
/// The maximum allowed items for the field value.
/// </summary>
public int? MaxItems { get; set; }
public override FieldProperties ToProperties()
{
return SimpleMapper.Map(this, new TagsFieldProperties());
}
}
}

34
tests/Squidex.Domain.Apps.Write.Tests/Schemas/Guards/FieldProperties/TagsFieldPropertiesTests.cs

@ -0,0 +1,34 @@
// ==========================================================================
// TagsFieldPropertiesTests.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Infrastructure;
using Xunit;
namespace Squidex.Domain.Apps.Write.Schemas.Guards.FieldProperties
{
public class TagsFieldPropertiesTests
{
[Fact]
public void Should_add_error_if_min_greater_than_max()
{
var sut = new TagsFieldProperties { MinItems = 10, MaxItems = 5 };
var errors = FieldPropertiesValidator.Validate(sut).ToList();
errors.ShouldBeEquivalentTo(
new List<ValidationError>
{
new ValidationError("Max items must be greater than min items.", "MinItems", "MaxItems")
});
}
}
}
Loading…
Cancel
Save