diff --git a/src/Umbraco.Cms.Api.Management/Controllers/DataType/Tree/DataTypeTreeControllerBase.cs b/src/Umbraco.Cms.Api.Management/Controllers/DataType/Tree/DataTypeTreeControllerBase.cs index 90d2669b7f40..52a7319b1a11 100644 --- a/src/Umbraco.Cms.Api.Management/Controllers/DataType/Tree/DataTypeTreeControllerBase.cs +++ b/src/Umbraco.Cms.Api.Management/Controllers/DataType/Tree/DataTypeTreeControllerBase.cs @@ -40,9 +40,11 @@ protected override Ordering ItemOrdering protected override DataTypeTreeItemResponseModel[] MapTreeItemViewModels(Guid? parentId, IEntitySlim[] entities) { - var dataTypes = _dataTypeService - .GetAllAsync(entities.Select(entity => entity.Key).ToArray()).GetAwaiter().GetResult() - .ToDictionary(contentType => contentType.Id); + Dictionary dataTypes = entities.Any() + ? _dataTypeService + .GetAllAsync(entities.Select(entity => entity.Key).ToArray()).GetAwaiter().GetResult() + .ToDictionary(contentType => contentType.Id) + : new Dictionary(); return entities.Select(entity => { diff --git a/src/Umbraco.Core/Services/ContentTypeEditing/ContentTypeEditingServiceBase.cs b/src/Umbraco.Core/Services/ContentTypeEditing/ContentTypeEditingServiceBase.cs index 80a574537fc2..ed818dc71b33 100644 --- a/src/Umbraco.Core/Services/ContentTypeEditing/ContentTypeEditingServiceBase.cs +++ b/src/Umbraco.Core/Services/ContentTypeEditing/ContentTypeEditingServiceBase.cs @@ -658,7 +658,12 @@ private Guid[] GetDataTypeKeys(ContentTypeEditingModelBase model.Properties.Select(property => property.DataTypeKey).Distinct().ToArray(); private async Task GetDataTypesAsync(ContentTypeEditingModelBase model) - => (await _dataTypeService.GetAllAsync(GetDataTypeKeys(model))).ToArray(); + { + Guid[] dataTypeKeys = GetDataTypeKeys(model); + return dataTypeKeys.Any() + ? (await _dataTypeService.GetAllAsync(GetDataTypeKeys(model))).ToArray() + : Array.Empty(); + } private int? GetParentId(ContentTypeEditingModelBase model, Guid? containerKey) { diff --git a/src/Umbraco.Core/Services/DataTypeService.cs b/src/Umbraco.Core/Services/DataTypeService.cs index 83a9f033d1dd..5564802f3c6c 100644 --- a/src/Umbraco.Core/Services/DataTypeService.cs +++ b/src/Umbraco.Core/Services/DataTypeService.cs @@ -1,5 +1,4 @@ using System.ComponentModel.DataAnnotations; -using System.Reflection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Umbraco.Cms.Core.DependencyInjection; @@ -14,7 +13,6 @@ using Umbraco.Cms.Core.Scoping; using Umbraco.Cms.Core.Services.OperationStatus; using Umbraco.Extensions; -using DataType = Umbraco.Cms.Core.Models.DataType; namespace Umbraco.Cms.Core.Services.Implement { @@ -224,27 +222,29 @@ public IEnumerable GetContainers(int[] containerIds) => GetAsync(name).GetAwaiter().GetResult(); /// - public async Task GetAsync(string name) + public Task GetAsync(string name) { using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); IDataType? dataType = _dataTypeRepository.Get(Query().Where(x => x.Name == name))?.FirstOrDefault(); ConvertMissingEditorOfDataTypeToLabel(dataType); - return await Task.FromResult(dataType); + + return Task.FromResult(dataType); } /// public Task> GetAllAsync(params Guid[] keys) { - // Nothing requested, return nothing - if (keys.Any() is false) + using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); + + IQuery query = Query(); + if (keys.Length > 0) { - return Task.FromResult(Enumerable.Empty()); + query = query.Where(x => keys.Contains(x.Key)); } - using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); - - IDataType[] dataTypes = _dataTypeRepository.Get(Query().Where(x => keys.Contains(x.Key))).ToArray(); + IDataType[] dataTypes = _dataTypeRepository.Get(query).ToArray(); ConvertMissingEditorsOfDataTypesToLabels(dataTypes); + return Task.FromResult>(dataTypes); } @@ -288,6 +288,7 @@ public Task> FilterAsync(string? name = null, string? edit using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); IDataType? dataType = _dataTypeRepository.Get(id); ConvertMissingEditorOfDataTypeToLabel(dataType); + return dataType; } @@ -301,12 +302,13 @@ public Task> FilterAsync(string? name = null, string? edit => GetAsync(id).GetAwaiter().GetResult(); /// - public async Task GetAsync(Guid id) + public Task GetAsync(Guid id) { using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); IDataType? dataType = GetDataTypeFromRepository(id); ConvertMissingEditorOfDataTypeToLabel(dataType); - return await Task.FromResult(dataType); + + return Task.FromResult(dataType); } /// @@ -319,23 +321,25 @@ public IEnumerable GetByEditorAlias(string propertyEditorAlias) => GetByEditorAliasAsync(propertyEditorAlias).GetAwaiter().GetResult(); /// - public async Task> GetByEditorAliasAsync(string propertyEditorAlias) + public Task> GetByEditorAliasAsync(string propertyEditorAlias) { using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); IQuery query = Query().Where(x => x.EditorAlias == propertyEditorAlias); IEnumerable dataTypes = _dataTypeRepository.Get(query).ToArray(); ConvertMissingEditorsOfDataTypesToLabels(dataTypes); - return await Task.FromResult(dataTypes); + + return Task.FromResult(dataTypes); } /// - public async Task> GetByEditorUiAlias(string editorUiAlias) + public Task> GetByEditorUiAlias(string editorUiAlias) { using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); IQuery query = Query().Where(x => x.EditorUiAlias == editorUiAlias); IEnumerable dataTypes = _dataTypeRepository.Get(query).ToArray(); ConvertMissingEditorsOfDataTypesToLabels(dataTypes); - return await Task.FromResult(dataTypes); + + return Task.FromResult(dataTypes); } /// @@ -347,8 +351,8 @@ public IEnumerable GetAll(params int[] ids) { using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); IEnumerable dataTypes = _dataTypeRepository.GetMany(ids).ToArray(); - ConvertMissingEditorsOfDataTypesToLabels(dataTypes); + return dataTypes; } @@ -366,8 +370,7 @@ private void ConvertMissingEditorsOfDataTypesToLabels(IEnumerable dat { // Any data types that don't have an associated editor are created of a specific type. // We convert them to labels to make clear to the user why the data type cannot be used. - IEnumerable dataTypesWithMissingEditors = dataTypes - .Where(x => x.Editor is MissingPropertyEditor); + IEnumerable dataTypesWithMissingEditors = dataTypes.Where(x => x.Editor is MissingPropertyEditor); foreach (IDataType dataType in dataTypesWithMissingEditors) { dataType.Editor = new LabelPropertyEditor(_dataValueEditorFactory, _ioHelper); @@ -398,7 +401,7 @@ private void ConvertMissingEditorsOfDataTypesToLabels(IEnumerable dat DataTypeOperationStatus.Success => OperationResult.Attempt.Succeed(MoveOperationStatusType.Success, evtMsgs), DataTypeOperationStatus.CancelledByNotification => OperationResult.Attempt.Fail(MoveOperationStatusType.FailedCancelledByEvent, evtMsgs), DataTypeOperationStatus.ParentNotFound => OperationResult.Attempt.Fail(MoveOperationStatusType.FailedParentNotFound, evtMsgs), - _ => OperationResult.Attempt.Fail(MoveOperationStatusType.FailedNotAllowedByPath, evtMsgs, new InvalidOperationException($"Invalid operation status: {result.Status}")), + _ => OperationResult.Attempt.Fail(MoveOperationStatusType.FailedNotAllowedByPath, evtMsgs, new InvalidOperationException($"Invalid operation status: {result.Status}")), }; } @@ -448,9 +451,7 @@ public async Task> MoveAsync(IDataTy [Obsolete("Use the method which specifies the userId parameter")] public Attempt?> Copy(IDataType copying, int containerId) - { - return Copy(copying, containerId, Constants.Security.SuperUserId); - } + => Copy(copying, containerId, Constants.Security.SuperUserId); public Attempt?> Copy(IDataType copying, int containerId, int userId = Constants.Security.SuperUserId) { @@ -671,7 +672,6 @@ public void Delete(IDataType dataType, int userId = Constants.Security.SuperUser scope.Notifications.Publish(new DataTypeDeletedNotification(dataType, eventMessages).WithStateFrom(deletingDataTypeNotification)); - var currentUserId = await _userIdKeyResolver.GetAsync(userKey); Audit(AuditType.Delete, currentUserId, dataType.Id);