diff --git a/src/DragonFly.API.Core/ContentItems/RestContentItem.cs b/src/DragonFly.API.Core/ContentItems/RestContentItem.cs index 25932335..9f9ad1d3 100644 --- a/src/DragonFly.API.Core/ContentItems/RestContentItem.cs +++ b/src/DragonFly.API.Core/ContentItems/RestContentItem.cs @@ -4,7 +4,6 @@ using System.Text.Json.Nodes; using System.Text.Json.Serialization; -using DragonFly.Validations; namespace DragonFly.API; @@ -29,7 +28,7 @@ public class RestContentItem : RestContentBase /// ValidationContext /// [JsonPropertyOrder(4)] - public ValidationContext? ValidationContext { get; set; } + public ValidationState? ValidationContext { get; set; } /// /// Fields diff --git a/src/DragonFly.API.Core/ContentItems/RestContentItemConverter.cs b/src/DragonFly.API.Core/ContentItems/RestContentItemConverter.cs index c259b8ef..8827b5d7 100644 --- a/src/DragonFly.API.Core/ContentItems/RestContentItemConverter.cs +++ b/src/DragonFly.API.Core/ContentItems/RestContentItemConverter.cs @@ -37,7 +37,7 @@ public static ContentItem ToModel(this RestContentItem restContentItem) contentItem.PublishedAt = restContentItem.PublishedAt; contentItem.Version = restContentItem.Version; contentItem.SchemaVersion = restContentItem.SchemaVersion; - contentItem.ValidationContext = restContentItem.ValidationContext; + contentItem.ValidationState = restContentItem.ValidationContext; foreach (var restField in restContentItem.Fields) { @@ -97,7 +97,7 @@ public static RestContentItem ToRest(this ContentItem contentItem, bool includeS restContentItem.PublishedAt = contentItem.PublishedAt; restContentItem.Version = contentItem.Version; restContentItem.SchemaVersion = contentItem.SchemaVersion; - restContentItem.ValidationContext = contentItem.ValidationContext; + restContentItem.ValidationContext = contentItem.ValidationState; foreach (var field in contentItem.Fields) { diff --git a/src/DragonFly.App.Server/Models/Product2.cs b/src/DragonFly.App.Server/Models/Product2.cs index 4d95e643..e0655a99 100644 --- a/src/DragonFly.App.Server/Models/Product2.cs +++ b/src/DragonFly.App.Server/Models/Product2.cs @@ -14,7 +14,7 @@ public partial class Product2 [StringField(Required = true, Index = true, MaxLength = 1024, ReferenceField = true, ListField = true, QueryField = true)] private string? _title; - [SlugField(Index = true, QueryField = true)] + [SlugField(Index = true, QueryField = true, TargetField = nameof(Title))] private SlugField _slug; [BoolField(Index = true, QueryField = true)] @@ -23,22 +23,19 @@ public partial class Product2 [IntegerField(Index = true, QueryField = true)] private long? _quantity; - [FloatField(Index = true, QueryField = true)] + [FloatField] private double? _quantity2; - [HtmlField] - private BlockField _htmlContent; - [BlockField] private BlockField _mainContent; [AssetField(ShowPreview = true, Index = true, QueryField = true)] private AssetField _image; - [AssetField(ShowPreview = true, Index = true, QueryField = true)] + [AssetField(ShowPreview = true)] private Asset _image2; - [ReferenceField(Index = true, QueryField = true)] + [ReferenceField] private ReferenceField _customerA; [ReferenceField] @@ -47,7 +44,7 @@ public partial class Product2 [ReferenceField] private Customer? _customerC; - [GeolocationField(Index = true, QueryField = true)] + [GeolocationField] private GeolocationField _location; [TextField] diff --git a/src/DragonFly.Client/Modules/ContentInitializer.cs b/src/DragonFly.Client/Modules/ContentInitializer.cs index a84cffda..eeab2eb7 100644 --- a/src/DragonFly.Client/Modules/ContentInitializer.cs +++ b/src/DragonFly.Client/Modules/ContentInitializer.cs @@ -32,7 +32,7 @@ public Task ExecuteAsync(IDragonFlyApi api) api.Field().Add().WithFieldView().WithOptionView().WithQueryView(); api.Field().Add().WithFieldView().WithOptionView().WithQueryView(); api.Field().Add().WithFieldView().WithOptionView().WithQueryView(); - api.Field().Add().WithFieldView(); + api.Field().Add().WithFieldView().WithOptionView(); api.Field().Add().WithFieldView(); api.Field().Add().WithFieldView(); api.Field().Add().WithFieldView(); diff --git a/src/DragonFly.Client/Pages/ContentItems/ContentItemButton.razor b/src/DragonFly.Client/Pages/ContentItems/ContentItemButton.razor index 349f085a..206c651f 100644 --- a/src/DragonFly.Client/Pages/ContentItems/ContentItemButton.razor +++ b/src/DragonFly.Client/Pages/ContentItems/ContentItemButton.razor @@ -12,9 +12,9 @@ }
- @if (ShowValidationErrors && ContentItem.ValidationContext.State == ValidationState.Invalid) + @if (ShowValidationErrors && ContentItem.ValidationState.Result == ValidationResult.Invalid) { - + }
@* no list fields available? Show id field only *@ diff --git a/src/DragonFly.Client/Pages/ContentItems/ContentItemDetail.razor.cs b/src/DragonFly.Client/Pages/ContentItems/ContentItemDetail.razor.cs index 23030a00..b39dc15c 100644 --- a/src/DragonFly.Client/Pages/ContentItems/ContentItemDetail.razor.cs +++ b/src/DragonFly.Client/Pages/ContentItems/ContentItemDetail.razor.cs @@ -4,7 +4,6 @@ using BlazorStrap; using DragonFly.Client.Base; -using DragonFly.Validations; using DragonFly.Razor.Base; using Microsoft.AspNetCore.Components; using System; @@ -52,7 +51,7 @@ public ContentItemDetailBase() public bool IsFieldValid(string field) { - return Entity.ValidationContext.Errors.All(x => x.Field != field); + return Entity.ValidationState.Errors.All(x => x.Field != field); } protected BSColor GetFieldColor(string field) @@ -159,7 +158,7 @@ protected override void OnGuiStateChanged() } } - foreach (ValidationError error in Entity.ValidationContext.Errors) + foreach (ValidationError error in Entity.ValidationState.Errors) { Notifications.Add(new NotificationItem(NotificationType.Error, error.Message)); } @@ -201,7 +200,7 @@ protected virtual async Task PreviewAsync() protected override void OnSaving(SavingEventArgs args) { - if (Entity.Validate() == ValidationState.Invalid) + if (Entity.Validate() == ValidationResult.Invalid) { OnGuiStateChanged(); diff --git a/src/DragonFly.Client/Pages/ContentItems/ContentItemFields2.razor b/src/DragonFly.Client/Pages/ContentItems/ContentItemFields2.razor index 84ccdcfe..b9017de7 100644 --- a/src/DragonFly.Client/Pages/ContentItems/ContentItemFields2.razor +++ b/src/DragonFly.Client/Pages/ContentItems/ContentItemFields2.razor @@ -23,10 +23,8 @@ @* fields *@ @foreach (var part in items) { -
- +
@ComponentManager.CreateComponent(IsReadOnly, part.FieldName, part.ContentField, part.SchemaField.Options) -
} @@ -47,7 +45,7 @@ { string fieldPath = field; - if (Content.ValidationContext.Errors.Any(x => x.Field == field)) + if (Content.ValidationState.Errors.Any(x => x.Field == field)) { return BSColor.Danger; } diff --git a/src/DragonFly.Client/Pages/ContentItems/ContentItemList.razor b/src/DragonFly.Client/Pages/ContentItems/ContentItemList.razor index 1f1ed654..72d80c5e 100644 --- a/src/DragonFly.Client/Pages/ContentItems/ContentItemList.razor +++ b/src/DragonFly.Client/Pages/ContentItems/ContentItemList.razor @@ -36,7 +36,7 @@
- +
@* no list fields available? Show id field only *@ @if (contentItem.Schema.ListFields.Any() == false) diff --git a/src/DragonFly.Client/Pages/ContentSchemas/Fields/SlugFieldOptionsView.razor b/src/DragonFly.Client/Pages/ContentSchemas/Fields/SlugFieldOptionsView.razor new file mode 100644 index 00000000..a6fa9a56 --- /dev/null +++ b/src/DragonFly.Client/Pages/ContentSchemas/Fields/SlugFieldOptionsView.razor @@ -0,0 +1,27 @@ +@inherits FieldOptionsComponent + + + + + + + + +
+ +
+
+ + +
+ +
+
+ + +
+ +
+
+
+
\ No newline at end of file diff --git a/src/DragonFly.Client/_Imports.razor b/src/DragonFly.Client/_Imports.razor index 5a148def..b5bcb708 100644 --- a/src/DragonFly.Client/_Imports.razor +++ b/src/DragonFly.Client/_Imports.razor @@ -28,7 +28,6 @@ @using DragonFly.Client.Pages.ContentItems.Query @using DragonFly.Client.Pages.ContentSchemas @using DragonFly.Client.Helpers -@using DragonFly.Validations @using BlazorStrap @using BlazorStrap.V5 @attribute [Authorize] diff --git a/src/DragonFly.Core/Api/DragonFlyApi.cs b/src/DragonFly.Core/Api/DragonFlyApi.cs index 9aefabd3..440cce7d 100644 --- a/src/DragonFly.Core/Api/DragonFlyApi.cs +++ b/src/DragonFly.Core/Api/DragonFlyApi.cs @@ -2,13 +2,17 @@ // https://github.com/usercode/DragonFly // MIT License -namespace DragonFly.Init; +namespace DragonFly; /// /// DragonFlyApi /// -public class DragonFlyApi : IDragonFlyApi +public sealed class DragonFlyApi : IDragonFlyApi { +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + public static IDragonFlyApi Default { get; internal set; } +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + public DragonFlyApi(IServiceProvider serviceProvider) { ServiceProvider = serviceProvider; @@ -17,5 +21,5 @@ public DragonFlyApi(IServiceProvider serviceProvider) /// /// ServiceProvider /// - public IServiceProvider ServiceProvider { get; } + public IServiceProvider ServiceProvider { get; } } diff --git a/src/DragonFly.Core/Api/Init/DragonFlyApiExtensions.cs b/src/DragonFly.Core/Api/Init/DragonFlyApiExtensions.cs index fc5553b5..e11cd707 100644 --- a/src/DragonFly.Core/Api/Init/DragonFlyApiExtensions.cs +++ b/src/DragonFly.Core/Api/Init/DragonFlyApiExtensions.cs @@ -11,6 +11,8 @@ public static class DragonFlyApiExtensions { public static async Task InitAsync(this IDragonFlyApi api) { + DragonFlyApi.Default = api; + ILogger logger = api.ServiceProvider.GetRequiredService>(); using IServiceScope scope = api.ServiceProvider.CreateScope(); @@ -34,7 +36,7 @@ public static async Task InitAsync(this IDragonFlyApi api) logger.LogInformation("Initializing {type}", GetName(item)); await item.ExecuteAsync(api); - } + } } private static string? GetName(object initializer) diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/ContentItem.cs b/src/DragonFly.Core/Modules/ContentItems/Models/ContentItem.cs index 3f6467a1..a39fe2c6 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/ContentItem.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/ContentItem.cs @@ -2,8 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; - namespace DragonFly; /// @@ -36,26 +34,22 @@ public ContentItem(ContentSchema schema, Guid id) /// public virtual ContentSchema Schema { get => _schema; set => _schema = value; } - private ContentFields _fields = new ContentFields(); - /// /// Fields /// - public virtual ContentFields Fields { get => _fields; set => _fields = value; } - - private ValidationContext _validationContext = new ValidationContext(); + public virtual ContentFields Fields { get; set; } = new ContentFields(); /// /// ValidationContext /// - public virtual ValidationContext ValidationContext { get => _validationContext; set => _validationContext = value; } + public virtual ValidationState ValidationState { get; set; } = new ValidationState(); /// /// Validates the content item. /// - public virtual ValidationState Validate() + public virtual ValidationResult Validate() { - ValidationContext validationContext = new ValidationContext(ValidationState.Valid); + ValidationContext validationContext = new ValidationContext(this); foreach (var field in Fields) { @@ -67,9 +61,9 @@ public virtual ValidationState Validate() } } - ValidationContext = validationContext; + ValidationState = validationContext.Execute(); - return validationContext.State; + return ValidationState.Result; } public override int GetHashCode() @@ -77,19 +71,7 @@ public override int GetHashCode() return HashCode.Combine(Schema.Name, Id); } - public override bool Equals(object? obj) - { - if (obj is ContentItem other) - { - return Equals(other); - } - else - { - return false; - } - } - - public bool Equals(ContentItem? other) + public override bool Equals(ContentItem? other) { if (other is null) { diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Entity.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Entity.cs index bfbab18e..61aec0b7 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Entity.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Entity.cs @@ -4,7 +4,7 @@ namespace DragonFly; -public abstract class Entity : IEntity +public abstract class Entity : IEntity, IEquatable where T : IEntity { protected Guid _id; @@ -20,10 +20,20 @@ public override bool Equals(object? obj) { if (obj is T other) { - return Id == other.Id; + return Equals(other); } - return base.Equals(obj); + return false; + } + + public virtual bool Equals(T? other) + { + if (other is null) + { + return false; + } + + return _id == other.Id; } public override int GetHashCode() diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ArrayField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ArrayField.cs index 6fa4cd40..7deb3a46 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ArrayField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ArrayField.cs @@ -2,8 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; - namespace DragonFly; /// @@ -15,13 +13,12 @@ public partial class ArrayField { public ArrayField() { - Items = new List(); } /// /// Items /// - public IList Items { get; set; } + public IList Items { get; set; }= new List(); public override void Clear() { diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/AssetField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/AssetField.cs index bf2ebc3f..7cf56bb2 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/AssetField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/AssetField.cs @@ -2,8 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; - namespace DragonFly; /// diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ColorField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ColorField.cs index 5046e4d9..8f94c9d1 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ColorField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ColorField.cs @@ -3,7 +3,6 @@ // MIT License using System.Text.RegularExpressions; -using DragonFly.Validations; namespace DragonFly; diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ComponentField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ComponentField.cs index d73fb267..f054f3c1 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ComponentField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/ComponentField.cs @@ -2,8 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; - namespace DragonFly; /// diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/FloatField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/FloatField.cs index 619b27a7..0488c84b 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/FloatField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/FloatField.cs @@ -2,8 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; - namespace DragonFly; /// diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/GeolocationField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/GeolocationField.cs index 4e6a96a9..7315f867 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/GeolocationField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/GeolocationField.cs @@ -2,8 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; - namespace DragonFly; [Field] diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/SlugField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/SlugField.cs index a444a6f3..e7b7203d 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/SlugField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/SlugField.cs @@ -2,6 +2,8 @@ // https://github.com/usercode/DragonFly // MIT License +using Microsoft.Extensions.DependencyInjection; + namespace DragonFly; /// @@ -21,11 +23,23 @@ public SlugField(string? text) Value = text; } - protected override void OnValueChanging(ref string? newValue) + public override void Validate(string fieldName, FieldOptions options, ValidationContext context) { - //if (newValue != null) - //{ - // newValue = Slugify.ToSlug(newValue); - //} + if (HasValue == false) + { + if (options is SlugFieldOptions fieldOptions && string.IsNullOrEmpty(fieldOptions.TargetField) == false) + { + var sourceField = context.ContentItem.GetField>(fieldOptions.TargetField); + + if (sourceField?.Value != null) + { + ISlugService slugService = DragonFlyApi.Default.ServiceProvider.GetRequiredService(); + + Value = slugService.Transform(sourceField.Value); + } + } + } + + base.Validate(fieldName, options, context); } } diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/SlugFieldOptions.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/SlugFieldOptions.cs index 15ce03f4..c7b9761c 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/SlugFieldOptions.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/SlugFieldOptions.cs @@ -4,10 +4,15 @@ namespace DragonFly; -public class SlugFieldOptions : StringFieldOptions +public class SlugFieldOptions : FieldOptions { public override ContentField CreateContentField() { - return new SlugField(DefaultValue); + return new SlugField(); } + + /// + /// Gets the name of a single value field which is used for the slug value. + /// + public string? TargetField { get; set; } } diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/StringField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/StringField.cs index d07f4c11..cfb25536 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/StringField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/StringField.cs @@ -2,9 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; -using DragonFly.Query; - namespace DragonFly; /// diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/UrlField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/UrlField.cs index 2256e4e9..699c85b0 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/UrlField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/UrlField.cs @@ -2,8 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; - namespace DragonFly; /// diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/XmlField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/XmlField.cs index 7d32f626..1ce212ea 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/XmlField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/XmlField.cs @@ -3,7 +3,6 @@ // MIT License using System.Xml.Linq; -using DragonFly.Validations; namespace DragonFly; diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/_Base/ContentField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/_Base/ContentField.cs index a7e4de81..7f248dab 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/_Base/ContentField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/_Base/ContentField.cs @@ -2,8 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; - namespace DragonFly; /// diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/_Base/SingleValueField.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/_Base/SingleValueField.cs index a181b97a..73294a25 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Fields/_Base/SingleValueField.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Fields/_Base/SingleValueField.cs @@ -2,7 +2,6 @@ // https://github.com/usercode/DragonFly // MIT License -using DragonFly.Validations; using System.Diagnostics.CodeAnalysis; namespace DragonFly; @@ -21,7 +20,7 @@ public virtual T? Value { get => _value; set - { + { OnValueChanging(ref value); _value = value; diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationContext.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationContext.cs index 1a6f7fe3..4dc72336 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationContext.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationContext.cs @@ -4,42 +4,27 @@ using System.Text; -namespace DragonFly.Validations; +namespace DragonFly; /// /// ValidationContext /// public class ValidationContext { - public ValidationContext() + public ValidationContext(ContentItem contentItem) { - State = ValidationState.Unknown; + ContentItem = contentItem; } - public ValidationContext(ValidationState state) - : this() - { - State = state; - } - - private IList _errors = new List(); - /// - /// Errors + /// ContentItem /// - public virtual IEnumerable Errors { get => _errors; set => _errors = value.ToList(); } + public ContentItem ContentItem { get; } /// - /// State + /// Errors /// - public ValidationState State { get; set; } - - public void AddError(ValidationError error) - { - State = ValidationState.Invalid; - - _errors.Add(error); - } + public IList Errors { get; } = new List(); public string GetMessage() { @@ -52,4 +37,14 @@ public string GetMessage() return stringBuilder.ToString(); } + + public ValidationState Execute() + { + ValidationState state = new ValidationState(); + state.Result = (Errors.Count == 0) ? ValidationResult.Valid : ValidationResult.Invalid; + state.Errors = Errors; + state.Message = GetMessage(); + + return state; + } } diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationError.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationError.cs index b897b38b..23288118 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationError.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationError.cs @@ -2,7 +2,7 @@ // https://github.com/usercode/DragonFly // MIT License -namespace DragonFly.Validations; +namespace DragonFly; /// /// ValidationError diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationErrorExtensions.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationErrorExtensions.cs index 4e5d576b..47a5cb25 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationErrorExtensions.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationErrorExtensions.cs @@ -2,41 +2,41 @@ // https://github.com/usercode/DragonFly // MIT License -namespace DragonFly.Validations; +namespace DragonFly; public static class ValidationErrorExtensions { public static ValidationContext AddRequireValidation(this ValidationContext context, string field) { - context.AddError(new ValidationError(field, $"The field \"{field}\" is required.")); + context.Errors.Add(new ValidationError(field, $"The field \"{field}\" is required.")); return context; } public static ValidationContext AddRangeValidation(this ValidationContext context, string field, double? from, double? to) { - context.AddError(new ValidationError(field, $"The field \"{field}\" is out of range! The value must be between {from} and {to}.")); + context.Errors.Add(new ValidationError(field, $"The field \"{field}\" is out of range! The value must be between {from} and {to}.")); return context; } public static ValidationContext AddMinimumValidation(this ValidationContext context, string field, double? value) { - context.AddError(new ValidationError(field, $"The field \"{field}\" must be at least {value} characters long.")); + context.Errors.Add(new ValidationError(field, $"The field \"{field}\" must be at least {value} characters long.")); return context; } public static ValidationContext AddMaximumValidation(this ValidationContext context, string field, double? value) { - context.AddError(new ValidationError(field, $"The field \"{field}\" can't be longer than {value} characters.")); + context.Errors.Add(new ValidationError(field, $"The field \"{field}\" can't be longer than {value} characters.")); return context; } public static ValidationContext AddInvalidValidation(this ValidationContext context, string field) { - context.AddError(new ValidationError(field, $"The field \"{field}\" isn't valid!")); + context.Errors.Add(new ValidationError(field, $"The field \"{field}\" isn't valid!")); return context; } diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationResult.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationResult.cs new file mode 100644 index 00000000..ed67aa4f --- /dev/null +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationResult.cs @@ -0,0 +1,12 @@ +// Copyright (c) usercode +// https://github.com/usercode/DragonFly +// MIT License + +namespace DragonFly; + +public enum ValidationResult +{ + Unknown, + Valid, + Invalid +} diff --git a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationState.cs b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationState.cs index 2f31acd9..a4dc1be1 100644 --- a/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationState.cs +++ b/src/DragonFly.Core/Modules/ContentItems/Models/Validations/ValidationState.cs @@ -4,9 +4,23 @@ namespace DragonFly; -public enum ValidationState +/// +/// ValidationState +/// +public class ValidationState { - Unknown, - Valid, - Invalid + /// + /// Errors + /// + public IList Errors { get; set; } = new List(); + + /// + /// Result + /// + public ValidationResult Result { get; set; } = ValidationResult.Unknown; + + /// + /// Message + /// + public string Message { get; set; } = string.Empty; } diff --git a/src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/DateFieldAttribute.cs b/src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/DateFieldAttribute.cs index 0c574abd..1ce9d605 100644 --- a/src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/DateFieldAttribute.cs +++ b/src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/DateFieldAttribute.cs @@ -17,6 +17,7 @@ public override void ApplyToSchema(ContentSchema schema, string property) schema.AddDate(property, x => { x.IsRequired = Required; + x.IsSearchable = Index; }, SortKey); } diff --git a/src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/SlugFieldAttribute.cs b/src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/SlugFieldAttribute.cs index 2da802d7..3d9e2666 100644 --- a/src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/SlugFieldAttribute.cs +++ b/src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/SlugFieldAttribute.cs @@ -10,6 +10,8 @@ public SlugFieldAttribute() { } + public string? TargetField { get; set; } + public override void ApplyToSchema(ContentSchema schema, string property) { base.ApplyToSchema(schema, property); @@ -18,7 +20,8 @@ public override void ApplyToSchema(ContentSchema schema, string property) { x.IsRequired = Required; x.IsSearchable = Index; + x.TargetField = TargetField; }, - SortKey); + SortKey); } } diff --git a/src/DragonFly.Core/Modules/ContentSchemas/Models/ContentSchema.cs b/src/DragonFly.Core/Modules/ContentSchemas/Models/ContentSchema.cs index ea90a044..74dee088 100644 --- a/src/DragonFly.Core/Modules/ContentSchemas/Models/ContentSchema.cs +++ b/src/DragonFly.Core/Modules/ContentSchemas/Models/ContentSchema.cs @@ -23,56 +23,44 @@ public ContentSchema(string name) /// public virtual string Name { get => _name; set => _name = value; } - private SchemaFields _fields = new SchemaFields(); - /// /// Fields /// - public virtual SchemaFields Fields { get => _fields; set => _fields = value; } - - private IList _listFields = new List(); + public virtual SchemaFields Fields { get; set; } = new SchemaFields(); /// /// ListFields /// - public virtual IList ListFields { get => _listFields; set => _listFields = value; } - - private IList _referenceFields = new List(); + public virtual IList ListFields { get; set; } = new List(); /// /// ReferenceFields /// - public virtual IList ReferenceFields { get => _referenceFields; set => _referenceFields = value; } - - private IList _orderFields = new List(); + public virtual IList ReferenceFields { get; set; } = new List(); /// /// OrderFields /// - public IList OrderFields { get => _orderFields; set => _orderFields = value; } - - private IList _queryFields = new List(); + public IList OrderFields { get; set; } = new List(); /// /// QueryFields /// - public IList QueryFields { get => _queryFields; set => _queryFields = value; } - - private PreviewItem _previewOptions = new PreviewItem(); + public IList QueryFields { get; set; } = new List(); /// /// Preview /// - public PreviewItem Preview { get => _previewOptions; set => _previewOptions = value; } + public PreviewItem Preview { get; set; } = new PreviewItem(); - public override bool Equals(object? obj) + public override bool Equals(ContentSchema? obj) { - if (obj is ContentSchema other) + if (obj is null) { - return Name == other.Name; + return false; } - return false; + return Name == obj.Name; } public override int GetHashCode() diff --git a/src/DragonFly.GraphQL/DragonFlyBuilderExtensions.cs b/src/DragonFly.GraphQL/DragonFlyBuilderExtensions.cs index c7a271a6..ce9e7004 100644 --- a/src/DragonFly.GraphQL/DragonFlyBuilderExtensions.cs +++ b/src/DragonFly.GraphQL/DragonFlyBuilderExtensions.cs @@ -3,6 +3,8 @@ // MIT License using DragonFly.AspNetCore.Builders; +using DragonFly.AspNetCore.GraphQL; +using DragonFly.GraphQL.ObjectTypes; using Microsoft.AspNetCore.Builder; namespace DragonFly.AspNetCore; @@ -23,11 +25,8 @@ public static IDragonFlyApplicationBuilder UseGraphQLApi(this IDragonFlyApplicat x.UseAuthentication(); x.UseAuthorization(); - //x.UseGraphQL<>(); - //x.UseGraphQLPlayground(new GraphQLPlaygroundOptions - //{ - // Path = "/ui/playground" - //}); + x.UseGraphQL(); + x.UseGraphQLPlayground(); x.UseEndpoints(endpoints => { diff --git a/src/DragonFly.GraphQL/ObjectTypes/DragonFlySchema.cs b/src/DragonFly.GraphQL/ObjectTypes/DragonFlySchema.cs new file mode 100644 index 00000000..2f122773 --- /dev/null +++ b/src/DragonFly.GraphQL/ObjectTypes/DragonFlySchema.cs @@ -0,0 +1,18 @@ +// Copyright (c) usercode +// https://github.com/usercode/DragonFly +// MIT License + +using DragonFly.AspNetCore.GraphQL; +using GraphQL; +using GraphQL.Types; + +namespace DragonFly.GraphQL.ObjectTypes; + +internal class DragonFlySchema : Schema +{ + public DragonFlySchema() + { + + + } +} diff --git a/src/DragonFly.GraphQL/Models/GraphContentSchema.cs b/src/DragonFly.GraphQL/ObjectTypes/GraphContentSchema.cs similarity index 100% rename from src/DragonFly.GraphQL/Models/GraphContentSchema.cs rename to src/DragonFly.GraphQL/ObjectTypes/GraphContentSchema.cs diff --git a/src/DragonFly.MongoDB/DragonFlyBuilderExtensions.cs b/src/DragonFly.MongoDB/DragonFlyBuilderExtensions.cs index 3a591056..42259ef7 100644 --- a/src/DragonFly.MongoDB/DragonFlyBuilderExtensions.cs +++ b/src/DragonFly.MongoDB/DragonFlyBuilderExtensions.cs @@ -6,6 +6,7 @@ using DragonFly.MongoDB; using Microsoft.Extensions.DependencyInjection; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Conventions; namespace DragonFly.AspNetCore; @@ -57,6 +58,12 @@ public static IDragonFlyBuilder AddMongoDbStorage(this IDragonFlyBuilder builder builder.PostInit(); + var pack = new ConventionPack() + { + new IgnoreExtraElementsConvention(true) + }; + ConventionRegistry.Register("IgnoreExtraElements", pack, t => true); + return builder; } diff --git a/src/DragonFly.MongoDB/Storages/MongoStorage.ContentItem.cs b/src/DragonFly.MongoDB/Storages/MongoStorage.ContentItem.cs index 2fc0861f..4fb6915b 100644 --- a/src/DragonFly.MongoDB/Storages/MongoStorage.ContentItem.cs +++ b/src/DragonFly.MongoDB/Storages/MongoStorage.ContentItem.cs @@ -245,17 +245,15 @@ public async Task UpdateAsync(ContentItem content) ContentSchema? schema = await GetSchemaAsync(content.Schema.Name); content.ApplySchema(schema); + content.Validate(); - IMongoCollection drafted = GetMongoCollection(content.Schema.Name); - - //validation - content.Validate(); - - if (content.ValidationContext.State == ValidationState.Invalid) + if (content.ValidationState.Result == ValidationResult.Invalid) { throw new Exception("Invalid state"); } + IMongoCollection drafted = GetMongoCollection(content.Schema.Name); + //versioning if (DragonFlyOptions.Versioning != ContentVersionKind.None) { diff --git a/src/Template/templates/DragonFlyApp.Client/Program.cs b/src/Template/templates/DragonFlyApp.Client/Program.cs index 32b7eb9b..f8d4b9c8 100644 --- a/src/Template/templates/DragonFlyApp.Client/Program.cs +++ b/src/Template/templates/DragonFlyApp.Client/Program.cs @@ -18,7 +18,7 @@ .Init(x => { //Use TinyMCE editor for html field - //x.ContentField().Add().WithTinyMceView(); + //x.Field().Add().WithTinyMceView(); }); WebAssemblyHost host = builder.Build(); diff --git a/src/Template/templates/DragonFlyApp.Client/wwwroot/index.html b/src/Template/templates/DragonFlyApp.Client/wwwroot/index.html index c71713d6..c4106e88 100644 --- a/src/Template/templates/DragonFlyApp.Client/wwwroot/index.html +++ b/src/Template/templates/DragonFlyApp.Client/wwwroot/index.html @@ -6,9 +6,8 @@ DragonFly - - - + +