Prevents saving blueprint with same name as other existing

This commit is contained in:
Lars-Erik Aabech
2017-06-05 16:52:47 +02:00
parent 8602aab6f7
commit 6c78b8b60a
+16 -9
View File
@@ -335,18 +335,13 @@ namespace Umbraco.Web.Editors
[HttpPost]
public SimpleNotificationModel CreateBlueprintFromContent([FromUri]int contentId, [FromUri]string name)
{
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Value cannot be null or whitespace.", "name");
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Value cannot be null or whitespace.", "name");
var content = Services.ContentService.GetById(contentId);
if (content == null)
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
var existing = Services.ContentService.GetBlueprintsForContentTypes(content.ContentTypeId);
if (existing.Any(x => x.Name == name))
{
ModelState.AddModelError("name", Services.TextService.Localize("content/duplicateBlueprintMessage"));
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
}
EnsureUniqueName(name, content, "name");
var blueprint = Services.ContentService.CreateContentFromBlueprint(content, name, Security.GetUserId());
@@ -356,11 +351,21 @@ namespace Umbraco.Web.Editors
notificationModel.AddSuccessNotification(
Services.TextService.Localize("content/createdBlueprintHeading"),
Services.TextService.Localize("content/createdBlueprintMessage", new[]{ content.Name})
);
);
return notificationModel;
}
private void EnsureUniqueName(string name, IContent content, string modelName)
{
var existing = Services.ContentService.GetBlueprintsForContentTypes(content.ContentTypeId);
if (existing.Any(x => x.Name == name && x.Id != content.Id))
{
ModelState.AddModelError(modelName, Services.TextService.Localize("content/duplicateBlueprintMessage"));
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
}
}
/// <summary>
/// Saves content
/// </summary>
@@ -373,6 +378,8 @@ namespace Umbraco.Web.Editors
var contentItemDisplay = PostSaveInternal(contentItem,
content =>
{
EnsureUniqueName(content.Name, content, "Name");
Services.ContentService.SaveBlueprint(contentItem.PersistedContent, Security.CurrentUser.Id);
//we need to reuse the underlying logic so return the result that it wants
return Attempt<OperationStatus>.Succeed(new OperationStatus(OperationStatusType.Success, new EventMessages()));