Files
Umbraco-CMS/src/Umbraco.Core/Services/PartialViewService.cs
Kenn Jacobsen e4f9f98f2d File system endpoints redo (#15521)
* First stab at a massive remake of file system based endpoints

* Do not prefix system paths with directory separator char

* Ensure correct and consistent response types

* Fix partial view snippets endpoints

* Clean up IO (path) operations

* Update OpenAPI JSON to match new endpoints

* Return 201 when renaming file system resources

* Add "IsFolder" to file system item endpoints

* Replace "parentPath" with a "parent" object for file system creation endpoints

* Update OpenAPI JSON

* Rewrite snippets

* Regenerate OpenAPI JSON after forward merge

* Remove stylesheet overview endpoint

* Regenerate OpenAPI JSON after forward merge

* add server-file-system module to importmap

* Expose generated resource identifier in 201 responses

---------

Co-authored-by: Mads Rasmussen <madsr@hey.com>
2024-01-22 08:20:45 +01:00

102 lines
4.7 KiB
C#

using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.OperationStatus;
using Umbraco.Cms.Core.Snippets;
using PartialViewSnippet = Umbraco.Cms.Core.Snippets.PartialViewSnippet;
namespace Umbraco.Cms.Core.Services;
public class PartialViewService : FileServiceOperationBase<IPartialViewRepository, IPartialView, PartialViewOperationStatus>, IPartialViewService
{
private readonly PartialViewSnippetCollection _snippetCollection;
public PartialViewService(
ICoreScopeProvider provider,
ILoggerFactory loggerFactory,
IEventMessagesFactory eventMessagesFactory,
IPartialViewRepository repository,
ILogger<StylesheetService> logger,
IUserIdKeyResolver userIdKeyResolver,
IAuditRepository auditRepository,
PartialViewSnippetCollection snippetCollection)
: base(provider, loggerFactory, eventMessagesFactory, repository, logger, userIdKeyResolver, auditRepository)
=> _snippetCollection = snippetCollection;
protected override string[] AllowedFileExtensions { get; } = { ".cshtml" };
protected override PartialViewOperationStatus Success => PartialViewOperationStatus.Success;
protected override PartialViewOperationStatus NotFound => PartialViewOperationStatus.NotFound;
protected override PartialViewOperationStatus CancelledByNotification => PartialViewOperationStatus.CancelledByNotification;
protected override PartialViewOperationStatus PathTooLong => PartialViewOperationStatus.PathTooLong;
protected override PartialViewOperationStatus AlreadyExists => PartialViewOperationStatus.AlreadyExists;
protected override PartialViewOperationStatus ParentNotFound => PartialViewOperationStatus.ParentNotFound;
protected override PartialViewOperationStatus InvalidName => PartialViewOperationStatus.InvalidName;
protected override PartialViewOperationStatus InvalidFileExtension => PartialViewOperationStatus.InvalidFileExtension;
protected override string EntityType => "PartialView";
protected override PartialViewSavingNotification SavingNotification(IPartialView target, EventMessages messages)
=> new(target, messages);
protected override PartialViewSavedNotification SavedNotification(IPartialView target, EventMessages messages)
=> new(target, messages);
protected override PartialViewDeletingNotification DeletingNotification(IPartialView target, EventMessages messages)
=> new(target, messages);
protected override PartialViewDeletedNotification DeletedNotification(IPartialView target, EventMessages messages)
=> new(target, messages);
protected override IPartialView CreateEntity(string path, string? content)
=> new PartialView(PartialViewType.PartialView, path) { Content = content };
/// <inheritdoc />
public async Task<PagedModel<PartialViewSnippetSlim>> GetSnippetsAsync(int skip, int take)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
var result = new PagedModel<PartialViewSnippetSlim>(
_snippetCollection.Count,
_snippetCollection
.Skip(skip)
.Take(take)
.Select(snippet => new PartialViewSnippetSlim(snippet.Id, snippet.Name))
.ToArray());
return await Task.FromResult(result);
}
/// <inheritdoc />
public async Task<PartialViewSnippet?> GetSnippetAsync(string id)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
PartialViewSnippet? snippet = _snippetCollection.FirstOrDefault(s => s.Id == id);
return await Task.FromResult(snippet);
}
/// <inheritdoc />
public async Task<PartialViewOperationStatus> DeleteAsync(string path, Guid userKey)
=> await HandleDeleteAsync(path, userKey);
/// <inheritdoc />
public async Task<Attempt<IPartialView?, PartialViewOperationStatus>> CreateAsync(PartialViewCreateModel createModel, Guid userKey)
=> await HandleCreateAsync(createModel.Name, createModel.ParentPath, createModel.Content, userKey);
/// <inheritdoc />
public async Task<Attempt<IPartialView?, PartialViewOperationStatus>> UpdateAsync(string path, PartialViewUpdateModel updateModel, Guid userKey)
=> await HandleUpdateAsync(path, updateModel.Content, userKey);
/// <inheritdoc />
public async Task<Attempt<IPartialView?, PartialViewOperationStatus>> RenameAsync(string path, PartialViewRenameModel renameModel, Guid userKey)
=> await HandleRenameAsync(path, renameModel.Name, userKey);
}