Implements CodeFileController

This commit is contained in:
Niels Hartvig
2017-01-09 22:54:28 +01:00
parent f5f9ff29ce
commit 4c330c8654
5 changed files with 229 additions and 0 deletions

View File

@@ -359,6 +359,10 @@ namespace Umbraco.Web.Editors
{
"healthCheckBaseUrl", Url.GetUmbracoApiServiceBaseUrl<HealthCheckController>(
controller => controller.GetAllHealthChecks())
},
{
"codeFileApiBaseUrl", Url.GetUmbracoApiServiceBaseUrl<CodeFileController>(
controller => controller.GetByPath("", ""))
}
}
},

View File

@@ -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<IPartialView, CodeFileDisplay>(view);
case "partialViewMacro":
var viewMacro = Services.FileService.GetPartialViewMacro(virtualPath);
return viewMacro == null ? null : Mapper.Map<IPartialView, CodeFileDisplay>(viewMacro);
case "script":
var script = Services.FileService.GetScriptByName(virtualPath);
return script == null ? null : Mapper.Map<Script, CodeFileDisplay>(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;
}
}
}
}

View File

@@ -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; }
}
}

View File

@@ -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<IPartialView, CodeFileDisplay>()
.ForMember(x => x.Snippet, exp => exp.Ignore());
config.CreateMap<Script, CodeFileDisplay>()
.ForMember(x => x.Snippet, exp => exp.Ignore());
config.CreateMap<CodeFileDisplay, IPartialView>()
.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<CodeFileDisplay, Script>()
.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());
}
}
}

View File

@@ -277,6 +277,7 @@
<Compile Include="Editors\EditorValidationResolver.cs" />
<Compile Include="Editors\EditorValidator.cs" />
<Compile Include="Editors\GravatarController.cs" />
<Compile Include="Editors\CodeFileController.cs" />
<Compile Include="HealthCheck\Checks\Config\AbstractConfigCheck.cs" />
<Compile Include="HealthCheck\Checks\Config\AcceptableConfiguration.cs" />
<Compile Include="HealthCheck\Checks\Config\ConfigurationService.cs" />
@@ -328,10 +329,12 @@
<Compile Include="Models\ContentEditing\PropertyTypeBasic.cs" />
<Compile Include="Models\ContentEditing\SimpleNotificationModel.cs" />
<Compile Include="Models\LocalPackageInstallModel.cs" />
<Compile Include="Models\Mapping\CodeFileDisplayMapper.cs" />
<Compile Include="Models\Mapping\ContentTypeModelMapperExtensions.cs" />
<Compile Include="Models\Mapping\LockedCompositionsResolver.cs" />
<Compile Include="Models\Mapping\PropertyGroupDisplayResolver.cs" />
<Compile Include="Models\PackageInstallResult.cs" />
<Compile Include="Models\ContentEditing\CodeFileDisplay.cs" />
<Compile Include="Models\SetPasswordModel.cs" />
<Compile Include="Models\RequestPasswordResetModel.cs" />
<Compile Include="Models\PublishedContentWithKeyBase.cs" />