From 4c330c865434a8b29b8c26ecf405dfbb6c331679 Mon Sep 17 00:00:00 2001 From: Niels Hartvig Date: Mon, 9 Jan 2017 22:54:28 +0100 Subject: [PATCH] Implements CodeFileController --- .../Editors/BackOfficeController.cs | 4 + src/Umbraco.Web/Editors/CodeFileController.cs | 155 ++++++++++++++++++ .../Models/ContentEditing/CodeFileDisplay.cs | 20 +++ .../Models/Mapping/CodeFileDisplayMapper.cs | 47 ++++++ src/Umbraco.Web/Umbraco.Web.csproj | 3 + 5 files changed, 229 insertions(+) create mode 100644 src/Umbraco.Web/Editors/CodeFileController.cs create mode 100644 src/Umbraco.Web/Models/ContentEditing/CodeFileDisplay.cs create mode 100644 src/Umbraco.Web/Models/Mapping/CodeFileDisplayMapper.cs diff --git a/src/Umbraco.Web/Editors/BackOfficeController.cs b/src/Umbraco.Web/Editors/BackOfficeController.cs index 199ffd9bed..50c8b2e82c 100644 --- a/src/Umbraco.Web/Editors/BackOfficeController.cs +++ b/src/Umbraco.Web/Editors/BackOfficeController.cs @@ -359,6 +359,10 @@ namespace Umbraco.Web.Editors { "healthCheckBaseUrl", Url.GetUmbracoApiServiceBaseUrl( controller => controller.GetAllHealthChecks()) + }, + { + "codeFileApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl( + controller => controller.GetByPath("", "")) } } }, diff --git a/src/Umbraco.Web/Editors/CodeFileController.cs b/src/Umbraco.Web/Editors/CodeFileController.cs new file mode 100644 index 0000000000..7c1e1d2430 --- /dev/null +++ b/src/Umbraco.Web/Editors/CodeFileController.cs @@ -0,0 +1,155 @@ +using AutoMapper; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Web.Http; +using Umbraco.Core.Models; +using Umbraco.Web.Models.ContentEditing; +using Umbraco.Web.Mvc; +using Umbraco.Web.WebApi; + +namespace Umbraco.Web.Editors +{ + [PluginController("UmbracoApi")] + public class CodeFileController : UmbracoAuthorizedJsonController + { + + public HttpResponseMessage PostCreate(string type, CodeFileDisplay display) + { + switch (type) + { + case "partialView": + var view = new PartialView(display.VirtualPath); + var result = Services.FileService.CreatePartialView(view, display.Snippet, Security.CurrentUser.Id); + return result.Success == true ? Request.CreateResponse(HttpStatusCode.OK, result.Result) : Request.CreateNotificationValidationErrorResponse(result.Exception.Message); + case "partialViewMacro": + var viewMacro = new PartialView(display.VirtualPath); + var resultMacro = Services.FileService.CreatePartialViewMacro(viewMacro, display.Snippet, Security.CurrentUser.Id); + return resultMacro.Success == true ? Request.CreateResponse(HttpStatusCode.OK, resultMacro.Result) : Request.CreateNotificationValidationErrorResponse(resultMacro.Exception.Message); + case "script": + var script = new Script(display.VirtualPath); + Services.FileService.SaveScript(script, Security.CurrentUser.Id); + return Request.CreateResponse(HttpStatusCode.OK); + default: + throw new ArgumentException("File Type not supported", "type"); + } + } + + + public CodeFileDisplay GetByPath(string type, string virtualPath) + { + switch (type) + { + case "partialView": + var view = Services.FileService.GetPartialView(virtualPath); + return view == null ? null : Mapper.Map(view); + case "partialViewMacro": + var viewMacro = Services.FileService.GetPartialViewMacro(virtualPath); + return viewMacro == null ? null : Mapper.Map(viewMacro); + case "script": + var script = Services.FileService.GetScriptByName(virtualPath); + return script == null ? null : Mapper.Map(script); + default: + throw new ArgumentException("File Type not supported", "type"); + } + } + + [HttpDelete] + public HttpResponseMessage Delete(string type, string virtualPath) + { + switch (type) + { + case "partialView": + if (Services.FileService.DeletePartialView(virtualPath, Security.CurrentUser.Id)) + { + return Request.CreateResponse(HttpStatusCode.OK); + } + else + { + return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Partial View found with the specified path"); + } + break; + case "partialViewMacro": + if (Services.FileService.DeletePartialViewMacro(virtualPath, Security.CurrentUser.Id)) + { + return Request.CreateResponse(HttpStatusCode.OK); + } + else + { + return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Partial View Macro found with the specified path"); + } + break; + case "script": + if (Services.FileService.GetScriptByName(virtualPath) != null) + { + Services.FileService.DeleteScript(virtualPath, Security.CurrentUser.Id); + return Request.CreateResponse(HttpStatusCode.OK); + } + else + { + return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Script found with the specified path"); + } + break; + default: + throw new ArgumentException("File Type not supported", "type"); + } + } + + public CodeFileDisplay PostSave(string type, CodeFileDisplay display) + { + if (display == null) + { + throw new ArgumentNullException("No file object has been passed"); + } + else + { + switch (type) + { + case "partialView": + var view = Services.FileService.GetPartialView(display.VirtualPath); + if (view != null) + { + view.Content = display.Content; + Services.FileService.SavePartialView(view, Security.CurrentUser.Id); + } + else + { + throw new ArgumentNullException($"File doesn't exist - {display.VirtualPath}"); + } + break; + case "partialViewMacro": + var viewMacro = Services.FileService.GetPartialViewMacro(display.VirtualPath); + if (viewMacro != null) + { + viewMacro.Content = display.Content; + Services.FileService.SavePartialViewMacro(viewMacro, Security.CurrentUser.Id); + } + else + { + throw new ArgumentNullException($"File doesn't exist - {display.VirtualPath}"); + } + break; + case "script": + var script = Services.FileService.GetScriptByName(display.VirtualPath); + if (script != null) + { + script.Content = display.Content; + Services.FileService.SaveScript(script, Security.CurrentUser.Id); + } + else + { + throw new ArgumentNullException($"File doesn't exist - {display.VirtualPath}"); + } + break; + default: + throw new ArgumentException("File Type not supported", "type"); + } + return display; + } + } + } +} diff --git a/src/Umbraco.Web/Models/ContentEditing/CodeFileDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/CodeFileDisplay.cs new file mode 100644 index 0000000000..1ab5f1ddb4 --- /dev/null +++ b/src/Umbraco.Web/Models/ContentEditing/CodeFileDisplay.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Umbraco.Web.Models.ContentEditing +{ + [DataContract(Name = "scriptFile", Namespace = "")] + public class CodeFileDisplay + { + [DataMember(Name = "virtualPath")] + public string VirtualPath { get; set; } + [DataMember(Name = "content")] + public string Content { get; set; } + [DataMember(Name = "snippet")] + public string Snippet { get; set; } + } +} diff --git a/src/Umbraco.Web/Models/Mapping/CodeFileDisplayMapper.cs b/src/Umbraco.Web/Models/Mapping/CodeFileDisplayMapper.cs new file mode 100644 index 0000000000..d777d27c43 --- /dev/null +++ b/src/Umbraco.Web/Models/Mapping/CodeFileDisplayMapper.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AutoMapper; +using Umbraco.Core; +using Umbraco.Core.Models.Mapping; +using Umbraco.Core.Models; +using Umbraco.Web.Models.ContentEditing; + +namespace Umbraco.Web.Models.Mapping +{ + public class CodeFileDisplayMapper : MapperConfiguration + { + public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext) + { + config.CreateMap() + .ForMember(x => x.Snippet, exp => exp.Ignore()); + + config.CreateMap() + .ForMember(x => x.Snippet, exp => exp.Ignore()); + + config.CreateMap() + .ForMember(x => x.Key, exp => exp.Ignore()) + .ForMember(x => x.Path, exp => exp.Ignore()) + .ForMember(x => x.CreateDate, exp => exp.Ignore()) + .ForMember(x => x.UpdateDate, exp => exp.Ignore()) + .ForMember(x => x.Path, exp => exp.Ignore()) + .ForMember(x => x.Alias, exp => exp.Ignore()) + .ForMember(x => x.Name, exp => exp.Ignore()) + .ForMember(x => x.OriginalPath, exp => exp.Ignore()) + .ForMember(x => x.HasIdentity, exp => exp.Ignore()); + + config.CreateMap() + .ForMember(x => x.Key, exp => exp.Ignore()) + .ForMember(x => x.Path, exp => exp.Ignore()) + .ForMember(x => x.CreateDate, exp => exp.Ignore()) + .ForMember(x => x.UpdateDate, exp => exp.Ignore()) + .ForMember(x => x.Path, exp => exp.Ignore()) + .ForMember(x => x.Alias, exp => exp.Ignore()) + .ForMember(x => x.Name, exp => exp.Ignore()) + .ForMember(x => x.OriginalPath, exp => exp.Ignore()) + .ForMember(x => x.HasIdentity, exp => exp.Ignore()); + } + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 1db9416f86..14c3e44dba 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -277,6 +277,7 @@ + @@ -328,10 +329,12 @@ + +