Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding helpful methods for ContentPart, ContentType builders and Cont… #14717

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,39 @@ namespace OrchardCore.ContentManagement
{
public static class ContentItemExtensions
{
public static bool TryGet<TPart>(this ContentItem contentItem, out TPart part) where TPart : ContentPart
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
=> contentItem.TryGet(typeof(TPart).Name, out part);

public static bool TryGet<TPart>(this ContentItem contentItem, string name, out TPart part) where TPart : ContentPart
{
ArgumentException.ThrowIfNullOrEmpty(name, nameof(name));

try
{
part = contentItem.Get<TPart>(name);
}
catch
{
part = null;
}

return part != null;
}

public static bool TryGet(this ContentItem contentItem, Type contentElementType, string name, out ContentElement part)
{
try
{
part = contentItem.Get(contentElementType, name);
}
catch
{
part = null;
}

return part != null;
}

/// <summary>
/// Gets a content part by its type.
/// </summary>
Expand Down Expand Up @@ -111,7 +144,7 @@ public static ContentItem Merge(this ContentItem contentItem, object properties,
contentItem.Data.Merge(props, jsonMergeSettings);
contentItem.Elements.Clear();

// Return to original value or it will be interpreated as a different object by YesSql.
// Return to original value or it will be interpreted as a different object by YesSql.
contentItem.Id = originalDocumentId;

// After merging content here we need to remove all the well known properties from the Data jObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ContentPartDefinitionBuilder(ContentPartDefinition existing)
if (existing == null)
{
_fields = new List<ContentPartFieldDefinition>();
_settings = new JObject();
_settings = [];
}
else
{
Expand Down Expand Up @@ -106,10 +106,7 @@ public ContentPartDefinitionBuilder MergeSettings(JObject settings)

public ContentPartDefinitionBuilder WithSettings<T>(T settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}
ArgumentNullException.ThrowIfNull(settings, nameof(settings));

var jObject = JObject.FromObject(settings, ContentBuilderSettings.IgnoreDefaultValuesSerializer);
_settings[typeof(T).Name] = jObject;
Expand All @@ -118,16 +115,11 @@ public ContentPartDefinitionBuilder WithSettings<T>(T settings)
}

public ContentPartDefinitionBuilder WithField(string fieldName)
{
return WithField(fieldName, configuration => { });
}
=> WithField(fieldName, configuration => { });

public ContentPartDefinitionBuilder WithField(string fieldName, Action<ContentPartFieldDefinitionBuilder> configuration)
{
if (string.IsNullOrWhiteSpace(fieldName))
{
throw new ArgumentException($"'{nameof(fieldName)}' cannot be null or empty.");
}
ArgumentException.ThrowIfNullOrWhiteSpace(fieldName, nameof(fieldName));

var existingField = _fields.FirstOrDefault(x => string.Equals(x.Name, fieldName, StringComparison.OrdinalIgnoreCase));
if (existingField != null)
Expand All @@ -151,12 +143,25 @@ public ContentPartDefinitionBuilder WithField(string fieldName, Action<ContentPa
return this;
}

public ContentPartDefinitionBuilder WithField<TField>(string fieldName, Action<ContentPartFieldDefinitionBuilder> configuration)
=> WithField(fieldName, part =>
{
configuration(part);

part.OfType(typeof(TField).Name);
});

public Task<ContentPartDefinitionBuilder> WithFieldAsync<TField>(string fieldName, Func<ContentPartFieldDefinitionBuilder, Task> configuration)
=> WithFieldAsync(fieldName, async part =>
{
await configuration(part);

part.OfType(typeof(TField).Name);
});

public async Task<ContentPartDefinitionBuilder> WithFieldAsync(string fieldName, Func<ContentPartFieldDefinitionBuilder, Task> configurationAsync)
{
if (string.IsNullOrWhiteSpace(fieldName))
{
throw new ArgumentException($"'{nameof(fieldName)}' cannot be null or empty.");
}
ArgumentException.ThrowIfNullOrWhiteSpace(fieldName, nameof(fieldName));

var existingField = _fields.FirstOrDefault(x => string.Equals(x.Name, fieldName, StringComparison.OrdinalIgnoreCase));

Expand Down Expand Up @@ -214,19 +219,14 @@ public override ContentPartFieldDefinition Build()
}

public override string Name
{
get { return _fieldName; }
}
=> _fieldName;

public override string FieldType
{
get { return _fieldDefinition.Name; }
}
=> _fieldDefinition.Name;


public override string PartName
{
get { return _partDefinition.Name; }
}
=> _partDefinition.Name;

public override ContentPartFieldDefinitionBuilder OfType(ContentFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ContentTypeDefinitionBuilder
private readonly IList<ContentTypePartDefinition> _parts;
private readonly JObject _settings;

public ContentTypeDefinition Current { get; private set; }
public ContentTypeDefinition Current { get; }

public ContentTypeDefinitionBuilder()
: this(new ContentTypeDefinition(null, null))
Expand All @@ -29,7 +29,7 @@ public ContentTypeDefinitionBuilder(ContentTypeDefinition existing)
if (existing == null)
{
_parts = new List<ContentTypePartDefinition>();
_settings = new JObject();
_settings = [];
}
else
{
Expand Down Expand Up @@ -123,24 +123,25 @@ public ContentTypeDefinitionBuilder RemovePart(string partName)
}

public ContentTypeDefinitionBuilder WithPart(string partName)
{
return WithPart(partName, configuration => { });
}
=> WithPart(partName, configuration => { });

public ContentTypeDefinitionBuilder WithPart(string name, string partName)
{
return WithPart(name, new ContentPartDefinition(partName), configuration => { });
}
=> WithPart(name, new ContentPartDefinition(partName), configuration => { });

public ContentTypeDefinitionBuilder WithPart(string name, string partName, Action<ContentTypePartDefinitionBuilder> configuration)
{
return WithPart(name, new ContentPartDefinition(partName), configuration);
}
=> WithPart(name, new ContentPartDefinition(partName), configuration);

public ContentTypeDefinitionBuilder WithPart<TPart>() where TPart : ContentPart
=> WithPart(typeof(TPart).Name, configuration => { });

public ContentTypeDefinitionBuilder WithPart<TPart>(string name) where TPart : ContentPart
=> WithPart(name, new ContentPartDefinition(typeof(TPart).Name), configuration => { });

public ContentTypeDefinitionBuilder WithPart<TPart>(string name, Action<ContentTypePartDefinitionBuilder> configuration) where TPart : ContentPart
=> WithPart(name, new ContentPartDefinition(typeof(TPart).Name), configuration);

public ContentTypeDefinitionBuilder WithPart(string partName, Action<ContentTypePartDefinitionBuilder> configuration)
{
return WithPart(partName, new ContentPartDefinition(partName), configuration);
}
=> WithPart(partName, new ContentPartDefinition(partName), configuration);

public ContentTypeDefinitionBuilder WithPart(string name, ContentPartDefinition partDefinition, Action<ContentTypePartDefinitionBuilder> configuration)
{
Expand All @@ -164,14 +165,10 @@ public ContentTypeDefinitionBuilder WithPart(string name, ContentPartDefinition
}

public Task<ContentTypeDefinitionBuilder> WithPartAsync(string name, string partName, Func<ContentTypePartDefinitionBuilder, Task> configurationAsync)
{
return WithPartAsync(name, new ContentPartDefinition(partName), configurationAsync);
}
=> WithPartAsync(name, new ContentPartDefinition(partName), configurationAsync);

public Task<ContentTypeDefinitionBuilder> WithPartAsync(string partName, Func<ContentTypePartDefinitionBuilder, Task> configurationAsync)
{
return WithPartAsync(partName, new ContentPartDefinition(partName), configurationAsync);
}
=> WithPartAsync(partName, new ContentPartDefinition(partName), configurationAsync);

public async Task<ContentTypeDefinitionBuilder> WithPartAsync(string name, ContentPartDefinition partDefinition, Func<ContentTypePartDefinitionBuilder, Task> configurationAsync)
{
Expand All @@ -183,7 +180,7 @@ public async Task<ContentTypeDefinitionBuilder> WithPartAsync(string name, Conte
}
else
{
existingPart = new ContentTypePartDefinition(name, partDefinition, new JObject())
existingPart = new ContentTypePartDefinition(name, partDefinition, [])
{
ContentTypeDefinition = Current,
};
Expand Down