From cb98385242d18c0abf7240145193a8f53b8de18e Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 18 Nov 2015 15:52:32 +0100 Subject: [PATCH] updates content/media service methods - null checks for parent IContent, dosn't use Parent() syntactic sugar methods, removes path creation in the WithIdentity methods. --- src/Umbraco.Core/Services/ContentService.cs | 9 ++++++--- src/Umbraco.Core/Services/MediaService.cs | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 499367088f..8427a46d75 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -152,7 +152,9 @@ namespace Umbraco.Core.Services { var contentType = FindContentTypeByAlias(contentTypeAlias); var content = new Content(name, parentId, contentType); - content.Path = string.Concat(content.Parent().IfNotNull(x => x.Path, content.ParentId.ToString()), ",", content.Id); + var parent = GetById(content.ParentId); + content.Path = string.Concat(parent.IfNotNull(x => x.Path, content.ParentId.ToString()), ",", content.Id); + if (Creating.IsRaisedEventCancelled(new NewEventArgs(content, contentTypeAlias, parentId), this)) { @@ -191,6 +193,8 @@ namespace Umbraco.Core.Services /// public IContent CreateContent(string name, IContent parent, string contentTypeAlias, int userId = 0) { + if (parent == null) throw new ArgumentNullException("parent"); + var contentType = FindContentTypeByAlias(contentTypeAlias); var content = new Content(name, parent, contentType); content.Path = string.Concat(parent.Path, ",", content.Id); @@ -227,8 +231,7 @@ namespace Umbraco.Core.Services public IContent CreateContentWithIdentity(string name, int parentId, string contentTypeAlias, int userId = 0) { var contentType = FindContentTypeByAlias(contentTypeAlias); - var content = new Content(name, parentId, contentType); - content.Path = string.Concat(content.Parent().IfNotNull(x => x.Path, content.ParentId.ToString()), ",", content.Id); + var content = new Content(name, parentId, contentType); //NOTE: I really hate the notion of these Creating/Created events - they are so inconsistent, I've only just found // out that in these 'WithIdentity' methods, the Saving/Saved events were not fired, wtf. Anyways, they're added now. diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index 50153d4163..f4755f281d 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -63,7 +63,8 @@ namespace Umbraco.Core.Services { var mediaType = FindMediaTypeByAlias(mediaTypeAlias); var media = new Models.Media(name, parentId, mediaType); - media.Path = string.Concat(media.Parent().IfNotNull(x => x.Path, media.ParentId.ToString()), ",", media.Id); + var parent = GetById(media.ParentId); + media.Path = string.Concat(parent.IfNotNull(x => x.Path, media.ParentId.ToString()), ",", media.Id); if (Creating.IsRaisedEventCancelled(new NewEventArgs(media, mediaTypeAlias, parentId), this)) { @@ -96,6 +97,8 @@ namespace Umbraco.Core.Services /// public IMedia CreateMedia(string name, IMedia parent, string mediaTypeAlias, int userId = 0) { + if (parent == null) throw new ArgumentNullException("parent"); + var mediaType = FindMediaTypeByAlias(mediaTypeAlias); var media = new Models.Media(name, parent, mediaType); media.Path = string.Concat(parent.Path, ",", media.Id); @@ -132,7 +135,6 @@ namespace Umbraco.Core.Services { var mediaType = FindMediaTypeByAlias(mediaTypeAlias); var media = new Models.Media(name, parentId, mediaType); - media.Path = string.Concat(media.Parent().IfNotNull(x => x.Path, media.ParentId.ToString()), ",", media.Id); //NOTE: I really hate the notion of these Creating/Created events - they are so inconsistent, I've only just found // out that in these 'WithIdentity' methods, the Saving/Saved events were not fired, wtf. Anyways, they're added now.