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, IPartialViewService { private readonly PartialViewSnippetCollection _snippetCollection; public PartialViewService( ICoreScopeProvider provider, ILoggerFactory loggerFactory, IEventMessagesFactory eventMessagesFactory, IPartialViewRepository repository, ILogger 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(path) { Content = content }; /// public async Task> GetSnippetsAsync(int skip, int take) { using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); var result = new PagedModel( _snippetCollection.Count, _snippetCollection .Skip(skip) .Take(take) .Select(snippet => new PartialViewSnippetSlim(snippet.Id, snippet.Name)) .ToArray()); return await Task.FromResult(result); } /// public async Task GetSnippetAsync(string id) { using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true); PartialViewSnippet? snippet = _snippetCollection.FirstOrDefault(s => s.Id == id); return await Task.FromResult(snippet); } /// public async Task DeleteAsync(string path, Guid userKey) => await HandleDeleteAsync(path, userKey); /// public async Task> CreateAsync(PartialViewCreateModel createModel, Guid userKey) => await HandleCreateAsync(createModel.Name, createModel.ParentPath, createModel.Content, userKey); /// public async Task> UpdateAsync(string path, PartialViewUpdateModel updateModel, Guid userKey) => await HandleUpdateAsync(path, updateModel.Content, userKey); /// public async Task> RenameAsync(string path, PartialViewRenameModel renameModel, Guid userKey) => await HandleRenameAsync(path, renameModel.Name, userKey); }