From 051be40eb00d3fe8c008f641aa26e980acd23c0f Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska Date: Thu, 6 May 2021 10:57:30 +0200 Subject: [PATCH] Introduce GetEmptyByKeys and add explicit scope so everything is handled in 1 scope --- src/Umbraco.Web/Editors/ContentController.cs | 61 ++++++++++++++++---- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index edc5077558..32c5750d69 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -38,6 +38,7 @@ using Umbraco.Core.Persistence; using Umbraco.Core.Security; using Umbraco.Web.Routing; using Umbraco.Core.Collections; +using Umbraco.Core.Scoping; namespace Umbraco.Web.Editors { @@ -55,14 +56,19 @@ namespace Umbraco.Web.Editors { private readonly PropertyEditorCollection _propertyEditors; private readonly Lazy> _allLangs; + private readonly IScopeProvider _scopeProvider; public object Domains { get; private set; } - public ContentController(PropertyEditorCollection propertyEditors, IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) + public ContentController(PropertyEditorCollection propertyEditors, IGlobalSettings globalSettings, + IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, + AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper, + IScopeProvider scopeProvider) : base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoHelper) { _propertyEditors = propertyEditors ?? throw new ArgumentNullException(nameof(propertyEditors)); _allLangs = new Lazy>(() => Services.LocalizationService.GetAllLanguages().ToDictionary(x => x.IsoCode, x => x, StringComparer.InvariantCultureIgnoreCase)); + _scopeProvider = scopeProvider; } /// @@ -97,7 +103,7 @@ namespace Umbraco.Web.Editors /// /// [FilterAllowedOutgoingContent(typeof(IEnumerable))] - public IEnumerable GetByIds([FromUri]int[] ids) + public IEnumerable GetByIds([FromUri] int[] ids) { var foundContent = Services.ContentService.GetByIds(ids); return foundContent.Select(MapToDisplay); @@ -367,13 +373,17 @@ namespace Umbraco.Web.Editors [OutgoingEditorModelEvent] public ContentItemDisplay GetEmptyByKey(Guid contentTypeKey, int parentId) { - var contentType = Services.ContentTypeService.Get(contentTypeKey); - if (contentType == null) + using (var scope = _scopeProvider.CreateScope()) { - throw new HttpResponseException(HttpStatusCode.NotFound); - } + var contentType = Services.ContentTypeService.Get(contentTypeKey); + if (contentType == null) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + scope.Complete(); - return GetEmpty(contentType, parentId); + return GetEmpty(contentType, parentId); + } } private ContentItemDisplay GetEmpty(IContentType contentType, int parentId) @@ -392,6 +402,37 @@ namespace Umbraco.Web.Editors return mapped; } + /// + /// Gets a collection of empty content items for all document types. + /// + /// + /// + [OutgoingEditorModelEvent] + public IDictionary GetEmptyByKeys([FromUri] Guid[] contentTypeKeys, [FromUri] int parentId) + { + var result = new Dictionary(); + + using (var scope = _scopeProvider.CreateScope()) + { + var contentTypes = Services.ContentTypeService.GetAll(contentTypeKeys).ToList(); + + if (contentTypes.Any(contentType => contentType == null)) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + + foreach (var contentTypeKey in contentTypeKeys) + { + var contentType = contentTypes.First(c => c.Key == contentTypeKey); + result.Add(contentTypeKey, GetEmpty(contentType, parentId)); + } + + scope.Complete(); + } + + return result; + } + [OutgoingEditorModelEvent] public ContentItemDisplay GetEmpty(int blueprintId, int parentId) { @@ -550,7 +591,7 @@ namespace Umbraco.Web.Editors /// The name of the blueprint /// [HttpPost] - public SimpleNotificationModel CreateBlueprintFromContent([FromUri]int contentId, [FromUri]string name) + public SimpleNotificationModel CreateBlueprintFromContent([FromUri] int contentId, [FromUri] string name) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(name)); @@ -1432,7 +1473,7 @@ namespace Umbraco.Web.Editors /// private string GetVariantName(string culture, string segment) { - if(culture.IsNullOrWhiteSpace() && segment.IsNullOrWhiteSpace()) + if (culture.IsNullOrWhiteSpace() && segment.IsNullOrWhiteSpace()) { // TODO: Get name for default variant from somewhere? return "Default"; @@ -2436,7 +2477,7 @@ namespace Umbraco.Web.Editors // set up public access using role based access [EnsureUserPermissionForContent("contentId", ActionProtect.ActionLetter)] [HttpPost] - public HttpResponseMessage PostPublicAccess(int contentId, [FromUri]string[] groups, [FromUri]string[] usernames, int loginPageId, int errorPageId) + public HttpResponseMessage PostPublicAccess(int contentId, [FromUri] string[] groups, [FromUri] string[] usernames, int loginPageId, int errorPageId) { if ((groups == null || groups.Any() == false) && (usernames == null || usernames.Any() == false)) {