Fixing CreateMemember and CreateContent to raise events and logging only when we persist the objs not when we only create them

This commit is contained in:
Elitsa Marinovska
2021-02-01 16:07:42 +01:00
parent 6df67cd93e
commit 5cd28c8711
4 changed files with 140 additions and 88 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
@@ -197,12 +197,7 @@ namespace Umbraco.Core.Services.Implement
if (parentId > 0 && parent == null)
throw new ArgumentException("No content with that id.", nameof(parentId));
var content = new Content(name, parentId, contentType);
using (var scope = ScopeProvider.CreateScope())
{
CreateContent(scope, content, userId, false);
scope.Complete();
}
var content = new Content(name, parentId, contentType, userId);
return content;
}
@@ -225,20 +220,13 @@ namespace Umbraco.Core.Services.Implement
if (parent == null) throw new ArgumentNullException(nameof(parent));
using (var scope = ScopeProvider.CreateScope())
{
// not locking since not saving anything
var contentType = GetContentType(contentTypeAlias);
if (contentType == null)
throw new ArgumentException("No content type with that alias.", nameof(contentTypeAlias)); // causes rollback
var contentType = GetContentType(contentTypeAlias);
if (contentType == null)
throw new ArgumentException("No content type with that alias.", nameof(contentTypeAlias)); // causes rollback
var content = new Content(name, parent, contentType, userId);
var content = new Content(name, parent, contentType);
CreateContent(scope, content, userId, false);
scope.Complete();
return content;
}
return content;
}
/// <summary>
@@ -267,8 +255,20 @@ namespace Umbraco.Core.Services.Implement
if (parentId > 0 && parent == null)
throw new ArgumentException("No content with that id.", nameof(parentId)); // causes rollback
var content = parentId > 0 ? new Content(name, parent, contentType) : new Content(name, parentId, contentType);
CreateContent(scope, content, userId, true);
var content = parentId > 0 ? new Content(name, parent, contentType, userId) : new Content(name, parentId, contentType, userId);
var evtMsgs = EventMessagesFactory.Get();
// if saving is cancelled, content remains without an identity
var saveEventArgs = new ContentSavingEventArgs(content, evtMsgs);
if (!scope.Events.DispatchCancelable(Saving, this, saveEventArgs, nameof(Saving)))
{
_documentRepository.Save(content);
scope.Events.Dispatch(Saved, this, saveEventArgs.ToContentSavedEventArgs(), nameof(Saved));
scope.Events.Dispatch(TreeChanged, this, new TreeChange<IContent>(content, TreeChangeTypes.RefreshNode).ToEventArgs());
Audit(AuditType.New, content.CreatorId, content.Id, $"Content '{content.Name}' was created with Id {content.Id}");
}
scope.Complete();
return content;
@@ -299,38 +299,25 @@ namespace Umbraco.Core.Services.Implement
if (contentType == null)
throw new ArgumentException("No content type with that alias.", nameof(contentTypeAlias)); // causes rollback
var content = new Content(name, parent, contentType);
CreateContent(scope, content, userId, true);
var content = new Content(name, parent, contentType, userId);
scope.Complete();
return content;
}
}
private void CreateContent(IScope scope, IContent content, int userId, bool withIdentity)
{
content.CreatorId = userId;
content.WriterId = userId;
if (withIdentity)
{
var evtMsgs = EventMessagesFactory.Get();
// if saving is cancelled, content remains without an identity
var saveEventArgs = new ContentSavingEventArgs(content, evtMsgs);
if (scope.Events.DispatchCancelable(Saving, this, saveEventArgs, nameof(Saving)))
return;
if (!scope.Events.DispatchCancelable(Saving, this, saveEventArgs, nameof(Saving)))
{
_documentRepository.Save(content);
_documentRepository.Save(content);
scope.Events.Dispatch(Saved, this, saveEventArgs.ToContentSavedEventArgs(), nameof(Saved));
scope.Events.Dispatch(TreeChanged, this, new TreeChange<IContent>(content, TreeChangeTypes.RefreshNode).ToEventArgs());
scope.Events.Dispatch(Saved, this, saveEventArgs.ToContentSavedEventArgs(), nameof(Saved));
scope.Events.Dispatch(TreeChanged, this, new TreeChange<IContent>(content, TreeChangeTypes.RefreshNode).ToEventArgs());
Audit(AuditType.New, content.CreatorId, content.Id, $"Content '{content.Name}' was created with Id {content.Id}");
}
scope.Complete();
return content;
}
if (withIdentity == false)
return;
Audit(AuditType.New, content.CreatorId, content.Id, $"Content '{content.Name}' was created with Id {content.Id}");
}
#endregion