diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index 903446d23c..63b137c2d1 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -1,8 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; +using System.Web.Http.Controllers; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Configuration; @@ -13,6 +15,7 @@ using Umbraco.Core.Persistence; using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Mvc; +using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; using Constants = Umbraco.Core.Constants; @@ -20,6 +23,7 @@ namespace Umbraco.Web.Editors { [PluginController("UmbracoApi")] [UmbracoTreeAuthorize(Constants.Trees.Templates)] + [TemplateControllerConfiguration] public class TemplateController : BackOfficeNotificationsController { public TemplateController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoHelper umbracoHelper) @@ -27,6 +31,19 @@ namespace Umbraco.Web.Editors { } + /// + /// Configures this controller with a custom action selector + /// + private class TemplateControllerConfigurationAttribute : 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 data type by alias /// @@ -48,7 +65,7 @@ namespace Umbraco.Web.Editors } /// - /// Gets the content json for the content id + /// Gets the template json for the template id /// /// /// @@ -61,6 +78,40 @@ namespace Umbraco.Web.Editors return Mapper.Map(template); } + /// + /// Gets the template json for the template guid + /// + /// + /// + public TemplateDisplay GetById(Guid id) + { + var template = Services.FileService.GetTemplate(id); + if (template == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + + return Mapper.Map(template); + } + + /// + /// Gets the template json for the template udi + /// + /// + /// + public TemplateDisplay GetById(Udi id) + { + var guidUdi = id as GuidUdi; + if (guidUdi == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + + var template = Services.FileService.GetTemplate(guidUdi.Guid); + if (template == null) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + + return Mapper.Map(template); + } + /// /// Deletes a template with a given ID ///