Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Jul 1, 2024
1 parent f549ddc commit 9d90044
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,71 @@
using OrchardCore.Environment.Extensions.Features;
using OrchardCore.Environment.Shell;

namespace OrchardCore.Queries.Services
namespace OrchardCore.Queries.Services;

public sealed class QueriesDocumentUpdater : FeatureEventHandler
{
public sealed class QueriesDocumentUpdater : FeatureEventHandler
private readonly IDocumentManager<QueriesDocument> _documentManager;
private readonly IEnumerable<IQuerySource> _querySources;
private readonly ITypeFeatureProvider _typeFeatureProvider;

public QueriesDocumentUpdater(
IDocumentManager<QueriesDocument> documentManager,
IEnumerable<IQuerySource> querySources,
ITypeFeatureProvider typeFeatureProvider)
{
private readonly IDocumentManager<QueriesDocument> _documentManager;
private readonly IEnumerable<IQuerySource> _querySources;
private readonly ITypeFeatureProvider _typeFeatureProvider;
_documentManager = documentManager;
_querySources = querySources;
_typeFeatureProvider = typeFeatureProvider;
}

public QueriesDocumentUpdater(
IDocumentManager<QueriesDocument> documentManager,
IEnumerable<IQuerySource> querySources,
ITypeFeatureProvider typeFeatureProvider)
public override async Task EnabledAsync(IFeatureInfo feature)
{
// If the newly activated feature contains a IQuerySource, update the QueriesDocument in case that source
// type has been used before.
if (_querySources.Any(source => _typeFeatureProvider.GetFeaturesForDependency(source.GetType()).Any(f => f.Id == feature.Id)))
{
_documentManager = documentManager;
_querySources = querySources;
_typeFeatureProvider = typeFeatureProvider;
await FixupDocumentAsync();
}
}

public override async Task EnabledAsync(IFeatureInfo feature)
{
// If the newly activated feature contains a IQuerySource, update the QueriesDocument in case that source
// type has been used before.
if (_querySources.Any(source => _typeFeatureProvider.GetFeaturesForDependency(source.GetType()).Any(f => f.Id == feature.Id)))
{
await FixupDocumentAsync();
}
}
private async Task FixupDocumentAsync()
{
var document = await _documentManager.GetOrCreateMutableAsync();

private async Task FixupDocumentAsync()
if (!ValidateDocument(document) && FixupDocument(document))
{
var document = await _documentManager.GetOrCreateMutableAsync();

if (!ValidateDocument(document) && FixupDocument(document))
{
await _documentManager.UpdateAsync(document);
}
await _documentManager.UpdateAsync(document);
}
}

private bool FixupDocument(QueriesDocument document)
{
var hasChanged = false;
private bool FixupDocument(QueriesDocument document)
{
var hasChanged = false;

// Check for any query that has no specific type information and try to fix it up with a derived type.
// The type information is lost if a user edits the document while it contains a query for a feature that
// has been disabled.
foreach (var kv in document.Queries.ToArray())
// Check for any query that has no specific type information and try to fix it up with a derived type.
// The type information is lost if a user edits the document while it contains a query for a feature that
// has been disabled.
foreach (var kv in document.Queries)
{
var query = kv.Value;
if (query.GetType() == typeof(Query))
{
var query = kv.Value;
if (query.GetType() == typeof(Query))
{
var sample = _querySources.FirstOrDefault(x => x.Name == query.Source)?.Create();
var sample = _querySources.FirstOrDefault(x => x.Name == query.Source)?.Create();

if (sample != null)
{
var json = JObject.FromObject(query);
if (sample != null)
{
var json = JObject.FromObject(query);

document.Queries[kv.Key] = (Query)json.ToObject(sample.GetType());
hasChanged = true;
}
document.Queries[kv.Key] = (Query)json.ToObject(sample.GetType());
hasChanged = true;
}
}

return hasChanged;
}

private static bool ValidateDocument(QueriesDocument document) => document.Queries.All(kv => kv.Value.GetType() != typeof(Query));
return hasChanged;
}

private static bool ValidateDocument(QueriesDocument document)
=> document.Queries.All(kv => kv.Value.GetType() != typeof(Query));
}
4 changes: 3 additions & 1 deletion src/OrchardCore/OrchardCore.Queries.Abstractions/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ protected Query(string source)
Source = source;
}

public Query() : this("Unknown") { }
public Query() : this("Unknown")
{
}

/// <summary>
/// Gets or sets the technical name of the query.
Expand Down

0 comments on commit 9d90044

Please sign in to comment.