Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datatype configuration refactor #13605

Merged
merged 14 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.ViewModels.DataType;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.DataType;

public class ByKeyDataTypeController : DataTypeControllerBase
{
private readonly IDataTypeService _dataTypeService;
private readonly IUmbracoMapper _umbracoMapper;

public ByKeyDataTypeController(IDataTypeService dataTypeService, IUmbracoMapper umbracoMapper)
{
_dataTypeService = dataTypeService;
_umbracoMapper = umbracoMapper;
}

[HttpGet("{key:guid}")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(DataTypeViewModel), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(NotFoundResult), StatusCodes.Status404NotFound)]
public async Task<ActionResult<DataTypeViewModel>> ByKey(Guid key)
{
IDataType? dataType = _dataTypeService.GetDataType(key);
if (dataType == null)
{
return NotFound();
}

return await Task.FromResult(Ok(_umbracoMapper.Map<DataTypeViewModel>(dataType)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.ViewModels.DataType;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.DataType;

public class CreateDataTypeController : DataTypeControllerBase
{
private readonly IDataTypeService _dataTypeService;
private readonly IUmbracoMapper _umbracoMapper;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;

public CreateDataTypeController(IDataTypeService dataTypeService, IUmbracoMapper umbracoMapper, IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
{
_dataTypeService = dataTypeService;
_umbracoMapper = umbracoMapper;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
}

[HttpPost]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<DataTypeViewModel>> Create(DataTypeCreateModel dataTypeCreateModel)
{
IDataType? created = _umbracoMapper.Map<IDataType>(dataTypeCreateModel);
if (created == null)
{
return BadRequest("Could not map the POSTed model to a datatype");
}

IUser? currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
_dataTypeService.Save(created, currentUser?.Id ?? Constants.Security.SuperUserId);

return await Task.FromResult(Ok(_umbracoMapper.Map<DataTypeViewModel>(created)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Routing;
using Umbraco.Cms.Core;

namespace Umbraco.Cms.Api.Management.Controllers.DataType;

[ApiController]
[VersionedApiBackOfficeRoute(Constants.UdiEntityType.DataType)]
[ApiExplorerSettings(GroupName = "Data Type")]
[ApiVersion("1.0")]
public abstract class DataTypeControllerBase : ManagementApiControllerBase
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.ViewModels.DataType;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.DataType;

public class UpdateDataTypeController : DataTypeControllerBase
{
private readonly IDataTypeService _dataTypeService;
private readonly IUmbracoMapper _umbracoMapper;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;

public UpdateDataTypeController(IDataTypeService dataTypeService, IUmbracoMapper umbracoMapper, IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
{
_dataTypeService = dataTypeService;
_umbracoMapper = umbracoMapper;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
}

[HttpPut("{key:guid}")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<DataTypeViewModel>> Update(Guid key, DataTypeUpdateModel dataTypeViewModel)
{
IDataType? current = _dataTypeService.GetDataType(key);
if (current == null)
{
return NotFound();
}

IDataType updated = _umbracoMapper.Map(dataTypeViewModel, current);

IUser? currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;
_dataTypeService.Save(updated, currentUser?.Id ?? Constants.Security.SuperUserId);

return await Task.FromResult(Ok(_umbracoMapper.Map<DataTypeViewModel>(updated)));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Api.Management.Mapping.Culture;
using Umbraco.Cms.Api.Management.Mapping.DataType;
using Umbraco.Cms.Api.Management.Mapping.Dictionary;
using Umbraco.Cms.Api.Management.Mapping.HealthCheck;
using Umbraco.Cms.Api.Management.Mapping.Installer;
Expand All @@ -23,7 +24,9 @@ internal static IUmbracoBuilder AddMappers(this IUmbracoBuilder builder)
.Add<LanguageViewModelsMapDefinition>()
.Add<InstallerViewModelsMapDefinition>()
.Add<CultureViewModelMapDefinition>()
.Add<HealthCheckViewModelsMapDefinition>();
.Add<HealthCheckViewModelsMapDefinition>()
.Add<CultureViewModelMapDefinition>()
.Add<DataTypeViewModelMapDefinition>();

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Umbraco.Cms.Api.Management.ViewModels.DataType;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Mapping.DataType;

public class DataTypeViewModelMapDefinition : IMapDefinition
{
private readonly PropertyEditorCollection _propertyEditors;
private readonly IConfigurationEditorJsonSerializer _serializer;
private readonly IDataTypeService _dataTypeService;

public DataTypeViewModelMapDefinition(
PropertyEditorCollection propertyEditors,
IConfigurationEditorJsonSerializer serializer,
IDataTypeService dataTypeService)
{
_propertyEditors = propertyEditors;
_serializer = serializer;
_dataTypeService = dataTypeService;
}

public void DefineMaps(IUmbracoMapper mapper)
{
mapper.Define<IDataType, DataTypeViewModel>((_, _) => new DataTypeViewModel(), Map);

mapper.Define<DataTypeUpdateModel, IDataType>(InitializeDataType, Map);

mapper.Define<DataTypeCreateModel, IDataType>(InitializeDataType, Map);

IDataType InitializeDataType<T>(T source, MapperContext _) where T : DataTypeModelBase =>
new Core.Models.DataType(_propertyEditors[source.PropertyEditorAlias], _serializer)
{
CreateDate = DateTime.Now
};
}

// Umbraco.Code.MapAll
private void Map(IDataType source, DataTypeViewModel target, MapperContext context)
{
target.Key = source.Key;
target.ParentKey = _dataTypeService.GetContainer(source.ParentId)?.Key;
target.Name = source.Name ?? string.Empty;
target.PropertyEditorAlias = source.EditorAlias;

IConfigurationEditor? configurationEditor = source.Editor?.GetConfigurationEditor();
IDictionary<string, object> configuration = configurationEditor?.ToConfigurationEditor(source.ConfigurationData)
?? new Dictionary<string, object>();

target.Data = configuration.Select(c =>
new DataTypePropertyViewModel
{
Alias = c.Key,
Value = c.Value
}).ToArray();
}

// Umbraco.Code.MapAll -CreateDate -DeleteDate -UpdateDate -ParentId
// Umbraco.Code.MapAll -Id -Key -Path -CreatorId -Level -SortOrder
private void Map(DataTypeUpdateModel source, IDataType target, MapperContext context)
{
IDataEditor editor = GetRequiredDataEditor(source);

target.Name = source.Name;
target.Editor = editor;
target.DatabaseType = GetEditorValueStorageType(editor);
target.ConfigurationData = MapConfigurationData(source, editor);
}

// Umbraco.Code.MapAll -CreateDate -DeleteDate -UpdateDate
// Umbraco.Code.MapAll -Id -Key -Path -CreatorId -Level -SortOrder
private void Map(DataTypeCreateModel source, IDataType target, MapperContext context)
{
IDataEditor editor = GetRequiredDataEditor(source);

target.Name = source.Name;
target.ParentId = MapParentId(source.ParentKey);
target.Editor = editor;
target.DatabaseType = GetEditorValueStorageType(editor);
target.ConfigurationData = MapConfigurationData(source, editor);
}

private IDataEditor GetRequiredDataEditor<T>(T source) where T : DataTypeModelBase
{
if (_propertyEditors.TryGet(source.PropertyEditorAlias, out IDataEditor? editor) == false)
{
throw new InvalidOperationException($"Could not find a property editor with alias \"{source.PropertyEditorAlias}\".");
}

return editor;
}

private ValueStorageType GetEditorValueStorageType(IDataEditor editor)
{
var valueType = editor.GetValueEditor().ValueType;
return ValueTypes.ToStorageType(valueType);
}

private IDictionary<string, object> MapConfigurationData<T>(T source, IDataEditor editor) where T : DataTypeModelBase
{
var configuration = source
.Data
.Where(p => p.Value is not null)
.ToDictionary(p => p.Alias, p => p.Value!);
IConfigurationEditor? configurationEditor = editor?.GetConfigurationEditor();
return configurationEditor?.FromConfigurationEditor(configuration)
?? new Dictionary<string, object>();
}

private int MapParentId(Guid? parentKey)
{
if (parentKey == null)
{
return Constants.System.Root;
}

EntityContainer? container = _dataTypeService.GetContainer(parentKey.Value);
return container?.Id ?? throw new InvalidOperationException($"Could not find a parent container with key \"{parentKey}\".");
}
}
Loading