Esempio n. 1
0
        /// <summary>
        /// Puts the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutHtmlContentResponse</c> with html content id.
        /// </returns>
        public PutHtmlContentResponse Put(PutHtmlContentRequest request)
        {
            var isNew = !request.Id.HasValue || request.Id.Value.HasDefaultValue();
            Module.Pages.Models.HtmlContent originalContent = null;
            if (!isNew)
            {
                originalContent = repository.FirstOrDefault<Module.Pages.Models.HtmlContent>(request.Id.Value);
                isNew = originalContent == null;
            }

            var contentToSave = new Module.Pages.Models.HtmlContent
            {
                Id = request.Id.GetValueOrDefault(),
                ActivationDate = request.Data.ActivationDate,
                ExpirationDate = TimeHelper.FormatEndDate(request.Data.ExpirationDate),
                Name = request.Data.Name,
                Html = request.Data.Html ?? string.Empty,
                UseCustomCss = request.Data.UseCustomCss,
                CustomCss = request.Data.CustomCss,
                UseCustomJs = request.Data.UseCustomJavaScript,
                CustomJs = request.Data.CustomJavaScript
            };

            if (request.Data.IsPublished)
            {
                if (isNew)
                {
                    if (request.Data.PublishedOn.HasValue)
                    {
                        contentToSave.PublishedOn = request.Data.PublishedOn;
                    }
                    if (!string.IsNullOrEmpty(request.Data.PublishedByUser))
                    {
                        contentToSave.PublishedByUser = request.Data.PublishedByUser;
                    }
                }
                else
                {
                    contentToSave.PublishedOn = originalContent.PublishedOn;
                    contentToSave.PublishedByUser = originalContent.PublishedByUser;
                }
            }

            unitOfWork.BeginTransaction();

            Module.Pages.Models.HtmlContent content;
            if (isNew && contentToSave.Id != default(Guid))
            {
                content = contentToSave;

                content.Status = request.Data.IsPublished ? ContentStatus.Published : ContentStatus.Draft;
                content.Id = contentToSave.Id;

                repository.Save(content);
            }
            else
            {
                if (request.Data.Version > 0)
                {
                    contentToSave.Version = request.Data.Version;
                }

                content = (Module.Pages.Models.HtmlContent)contentService
                    .SaveContentWithStatusUpdate(contentToSave, request.Data.IsPublished ? ContentStatus.Published : ContentStatus.Draft);
            }

            unitOfWork.Commit();

            if (isNew)
            {
                Events.PageEvents.Instance.OnHtmlContentCreated(content);
            }
            else
            {
                Events.PageEvents.Instance.OnHtmlContentUpdated(content);
            }

            return new PutHtmlContentResponse
            {
                Data = content.Id
            };
        }
        /// <summary>
        /// Puts the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutHtmlContentResponse</c> with html content id.
        /// </returns>
        public PutHtmlContentResponse Put(PutHtmlContentRequest request)
        {
            var isNew = !request.Id.HasValue || request.Id.Value.HasDefaultValue();
            Module.Pages.Models.HtmlContent originalContent = null;
            if (!isNew)
            {
                originalContent = repository.FirstOrDefault<Module.Pages.Models.HtmlContent>(request.Id.Value);
                isNew = originalContent == null;
            }

            var contentToSave = new Module.Pages.Models.HtmlContent
            {
                Id = request.Id.GetValueOrDefault(),
                ActivationDate = request.Data.ActivationDate,
                ExpirationDate = TimeHelper.FormatEndDate(request.Data.ExpirationDate),
                Name = request.Data.Name,
                Html = request.Data.Html ?? string.Empty,
                OriginalText = request.Data.OriginalText ?? string.Empty,
                ContentTextMode = (Module.Pages.Models.Enums.ContentTextMode)request.Data.ContentTextMode,
                UseCustomCss = request.Data.UseCustomCss,
                CustomCss = request.Data.CustomCss,
                UseCustomJs = request.Data.UseCustomJavaScript,
                CustomJs = request.Data.CustomJavaScript
            };

            if (request.Data.ContentTextMode == ContentTextMode.Markdown
                && request.Data.Html == null
                && request.Data.OriginalText != null)
            {
                contentToSave.Html = MarkdownConverter.ToHtml(request.Data.OriginalText);
            }
            
            if (request.Data.ContentTextMode == ContentTextMode.SimpleText
                && request.Data.Html == null
                && request.Data.OriginalText != null)
            {
                contentToSave.Html = HttpUtility.HtmlEncode(request.Data.OriginalText);
            }

            if (request.Data.IsPublished)
            {
                if (isNew)
                {
                    if (request.Data.PublishedOn.HasValue)
                    {
                        contentToSave.PublishedOn = request.Data.PublishedOn;
                    }
                    if (!string.IsNullOrEmpty(request.Data.PublishedByUser))
                    {
                        contentToSave.PublishedByUser = request.Data.PublishedByUser;
                    }
                }
                else
                {
                    contentToSave.PublishedOn = originalContent.PublishedOn;
                    contentToSave.PublishedByUser = originalContent.PublishedByUser;
                }
            }

            unitOfWork.BeginTransaction();

            Module.Pages.Models.HtmlContent content;
            var desirableStatus = request.Data.IsPublished ? ContentStatus.Published : ContentStatus.Draft;
            if (isNew && contentToSave.Id != default(Guid))
            {
                content = contentToSave;
                contentService.UpdateDynamicContainer(content);

                content.Status = desirableStatus;
                content.Id = contentToSave.Id;

                repository.Save(content);
            }
            else
            {
                if (request.Data.Version > 0)
                {
                    contentToSave.Version = request.Data.Version;
                }

                content = (Module.Pages.Models.HtmlContent)contentService
                    .SaveContentWithStatusUpdate(contentToSave, desirableStatus);
            }
            var childContentOptionValues = request.Data.ChildContentsOptionValues != null ? request.Data.ChildContentsOptionValues.ToViewModel() : null;
            optionService.SaveChildContentOptions(content, childContentOptionValues, desirableStatus);

            unitOfWork.Commit();

            if (isNew)
            {
                Events.PageEvents.Instance.OnHtmlContentCreated(content);
            }
            else
            {
                Events.PageEvents.Instance.OnHtmlContentUpdated(content);
            }

            return new PutHtmlContentResponse
            {
                Data = content.Id
            };
        }
