Adds GetScaffold in CodeFileController to simplify initial view in backoffice

Refactored the FileService a bit to be able to get content of the snippets without duplicate code
This commit is contained in:
Emil Wangaa
2017-02-01 12:33:20 +01:00
parent d4fab66411
commit 6a1d7d2586
3 changed files with 106 additions and 32 deletions

View File

@@ -744,40 +744,9 @@ namespace Umbraco.Core.Services
if (CreatingPartialView.IsRaisedEventCancelled(new NewEventArgs<IPartialView>(partialView, true, partialView.Alias, -1), this))
return Attempt<IPartialView>.Fail();
string partialViewHeader;
switch (partialViewType)
{
case PartialViewType.PartialView:
partialViewHeader = PartialViewHeader;
break;
case PartialViewType.PartialViewMacro:
partialViewHeader = PartialViewMacroHeader;
break;
default:
throw new ArgumentOutOfRangeException("partialViewType");
}
if (snippetName.IsNullOrWhiteSpace() == false)
{
//create the file
var snippetPathAttempt = TryGetSnippetPath(snippetName);
if (snippetPathAttempt.Success == false)
{
throw new InvalidOperationException("Could not load snippet with name " + snippetName);
}
using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result)))
{
var snippetContent = snippetFile.ReadToEnd().Trim();
//strip the @inherits if it's there
snippetContent = StripPartialViewHeader(snippetContent);
var content = string.Format("{0}{1}{2}",
partialViewHeader,
Environment.NewLine, snippetContent);
partialView.Content = content;
}
partialView.Content = GetPartialViewMacroSnippetContent(snippetName, partialViewType);
}
var uow = _fileUowProvider.GetUnitOfWork();
@@ -914,6 +883,55 @@ namespace Umbraco.Core.Services
}
}
public string GetPartialViewSnippetContent(string snippetName)
{
return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialView);
}
public string GetPartialViewMacroSnippetContent(string snippetName)
{
return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialViewMacro);
}
private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType)
{
if (snippetName.IsNullOrWhiteSpace())
throw new ArgumentNullException("snippetName");
string partialViewHeader;
switch (partialViewType)
{
case PartialViewType.PartialView:
partialViewHeader = PartialViewHeader;
break;
case PartialViewType.PartialViewMacro:
partialViewHeader = PartialViewMacroHeader;
break;
default:
throw new ArgumentOutOfRangeException("partialViewType");
}
// Try and get the snippet path
var snippetPathAttempt = TryGetSnippetPath(snippetName);
if (snippetPathAttempt.Success == false)
{
throw new InvalidOperationException("Could not load snippet with name " + snippetName);
}
using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result)))
{
var snippetContent = snippetFile.ReadToEnd().Trim();
//strip the @inherits if it's there
snippetContent = StripPartialViewHeader(snippetContent);
var content = string.Format("{0}{1}{2}",
partialViewHeader,
Environment.NewLine, snippetContent);
return content;
}
}
public void SetPartialViewMacroFileContent(string filepath, Stream content)
{
using (var repository = GetPartialViewRepository(PartialViewType.PartialViewMacro, UowProvider.GetUnitOfWork()))

View File

@@ -331,6 +331,13 @@ namespace Umbraco.Core.Services
/// <returns>The content of the macro partial view.</returns>
Stream GetPartialViewMacroFileContentStream(string filepath);
/// <summary>
/// Gets the content of a macro partial view snippet as a string
/// </summary>
/// <param name="snippetName">The name of the snippet</param>
/// <returns></returns>
string GetPartialViewMacroSnippetContent(string snippetName);
/// <summary>
/// Sets the content of a macro partial view.
/// </summary>
@@ -352,6 +359,13 @@ namespace Umbraco.Core.Services
/// <returns>The content of the partial view.</returns>
Stream GetPartialViewFileContentStream(string filepath);
/// <summary>
/// Gets the content of a partial view snippet as a string.
/// </summary>
/// <param name="snippetName">The name of the snippet</param>
/// <returns>The content of the partial view.</returns>
string GetPartialViewSnippetContent(string snippetName);
/// <summary>
/// Sets the content of a partial view.
/// </summary>

View File

@@ -33,11 +33,13 @@ namespace Umbraco.Web.Editors
{
case Core.Constants.Trees.PartialViews:
var view = new PartialView(display.VirtualPath);
view.Content = display.Content;
var result = Services.FileService.CreatePartialView(view, display.Snippet, Security.CurrentUser.Id);
return result.Success == true ? Request.CreateResponse(HttpStatusCode.OK) : Request.CreateNotificationValidationErrorResponse(result.Exception.Message);
case Core.Constants.Trees.PartialViewMacros:
var viewMacro = new PartialView(display.VirtualPath);
viewMacro.Content = display.Content;
var resultMacro = Services.FileService.CreatePartialViewMacro(viewMacro, display.Snippet, Security.CurrentUser.Id);
return resultMacro.Success == true ? Request.CreateResponse(HttpStatusCode.OK) : Request.CreateNotificationValidationErrorResponse(resultMacro.Exception.Message);
@@ -136,6 +138,46 @@ namespace Umbraco.Web.Editors
return snippets.Select(snippet => new SnippetDisplay() {Name = snippet.SplitPascalCasing().ToFirstUpperInvariant(), FileName = snippet});
}
/// <summary>
/// Used to scaffold the json object for the editors for 'scripts', 'partialViews', 'partialViewMacros'
/// </summary>
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros'</param>
/// <param name="snippetName"></param>
/// <returns></returns>
public CodeFileDisplay GetScaffold(string type, string snippetName = null)
{
if (string.IsNullOrWhiteSpace(type))
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
CodeFileDisplay codeFileDisplay;
switch (type)
{
case Core.Constants.Trees.PartialViews:
codeFileDisplay = Mapper.Map<IPartialView, CodeFileDisplay>(new PartialView(string.Empty));
if (snippetName.IsNullOrWhiteSpace() == false)
codeFileDisplay.Content = Services.FileService.GetPartialViewSnippetContent(snippetName);
break;
case Core.Constants.Trees.PartialViewMacros:
codeFileDisplay = Mapper.Map<IPartialView, CodeFileDisplay>(new PartialView(string.Empty));
if (snippetName.IsNullOrWhiteSpace() == false)
codeFileDisplay.Content = Services.FileService.GetPartialViewMacroSnippetContent(snippetName);
break;
case Core.Constants.Trees.Scripts:
codeFileDisplay = Mapper.Map<Script, CodeFileDisplay>(new Script(string.Empty));
break;
default:
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Unsupported editortype"));
}
codeFileDisplay.FileType = type;
codeFileDisplay.VirtualPath = "-1";
return codeFileDisplay;
}
/// <summary>
/// Used to delete a specific file from disk via the FileService
/// </summary>