Skip to content

Commit

Permalink
Fixing LoadingAsync for known parts (#3308)
Browse files Browse the repository at this point in the history
Fixes #3305
  • Loading branch information
sebastienros authored Mar 12, 2019
1 parent 353da80 commit dbee973
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,156 +4,137 @@ namespace OrchardCore.ContentManagement.Handlers
{
public abstract class ContentPartHandler<TPart> : IContentPartHandler where TPart : ContentPart, new()
{
async Task IContentPartHandler.ActivatedAsync(ActivatedContentContext context, ContentPart part)
Task IContentPartHandler.ActivatedAsync(ActivatedContentContext context, ContentPart part)
{
if (part is TPart)
{
await ActivatedAsync(context, (TPart)part);
}
return part is TPart tpart
? ActivatedAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.ActivatingAsync(ActivatingContentContext context, ContentPart part)
Task IContentPartHandler.ActivatingAsync(ActivatingContentContext context, ContentPart part)
{
if (part is TPart)
{
await ActivatingAsync(context, (TPart)part);
}
return part is TPart tpart
? ActivatingAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.InitializingAsync(InitializingContentContext context, ContentPart part)
Task IContentPartHandler.InitializingAsync(InitializingContentContext context, ContentPart part)
{
if (part is TPart)
{
await InitializingAsync(context, (TPart)part);
}
return part is TPart tpart
? InitializingAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.InitializedAsync(InitializingContentContext context, ContentPart part)
Task IContentPartHandler.InitializedAsync(InitializingContentContext context, ContentPart part)
{
if (part is TPart)
{
await InitializedAsync(context, (TPart)part);
}
return part is TPart tpart
? InitializedAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.CreatingAsync(CreateContentContext context, ContentPart part)
Task IContentPartHandler.CreatingAsync(CreateContentContext context, ContentPart part)
{
if (part is TPart)
{
await CreatingAsync(context, (TPart)part);
}
return part is TPart tpart
? CreatingAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.CreatedAsync(CreateContentContext context, ContentPart part)
Task IContentPartHandler.CreatedAsync(CreateContentContext context, ContentPart part)
{
if (part is TPart)
{
await CreatedAsync(context, (TPart)part);
}
return part is TPart tpart
? CreatedAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.LoadingAsync(LoadContentContext context, ContentPart part)
Task IContentPartHandler.LoadingAsync(LoadContentContext context, ContentPart part)
{
if (part is TPart)
{
await LoadingAsync(context, (TPart)part);
}
return part is TPart tpart
? LoadingAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.LoadedAsync(LoadContentContext context, ContentPart part)
Task IContentPartHandler.LoadedAsync(LoadContentContext context, ContentPart part)
{
if (part is TPart)
{
await LoadedAsync(context, (TPart)part);
}
return part is TPart tpart
? LoadedAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.UpdatingAsync(UpdateContentContext context, ContentPart part)
Task IContentPartHandler.UpdatingAsync(UpdateContentContext context, ContentPart part)
{
if (part is TPart)
{
await UpdatingAsync(context, (TPart)part);
}
return part is TPart tpart
? UpdatingAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.UpdatedAsync(UpdateContentContext context, ContentPart part)
Task IContentPartHandler.UpdatedAsync(UpdateContentContext context, ContentPart part)
{
if (part is TPart)
{
await UpdatedAsync(context, (TPart)part);
}
return part is TPart tpart
? UpdatedAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.VersioningAsync(VersionContentContext context, ContentPart existing, ContentPart building)
Task IContentPartHandler.VersioningAsync(VersionContentContext context, ContentPart existing, ContentPart building)
{
if (existing is TPart && building is TPart)
{
await VersioningAsync(context, (TPart)existing, (TPart)building);
}
return existing is TPart texisting && building is TPart tbuilding
? VersioningAsync(context, texisting, tbuilding)
: Task.CompletedTask;
}

async Task IContentPartHandler.VersionedAsync(VersionContentContext context, ContentPart existing, ContentPart building)
Task IContentPartHandler.VersionedAsync(VersionContentContext context, ContentPart existing, ContentPart building)
{
if (existing is TPart && building is TPart)
{
await VersionedAsync(context, (TPart)existing, (TPart)building);
}
return existing is TPart texisting && building is TPart tbuilding
? VersionedAsync(context, texisting, tbuilding)
: Task.CompletedTask;
}

async Task IContentPartHandler.PublishingAsync(PublishContentContext context, ContentPart part)
Task IContentPartHandler.PublishingAsync(PublishContentContext context, ContentPart part)
{
if (part is TPart)
{
await PublishingAsync(context, (TPart)part);
}
return part is TPart tpart
? PublishingAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.PublishedAsync(PublishContentContext context, ContentPart part)
Task IContentPartHandler.PublishedAsync(PublishContentContext context, ContentPart part)
{
if (part is TPart)
{
await PublishedAsync(context, (TPart)part);
}
return part is TPart tpart
? PublishedAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.UnpublishingAsync(PublishContentContext context, ContentPart part)
Task IContentPartHandler.UnpublishingAsync(PublishContentContext context, ContentPart part)
{
if (part is TPart)
{
await UnpublishingAsync(context, (TPart)part);
}
return part is TPart tpart
? UnpublishingAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.UnpublishedAsync(PublishContentContext context, ContentPart part)
Task IContentPartHandler.UnpublishedAsync(PublishContentContext context, ContentPart part)
{
if (part is TPart)
{
await UnpublishedAsync(context, (TPart)part);
}
return part is TPart tpart
? UnpublishedAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.RemovingAsync(RemoveContentContext context, ContentPart part)
Task IContentPartHandler.RemovingAsync(RemoveContentContext context, ContentPart part)
{
if (part is TPart)
{
await RemovingAsync(context, (TPart)part);
}
return part is TPart tpart
? RemovingAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.RemovedAsync(RemoveContentContext context, ContentPart part)
Task IContentPartHandler.RemovedAsync(RemoveContentContext context, ContentPart part)
{
if (part is TPart)
{
await RemovedAsync(context, (TPart)part);
}
return part is TPart tpart
? RemovedAsync(context, tpart)
: Task.CompletedTask;
}

async Task IContentPartHandler.GetContentItemAspectAsync(ContentItemAspectContext context, ContentPart part)
Task IContentPartHandler.GetContentItemAspectAsync(ContentItemAspectContext context, ContentPart part)
{
if (part is TPart)
{
await GetContentItemAspectAsync(context, (TPart)part);
}
return part is TPart tpart
? GetContentItemAspectAsync(context, tpart)
: Task.CompletedTask;
}

public virtual Task ActivatedAsync(ActivatedContentContext context, TPart instance) => Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@ public override async Task LoadingAsync(LoadContentContext context)
foreach (var typePartDefinition in contentTypeDefinition.Parts)
{
var partName = typePartDefinition.PartDefinition.Name;
var part = context.ContentItem.Get<ContentPart>(typePartDefinition.Name);
var activator = _contentPartFactory.GetTypeActivator(partName);

var part = context.ContentItem.Get(activator.Type, typePartDefinition.Name) as ContentPart;

// If no existing part was not found in the content item, create a new one
if (part == null)
{
part = _contentPartFactory.GetTypeActivator(partName).CreateInstance();
part = activator.CreateInstance();
context.ContentItem.Weld(typePartDefinition.Name, part);
}

Expand All @@ -183,8 +186,8 @@ public override async Task LoadingAsync(LoadContentContext context)

if (!part.Has(fieldName))
{
var activator = _contentFieldFactory.GetTypeActivator(partFieldDefinition.FieldDefinition.Name);
context.ContentItem.Get<ContentPart>(typePartDefinition.Name).Weld(fieldName, activator.CreateInstance());
var fieldActivator = _contentFieldFactory.GetTypeActivator(partFieldDefinition.FieldDefinition.Name);
context.ContentItem.Get<ContentPart>(typePartDefinition.Name).Weld(fieldName, fieldActivator.CreateInstance());
}
}
}
Expand Down

0 comments on commit dbee973

Please sign in to comment.