Skip to content

Commit

Permalink
v14: Return all data types from DataTypeService.GetAllAsync() (#16230)
Browse files Browse the repository at this point in the history
* Return all data types from GetAllAsync

* Remove unnecessary async/awaits

* Ensure we don't accidentally fetch all data types when we mean to fetch none.

---------

Co-authored-by: kjac <kja@umbraco.dk>
  • Loading branch information
ronaldbarendse and kjac authored May 10, 2024
1 parent 78d93a1 commit c7328bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, IDataType> dataTypes = entities.Any()
? _dataTypeService
.GetAllAsync(entities.Select(entity => entity.Key).ToArray()).GetAwaiter().GetResult()
.ToDictionary(contentType => contentType.Id)
: new Dictionary<int, IDataType>();

return entities.Select(entity =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,12 @@ private Guid[] GetDataTypeKeys(ContentTypeEditingModelBase<TPropertyTypeModel, T
=> model.Properties.Select(property => property.DataTypeKey).Distinct().ToArray();

private async Task<IDataType[]> GetDataTypesAsync(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model)
=> (await _dataTypeService.GetAllAsync(GetDataTypeKeys(model))).ToArray();
{
Guid[] dataTypeKeys = GetDataTypeKeys(model);
return dataTypeKeys.Any()
? (await _dataTypeService.GetAllAsync(GetDataTypeKeys(model))).ToArray()
: Array.Empty<IDataType>();
}

private int? GetParentId(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, Guid? containerKey)
{
Expand Down
48 changes: 24 additions & 24 deletions src/Umbraco.Core/Services/DataTypeService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.DependencyInjection;
Expand All @@ -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
{
Expand Down Expand Up @@ -224,27 +222,29 @@ public IEnumerable<EntityContainer> GetContainers(int[] containerIds)
=> GetAsync(name).GetAwaiter().GetResult();

/// <inheritdoc />
public async Task<IDataType?> GetAsync(string name)
public Task<IDataType?> GetAsync(string name)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
IDataType? dataType = _dataTypeRepository.Get(Query<IDataType>().Where(x => x.Name == name))?.FirstOrDefault();
ConvertMissingEditorOfDataTypeToLabel(dataType);
return await Task.FromResult(dataType);

return Task.FromResult(dataType);
}

/// <inheritdoc />
public Task<IEnumerable<IDataType>> GetAllAsync(params Guid[] keys)
{
// Nothing requested, return nothing
if (keys.Any() is false)
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);

IQuery<IDataType> query = Query<IDataType>();
if (keys.Length > 0)
{
return Task.FromResult(Enumerable.Empty<IDataType>());
query = query.Where(x => keys.Contains(x.Key));
}

using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);

IDataType[] dataTypes = _dataTypeRepository.Get(Query<IDataType>().Where(x => keys.Contains(x.Key))).ToArray();
IDataType[] dataTypes = _dataTypeRepository.Get(query).ToArray();
ConvertMissingEditorsOfDataTypesToLabels(dataTypes);

return Task.FromResult<IEnumerable<IDataType>>(dataTypes);
}

Expand Down Expand Up @@ -288,6 +288,7 @@ public Task<PagedModel<IDataType>> FilterAsync(string? name = null, string? edit
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
IDataType? dataType = _dataTypeRepository.Get(id);
ConvertMissingEditorOfDataTypeToLabel(dataType);

return dataType;
}

Expand All @@ -301,12 +302,13 @@ public Task<PagedModel<IDataType>> FilterAsync(string? name = null, string? edit
=> GetAsync(id).GetAwaiter().GetResult();

/// <inheritdoc />
public async Task<IDataType?> GetAsync(Guid id)
public Task<IDataType?> 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);
}

/// <summary>
Expand All @@ -319,23 +321,25 @@ public IEnumerable<IDataType> GetByEditorAlias(string propertyEditorAlias)
=> GetByEditorAliasAsync(propertyEditorAlias).GetAwaiter().GetResult();

/// <inheritdoc />
public async Task<IEnumerable<IDataType>> GetByEditorAliasAsync(string propertyEditorAlias)
public Task<IEnumerable<IDataType>> GetByEditorAliasAsync(string propertyEditorAlias)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
IQuery<IDataType> query = Query<IDataType>().Where(x => x.EditorAlias == propertyEditorAlias);
IEnumerable<IDataType> dataTypes = _dataTypeRepository.Get(query).ToArray();
ConvertMissingEditorsOfDataTypesToLabels(dataTypes);
return await Task.FromResult(dataTypes);

return Task.FromResult(dataTypes);
}

/// <inheritdoc />
public async Task<IEnumerable<IDataType>> GetByEditorUiAlias(string editorUiAlias)
public Task<IEnumerable<IDataType>> GetByEditorUiAlias(string editorUiAlias)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
IQuery<IDataType> query = Query<IDataType>().Where(x => x.EditorUiAlias == editorUiAlias);
IEnumerable<IDataType> dataTypes = _dataTypeRepository.Get(query).ToArray();
ConvertMissingEditorsOfDataTypesToLabels(dataTypes);
return await Task.FromResult(dataTypes);

return Task.FromResult(dataTypes);
}

/// <summary>
Expand All @@ -347,8 +351,8 @@ public IEnumerable<IDataType> GetAll(params int[] ids)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
IEnumerable<IDataType> dataTypes = _dataTypeRepository.GetMany(ids).ToArray();

ConvertMissingEditorsOfDataTypesToLabels(dataTypes);

return dataTypes;
}

Expand All @@ -366,8 +370,7 @@ private void ConvertMissingEditorsOfDataTypesToLabels(IEnumerable<IDataType> 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<IDataType> dataTypesWithMissingEditors = dataTypes
.Where(x => x.Editor is MissingPropertyEditor);
IEnumerable<IDataType> dataTypesWithMissingEditors = dataTypes.Where(x => x.Editor is MissingPropertyEditor);
foreach (IDataType dataType in dataTypesWithMissingEditors)
{
dataType.Editor = new LabelPropertyEditor(_dataValueEditorFactory, _ioHelper);
Expand Down Expand Up @@ -398,7 +401,7 @@ private void ConvertMissingEditorsOfDataTypesToLabels(IEnumerable<IDataType> 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>(MoveOperationStatusType.FailedNotAllowedByPath, evtMsgs, new InvalidOperationException($"Invalid operation status: {result.Status}")),
_ => OperationResult.Attempt.Fail<MoveOperationStatusType>(MoveOperationStatusType.FailedNotAllowedByPath, evtMsgs, new InvalidOperationException($"Invalid operation status: {result.Status}")),
};
}

Expand Down Expand Up @@ -448,9 +451,7 @@ public async Task<Attempt<IDataType, DataTypeOperationStatus>> MoveAsync(IDataTy

[Obsolete("Use the method which specifies the userId parameter")]
public Attempt<OperationResult<MoveOperationStatusType, IDataType>?> Copy(IDataType copying, int containerId)
{
return Copy(copying, containerId, Constants.Security.SuperUserId);
}
=> Copy(copying, containerId, Constants.Security.SuperUserId);

public Attempt<OperationResult<MoveOperationStatusType, IDataType>?> Copy(IDataType copying, int containerId, int userId = Constants.Security.SuperUserId)
{
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit c7328bc

Please sign in to comment.