From 2a4d4c82ff47f4c6cdb8b4d67666b461f78c2293 Mon Sep 17 00:00:00 2001 From: Bjarne Fyrstenborg Date: Mon, 27 Jan 2020 22:47:06 +0100 Subject: [PATCH] Get relation type by id, guid and udi --- .../Editors/RelationTypeController.cs | 55 +++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Web/Editors/RelationTypeController.cs b/src/Umbraco.Web/Editors/RelationTypeController.cs index f12faf77cc..3e45600be9 100644 --- a/src/Umbraco.Web/Editors/RelationTypeController.cs +++ b/src/Umbraco.Web/Editors/RelationTypeController.cs @@ -17,6 +17,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 { @@ -26,6 +27,7 @@ namespace Umbraco.Web.Editors [PluginController("UmbracoApi")] [UmbracoTreeAuthorize(Constants.Trees.RelationTypes)] [EnableOverrideAuthorization] + [RelationTypeControllerConfiguration] public class RelationTypeController : BackOfficeNotificationsController { public RelationTypeController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) @@ -34,22 +36,65 @@ namespace Umbraco.Web.Editors } /// - /// Gets a relation type by ID. + /// Configures this controller with a custom action selector + /// + private class RelationTypeControllerConfigurationAttribute : 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)) + )); + } + } + + /// + /// Gets a relation type by id /// /// The relation type ID. /// Returns the . public RelationTypeDisplay GetById(int id) { var relationType = Services.RelationService.GetRelationTypeById(id); - if (relationType == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } + return Mapper.Map(relationType); + } - var display = Mapper.Map(relationType); - - return display; + /// + /// Gets a relation type by guid + /// + /// The relation type ID. + /// Returns the . + public RelationTypeDisplay GetById(Guid id) + { + var relationType = Services.RelationService.GetRelationTypeById(id); + if (relationType == null) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + return Mapper.Map(relationType); + } + + /// + /// Gets a relation type by udi + /// + /// The relation type ID. + /// Returns the . + public RelationTypeDisplay GetById(Udi id) + { + var guidUdi = id as GuidUdi; + if (guidUdi == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + + var relationType = Services.RelationService.GetRelationTypeById(guidUdi.Guid); + if (relationType == null) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + return Mapper.Map(relationType); } public PagedResult GetPagedResults(int id, int pageNumber = 1, int pageSize = 100)