Ejemplo n.º 1
0
        public static async Task <ContentValidateResult> UpdateValidateAndCreateAsync(this IContentManager contentManager, ContentItem contentItem, VersionOptions options)
        {
            await contentManager.UpdateAsync(contentItem);

            var result = await contentManager.ValidateAsync(contentItem);

            if (result.Succeeded)
            {
                await contentManager.CreateAsync(contentItem, options);
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets either the container content item with the specified id and version, or if the json path supplied gets the contained content item.
        /// </summary>
        /// <param name="contentManager">The <see cref="IContentManager"/> instance.</param>
        /// <param name="id">The id content item id to load</param>
        /// <param name="options">The version option</param>
        /// <param name="jsonPath">The json path of the contained content item</param>
        public static async Task <ContentItem> GetAsync(this IContentManager contentManager, string id, string jsonPath, VersionOptions options)
        {
            var contentItem = await contentManager.GetAsync(id, options);

            // It represents a contained content item
            if (!string.IsNullOrEmpty(jsonPath))
            {
                var root = contentItem.Content as JObject;
                contentItem = root.SelectToken(jsonPath)?.ToObject <ContentItem>();

                return(contentItem);
            }

            return(contentItem);
        }
Ejemplo n.º 3
0
        public async Task <ContentItem> GetAsync(string contentItemId, VersionOptions options)
        {
            ContentItem contentItem = null;

            if (options.IsLatest)
            {
                contentItem = await _session
                              .Query <ContentItem, ContentItemIndex>()
                              .Where(x => x.ContentItemId == contentItemId && x.Latest == true)
                              .FirstOrDefaultAsync();
            }
            else if (options.IsDraft && !options.IsDraftRequired)
            {
                contentItem = await _session
                              .Query <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Published == false &&
                                     x.Latest == true)
                              .FirstOrDefaultAsync();
            }
            else if (options.IsDraft || options.IsDraftRequired)
            {
                // Loaded whatever is the latest as it will be cloned
                contentItem = await _session
                              .Query <ContentItem, ContentItemIndex>()
                              .Where(x =>
                                     x.ContentItemId == contentItemId &&
                                     x.Latest == true)
                              .FirstOrDefaultAsync();
            }
            else if (options.IsPublished)
            {
                // If the published version is requested and is already loaded, we can
                // return it right away
                if (_contentManagerSession.RecallPublishedItemId(contentItemId, out contentItem))
                {
                    return(contentItem);
                }

                contentItem = await _session.ExecuteQuery(new PublishedContentItemById(contentItemId)).FirstOrDefaultAsync();
            }

            if (contentItem == null)
            {
                return(null);
            }

            contentItem = await LoadAsync(contentItem);

            if (options.IsDraftRequired)
            {
                // When draft is required and latest is published a new version is added
                if (contentItem.Published)
                {
                    // Save the previous version
                    _session.Save(contentItem);

                    var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(contentItem.ContentType);

                    // Check if not versionable, meaning we use only one version
                    if (!(contentTypeDefinition?.Settings.ToObject <ContentTypeSettings>().Versionable ?? true))
                    {
                        contentItem.Published = false;
                    }
                    else
                    {
                        contentItem = await BuildNewVersionAsync(contentItem);
                    }
                }

                // Save the new version
                _session.Save(contentItem);
            }

            return(contentItem);
        }