Request datatype by guid (#6847)

This commit is contained in:
Bjarne Fyrstenborg
2020-01-08 19:02:16 +01:00
committed by Kenn Jacobsen
parent 2908ecac64
commit 0dfd7679e0
4 changed files with 144 additions and 33 deletions

View File

@@ -303,7 +303,7 @@ namespace Umbraco.Web.Editors
}
/// <summary>
/// Gets the content json for the content id
/// Gets the content json for the content guid
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
@@ -323,7 +323,7 @@ namespace Umbraco.Web.Editors
}
/// <summary>
/// Gets the content json for the content id
/// Gets the content json for the content udi
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
@@ -341,7 +341,7 @@ namespace Umbraco.Web.Editors
}
/// <summary>
/// Gets an empty content item for the
/// Gets an empty content item for the document type.
/// </summary>
/// <param name="contentTypeAlias"></param>
/// <param name="parentId"></param>

View File

@@ -20,6 +20,7 @@ using Umbraco.Web.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using System.Web.Http.Controllers;
namespace Umbraco.Web.Editors
{
@@ -34,6 +35,7 @@ namespace Umbraco.Web.Editors
[PluginController("UmbracoApi")]
[UmbracoTreeAuthorize(Constants.Trees.DataTypes, Constants.Trees.DocumentTypes, Constants.Trees.MediaTypes, Constants.Trees.MemberTypes)]
[EnableOverrideAuthorization]
[DataTypeControllerConfiguration]
public class DataTypeController : BackOfficeNotificationsController
{
private readonly PropertyEditorCollection _propertyEditors;
@@ -44,6 +46,19 @@ namespace Umbraco.Web.Editors
_propertyEditors = propertyEditors;
}
/// <summary>
/// Configures this controller with a custom action selector
/// </summary>
private class DataTypeControllerConfigurationAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
controllerSettings.Services.Replace(typeof(IHttpActionSelector), new ParameterSwapControllerActionSelector(
new ParameterSwapControllerActionSelector.ParameterSwapInfo("GetById", "id", typeof(int), typeof(Guid), typeof(Udi))
));
}
}
/// <summary>
/// Gets data type by name
/// </summary>
@@ -70,6 +85,40 @@ namespace Umbraco.Web.Editors
return Mapper.Map<IDataType, DataTypeDisplay>(dataType);
}
/// <summary>
/// Gets the datatype json for the datatype guid
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataTypeDisplay GetById(Guid id)
{
var dataType = Services.DataTypeService.GetDataType(id);
if (dataType == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return Mapper.Map<IDataType, DataTypeDisplay>(dataType);
}
/// <summary>
/// Gets the datatype json for the datatype udi
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataTypeDisplay GetById(Udi id)
{
var guidUdi = id as GuidUdi;
if (guidUdi == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
var dataType = Services.DataTypeService.GetDataType(guidUdi.Guid);
if (dataType == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return Mapper.Map<IDataType, DataTypeDisplay>(dataType);
}
/// <summary>
/// Deletes a data type with a given ID
/// </summary>

View File

@@ -19,6 +19,7 @@ using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;
using Umbraco.Web.WebApi.Filters;
using Constants = Umbraco.Core.Constants;
using System.Web.Http.Controllers;
namespace Umbraco.Web.Editors
{
@@ -28,6 +29,7 @@ namespace Umbraco.Web.Editors
/// </summary>
[PluginController("UmbracoApi")]
[UmbracoTreeAuthorize(Constants.Trees.Macros)]
[MacrosControllerConfiguration]
public class MacrosController : BackOfficeNotificationsController
{
private readonly IMacroService _macroService;
@@ -38,6 +40,19 @@ namespace Umbraco.Web.Editors
_macroService = Services.MacroService;
}
/// <summary>
/// Configures this controller with a custom action selector
/// </summary>
private class MacrosControllerConfigurationAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
controllerSettings.Services.Replace(typeof(IHttpActionSelector), new ParameterSwapControllerActionSelector(
new ParameterSwapControllerActionSelector.ParameterSwapInfo("GetById", "id", typeof(int), typeof(Guid), typeof(Udi))
));
}
}
/// <summary>
/// Creates a new macro
/// </summary>
@@ -97,39 +112,43 @@ namespace Umbraco.Web.Editors
return this.ReturnErrorResponse($"Macro with id {id} does not exist");
}
var macroDisplay = new MacroDisplay
{
Alias = macro.Alias,
Id = macro.Id,
Key = macro.Key,
Name = macro.Name,
CacheByPage = macro.CacheByPage,
CacheByUser = macro.CacheByMember,
CachePeriod = macro.CacheDuration,
View = macro.MacroSource,
RenderInEditor = !macro.DontRender,
UseInEditor = macro.UseInEditor,
Path = $"-1,{macro.Id}"
};
var parameters = new List<MacroParameterDisplay>();
foreach (var param in macro.Properties.Values.OrderBy(x => x.SortOrder))
{
parameters.Add(new MacroParameterDisplay
{
Editor = param.EditorAlias,
Key = param.Alias,
Label = param.Name,
Id = param.Id
});
}
macroDisplay.Parameters = parameters;
var macroDisplay = MapToDisplay(macro);
return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay);
}
[HttpGet]
public HttpResponseMessage GetById(Guid id)
{
var macro = _macroService.GetById(id);
if (macro == null)
{
return this.ReturnErrorResponse($"Macro with id {id} does not exist");
}
var macroDisplay = MapToDisplay(macro);
return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay);
}
[HttpGet]
public HttpResponseMessage GetById(Udi id)
{
var guidUdi = id as GuidUdi;
if (guidUdi == null)
this.ReturnErrorResponse($"Macro with id {id} does not exist");
var macro = _macroService.GetById(guidUdi.Guid);
if (macro == null)
{
return this.ReturnErrorResponse($"Macro with id {id} does not exist");
}
var macroDisplay = MapToDisplay(macro);
return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay);
}
[HttpPost]
public HttpResponseMessage DeleteById(int id)
@@ -384,5 +403,29 @@ namespace Umbraco.Web.Editors
return files;
}
/// <summary>
/// Used to map an <see cref="IMacro"/> instance to a <see cref="MacroDisplay"/>
/// </summary>
/// <param name="macro"></param>
/// <returns></returns>
private MacroDisplay MapToDisplay(IMacro macro)
{
var display = Mapper.Map<MacroDisplay>(macro);
var parameters = macro.Properties.Values
.OrderBy(x => x.SortOrder)
.Select(x => new MacroParameterDisplay()
{
Editor = x.EditorAlias,
Key = x.Alias,
Label = x.Name,
Id = x.Id
});
display.Parameters = parameters;
return display;
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Logging;
@@ -23,6 +24,7 @@ namespace Umbraco.Web.Models.Mapping
public void DefineMaps(UmbracoMapper mapper)
{
mapper.Define<IMacro, EntityBasic>((source, context) => new EntityBasic(), Map);
mapper.Define<IMacro, MacroDisplay>((source, context) => new MacroDisplay(), Map);
mapper.Define<IMacro, IEnumerable<MacroParameter>>((source, context) => context.MapEnumerable<IMacroProperty, MacroParameter>(source.Properties.Values));
mapper.Define<IMacroProperty, MacroParameter>((source, context) => new MacroParameter(), Map);
}
@@ -40,6 +42,23 @@ namespace Umbraco.Web.Models.Mapping
target.Udi = Udi.Create(Constants.UdiEntityType.Macro, source.Key);
}
private void Map(IMacro source, MacroDisplay target, MapperContext context)
{
target.Alias = source.Alias;
target.Icon = Constants.Icons.Macro;
target.Id = source.Id;
target.Key = source.Key;
target.Name = source.Name;
target.ParentId = -1;
target.Path = "-1," + source.Id;
target.Udi = Udi.Create(Constants.UdiEntityType.Macro, source.Key);
target.CacheByPage = source.CacheByPage;
target.CacheByUser = source.CacheByMember;
target.CachePeriod = source.CacheDuration;
target.UseInEditor = source.UseInEditor;
target.RenderInEditor = !source.DontRender;
target.View = source.MacroSource;
}
// Umbraco.Code.MapAll -Value
private void Map(IMacroProperty source, MacroParameter target, MapperContext context)
{