Skip to content

Commit

Permalink
Use the actual save function for saving the copy (#13066)
Browse files Browse the repository at this point in the history
* Use the actual save function for saving the copy

* PR feedback

(cherry picked from commit 3802097)
  • Loading branch information
patrickdemooij9 authored and nul800sebastiaan committed Oct 25, 2022
1 parent 86fb24a commit 8954b7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
36 changes: 17 additions & 19 deletions src/Umbraco.Core/Services/DataTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Web.Common.DependencyInjection;
using Umbraco.Extensions;
using static System.Formats.Asn1.AsnWriter;

namespace Umbraco.Cms.Core.Services.Implement
{
Expand Down Expand Up @@ -441,34 +442,31 @@ private void ConvertMissingEditorsOfDataTypesToLabels(IEnumerable<IDataType> dat
return OperationResult.Attempt.Succeed(MoveOperationStatusType.Success, evtMsgs);
}

public Attempt<OperationResult<MoveOperationStatusType, IDataType>?> Copy(IDataType copying, int containerId)
public Attempt<OperationResult<MoveOperationStatusType, IDataType>?> Copy(IDataType copying, int containerId, int userId = Constants.Security.SuperUserId)
{
var evtMsgs = EventMessagesFactory.Get();

IDataType copy;
using (var scope = ScopeProvider.CreateCoreScope())
try
{
try
if (containerId > 0)
{
if (containerId > 0)
var container = GetContainer(containerId);
if (container is null)
{
var container = _dataTypeContainerRepository.Get(containerId);
if (container is null)
{
throw new DataOperationException<MoveOperationStatusType>(MoveOperationStatusType.FailedParentNotFound); // causes rollback
}
throw new DataOperationException<MoveOperationStatusType>(MoveOperationStatusType.FailedParentNotFound); // causes rollback
}
copy = copying.DeepCloneWithResetIdentities();

copy.Name += " (copy)"; // might not be unique
copy.ParentId = containerId;
_dataTypeRepository.Save(copy);
scope.Complete();
}
catch (DataOperationException<MoveOperationStatusType> ex)
{
return OperationResult.Attempt.Fail<MoveOperationStatusType, IDataType>(ex.Operation, evtMsgs); // causes rollback
}
copy = copying.DeepCloneWithResetIdentities();

copy.Name += " (copy)"; // might not be unique
copy.ParentId = containerId;

Save(copy, userId);
}
catch (DataOperationException<MoveOperationStatusType> ex)
{
return OperationResult.Attempt.Fail<MoveOperationStatusType, IDataType>(ex.Operation, evtMsgs); // causes rollback
}

return OperationResult.Attempt.Succeed(MoveOperationStatusType.Success, evtMsgs, copy);
Expand Down
3 changes: 2 additions & 1 deletion src/Umbraco.Core/Services/IDataTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ public interface IDataTypeService : IService
/// </summary>
/// <param name="copying">The data type that will be copied</param>
/// <param name="containerId">The container ID under where the data type will be copied</param>
/// <param name="userId">The user that did the Copy action</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
Attempt<OperationResult<MoveOperationStatusType, IDataType>?> Copy(IDataType copying, int containerId) => throw new NotImplementedException();
Attempt<OperationResult<MoveOperationStatusType, IDataType>?> Copy(IDataType copying, int containerId, int userId = Constants.Security.SuperUserId) => throw new NotImplementedException();

}
4 changes: 3 additions & 1 deletion src/Umbraco.Web.BackOffice/Controllers/DataTypeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NPoco;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Mapping;
Expand Down Expand Up @@ -391,7 +392,8 @@ public IActionResult PostCopy(MoveOrCopy copy)
return NotFound();
}

Attempt<OperationResult<MoveOperationStatusType, IDataType>?> result = _dataTypeService.Copy(toCopy, copy.ParentId);
var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
Attempt<OperationResult<MoveOperationStatusType, IDataType>?> result = _dataTypeService.Copy(toCopy, copy.ParentId, currentUser?.Id ?? Constants.Security.SuperUserId);
if (result.Success)
{
return Content(toCopy.Path, MediaTypeNames.Text.Plain, Encoding.UTF8);
Expand Down

0 comments on commit 8954b7f

Please sign in to comment.