Esempio n. 3
0
        /// <summary>
        /// Puts the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutHtmlContentResponse</c> with html content id.
        /// </returns>
        public PutHtmlContentResponse Put(PutHtmlContentRequest request)
        {
            var isNew = !request.Id.HasValue || request.Id.Value.HasDefaultValue();

            Module.Pages.Models.HtmlContent originalContent = null;
            if (!isNew)
            {
                originalContent = repository.FirstOrDefault <Module.Pages.Models.HtmlContent>(request.Id.Value);
                isNew           = originalContent == null;
            }

            var contentToSave = new Module.Pages.Models.HtmlContent
            {
                Id             = request.Id.GetValueOrDefault(),
                ActivationDate = request.Data.ActivationDate,
                ExpirationDate = TimeHelper.FormatEndDate(request.Data.ExpirationDate),
                Name           = request.Data.Name,
                Html           = request.Data.Html ?? string.Empty,
                UseCustomCss   = request.Data.UseCustomCss,
                CustomCss      = request.Data.CustomCss,
                UseCustomJs    = request.Data.UseCustomJavaScript,
                CustomJs       = request.Data.CustomJavaScript
            };

            if (request.Data.IsPublished)
            {
                if (isNew)
                {
                    if (request.Data.PublishedOn.HasValue)
                    {
                        contentToSave.PublishedOn = request.Data.PublishedOn;
                    }
                    if (!string.IsNullOrEmpty(request.Data.PublishedByUser))
                    {
                        contentToSave.PublishedByUser = request.Data.PublishedByUser;
                    }
                }
                else
                {
                    contentToSave.PublishedOn     = originalContent.PublishedOn;
                    contentToSave.PublishedByUser = originalContent.PublishedByUser;
                }
            }

            unitOfWork.BeginTransaction();

            Module.Pages.Models.HtmlContent content;
            var desirableStatus = request.Data.IsPublished ? ContentStatus.Published : ContentStatus.Draft;

            if (isNew && contentToSave.Id != default(Guid))
            {
                content = contentToSave;
                contentService.UpdateDynamicContainer(content);

                content.Status = desirableStatus;
                content.Id     = contentToSave.Id;

                repository.Save(content);
            }
            else
            {
                if (request.Data.Version > 0)
                {
                    contentToSave.Version = request.Data.Version;
                }

                content = (Module.Pages.Models.HtmlContent)contentService
                          .SaveContentWithStatusUpdate(contentToSave, desirableStatus);
            }
            var childContentOptionValues = request.Data.ChildContentsOptionValues != null?request.Data.ChildContentsOptionValues.ToViewModel() : null;

            optionService.SaveChildContentOptions(content, childContentOptionValues, desirableStatus);

            unitOfWork.Commit();

            if (isNew)
            {
                Events.PageEvents.Instance.OnHtmlContentCreated(content);
            }
            else
            {
                Events.PageEvents.Instance.OnHtmlContentUpdated(content);
            }

            return(new PutHtmlContentResponse
            {
                Data = content.Id
            });
        }