Exemplo n.º 1
0
        public async Task <IEnumerable <ContentItem> > GetAsync(IEnumerable <string> contentItemIds, bool latest = false)
        {
            if (contentItemIds == null)
            {
                throw new ArgumentNullException(nameof(contentItemIds));
            }

            List <ContentItem> contentItems = null;
            List <ContentItem> storedItems  = null;

            if (latest)
            {
                contentItems = (await _session
                                .Query <ContentItem, ContentItemIndex>()
                                .Where(x => x.ContentItemId.IsIn(contentItemIds) && x.Latest == true)
                                .ListAsync()).ToList();
            }
            else
            {
                foreach (var contentItemId in contentItemIds)
                {
                    // If the published version is already stored, we can return it.
                    if (_contentManagerSession.RecallPublishedItemId(contentItemId, out var contentItem))
                    {
                        if (storedItems == null)
                        {
                            storedItems = new List <ContentItem>();
                        }

                        storedItems.Add(contentItem);
                    }
                }

                // Only query the ids not already stored.
                var itemIdsToQuery = storedItems != null
                    ? contentItemIds.Except(storedItems.Select(x => x.ContentItemId))
                    : contentItemIds;

                if (itemIdsToQuery.Any())
                {
                    contentItems = (await _session
                                    .Query <ContentItem, ContentItemIndex>()
                                    .Where(x => x.ContentItemId.IsIn(itemIdsToQuery) && x.Published == true)
                                    .ListAsync()).ToList();
                }
            }

            if (contentItems != null)
            {
                for (var i = 0; i < contentItems.Count; i++)
                {
                    contentItems[i] = await LoadAsync(contentItems[i]);
                }

                if (storedItems != null)
                {
                    contentItems.AddRange(storedItems);
                }
            }
            else if (storedItems != null)
            {
                contentItems = storedItems;
            }
            else
            {
                return(Enumerable.Empty <ContentItem>());
            }

            var contentItemIdsArray = contentItemIds.ToImmutableArray();

            return(contentItems.OrderBy(c => contentItemIdsArray.IndexOf(c.ContentItemId)));
        }
        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)
                {
                    // We save the previous version further because this call might do a session query.
                    var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(contentItem.ContentType);

                    // Check if not versionable, meaning we use only one version
                    if (!(contentTypeDefinition?.GetSettings <ContentTypeSettings>().Versionable ?? true))
                    {
                        contentItem.Published = false;
                    }
                    else
                    {
                        // Save the previous version
                        _session.Save(contentItem);

                        contentItem = await BuildNewVersionAsync(contentItem);
                    }
                }

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

            return(contentItem);
        }