Ejemplo n.º 1
0
    private Attempt <IPartialView?> SavePartialView(IPartialView partialView, PartialViewType partialViewType, int?userId = null)
    {
        using (ICoreScope scope = ScopeProvider.CreateCoreScope())
        {
            EventMessages eventMessages      = EventMessagesFactory.Get();
            var           savingNotification = new PartialViewSavingNotification(partialView, eventMessages);
            if (scope.Notifications.PublishCancelable(savingNotification))
            {
                scope.Complete();
                return(Attempt <IPartialView?> .Fail());
            }

            userId ??= Constants.Security.SuperUserId;
            IPartialViewRepository repository = GetPartialViewRepository(partialViewType);
            repository.Save(partialView);

            Audit(AuditType.Save, userId.Value, -1, partialViewType.ToString());
            scope.Notifications.Publish(
                new PartialViewSavedNotification(partialView, eventMessages).WithStateFrom(savingNotification));

            scope.Complete();
        }

        return(Attempt.Succeed(partialView));
    }
Ejemplo n.º 2
0
        private Attempt <IPartialView?> CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string?snippetName = null, int?userId = Constants.Security.SuperUserId)
        {
            string partialViewHeader;

            switch (partialViewType)
            {
            case PartialViewType.PartialView:
                partialViewHeader = PartialViewHeader;
                break;

            case PartialViewType.PartialViewMacro:
                partialViewHeader = PartialViewMacroHeader;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(partialViewType));
            }

            string?partialViewContent = null;

            if (snippetName.IsNullOrWhiteSpace() == false)
            {
                //create the file
                Attempt <string> 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);

                    //Update Model.Content. to be Model. when used as PartialView
                    if (partialViewType == PartialViewType.PartialView)
                    {
                        snippetContent = snippetContent.Replace("Model.Content.", "Model.");
                    }

                    partialViewContent = $"{partialViewHeader}{Environment.NewLine}{snippetContent}";
                }
            }

            using (ICoreScope scope = ScopeProvider.CreateCoreScope())
            {
                EventMessages eventMessages        = EventMessagesFactory.Get();
                var           creatingNotification = new PartialViewCreatingNotification(partialView, eventMessages);
                if (scope.Notifications.PublishCancelable(creatingNotification))
                {
                    scope.Complete();
                    return(Attempt <IPartialView?> .Fail());
                }

                IPartialViewRepository repository = GetPartialViewRepository(partialViewType);
                if (partialViewContent != null)
                {
                    partialView.Content = partialViewContent;
                }

                repository.Save(partialView);

                scope.Notifications.Publish(new PartialViewCreatedNotification(partialView, eventMessages).WithStateFrom(creatingNotification));

                Audit(AuditType.Save, userId !.Value, -1, partialViewType.ToString());

                scope.Complete();
            }

            return(Attempt <IPartialView?> .Succeed(partialView));
        }