public async Task <bool> Update(string contentItemId, string[] relContentItemIds, string contentRelationRecordId)
        {
            var contentRelations = (await _session
                                    .Query <ContentRelationItemRecord, ContentRelationItemIndex>()
                                    .Where(r => r.ContentRelationRecordId == contentRelationRecordId &&
                                           (r.ContentItemRecord1Id == contentItemId || r.ContentItemRecord2Id == contentItemId)).ListAsync()).ToList();

            //var contentRelations = _contentRelationItemRepository
            //     .Fetch(r => r.ContentRelationRecord_Id == contentRelationRecordId
            //                 && (r.ContentItemRecord1_Id == contentItemId || r.ContentItemRecord2_Id == contentItemId)).ToList();

            for (var i = 0; i < contentRelations.Count(); i++)
            {
                var contentRelationItemRecord = contentRelations[i];
                var relContentItemId          = contentRelationItemRecord.ContentItemRecord1Id == contentItemId ?
                                                contentRelationItemRecord.ContentItemRecord2Id : contentRelationItemRecord.ContentItemRecord1Id;

                if (!relContentItemIds.Any(r => r == relContentItemId))
                {
                    await _contentRelationItemRepository.DeleteAsync(contentRelationItemRecord);

                    contentRelations.Remove(contentRelationItemRecord);
                    i--;
                }
            }

            for (var i = 0; i < relContentItemIds.Count(); i++)
            {
                var relContentItemId          = relContentItemIds[i];
                var contentRelationItemRecord = contentRelations.FirstOrDefault(r =>
                                                                                r.ContentItemRecord1Id == relContentItemId || r.ContentItemRecord2Id == relContentItemId);
                if (contentRelationItemRecord == null)
                {
                    contentRelationItemRecord = new ContentRelationItemRecord()
                    {
                        Uid = Guid.NewGuid().ToString("N"),
                        ContentItemRecord1Id    = contentItemId,
                        ContentItemRecord2Id    = relContentItemId,
                        ContentRelationRecordId = contentRelationRecordId
                    };
                    await _contentRelationItemRepository.SaveAsync(contentRelationItemRecord);
                }
            }

            return(true);
        }
        public Task DeleteAsync(ContentRelationItemRecord contentRelationItemIndex)
        {
            // TODO: Remove this when versioning is implemented.

            // Delete workflow instances first.
            //var workflowInstances = await _session.Query<ContentRelationRecord, ContentRelationIndex>(x => x.WorkflowDefinitionUid == workflowDefinition.Uid).ListAsync();

            //foreach (var workflowInstance in workflowInstances)
            //{
            //    _session.Delete(workflowInstance);
            //}

            // Then delete the workflow definition.
            _session.Delete(contentRelationItemIndex);

            return(Task.CompletedTask);
            //var context = new WorkflowDefinitionDeletedContext(workflowDefinition);
            //await _handlers.InvokeAsync(async x => await x.DeletedAsync(context), _logger);
        }
        //public async Task<IList<WorkflowDefinitionRecord>> GetByStartActivityAsync(string activityName)
        //{
        //    var query = await _session
        //        .Query<WorkflowDefinitionRecord, WorkflowDefinitionStartActivitiesIndex>(index =>
        //            index.StartActivityName == activityName &&
        //            index.IsEnabled)
        //        .ListAsync();

        //    return query.ToList();
        //}

        public Task SaveAsync(ContentRelationItemRecord contentRelationItemIndex)
        {
            var isNew = contentRelationItemIndex.Id == 0;

            _session.Save(contentRelationItemIndex);

            //if (isNew)
            //{
            //    var context = new WorkflowDefinitionCreatedContext(workflowDefinition);
            //    await _handlers.InvokeAsync(async x => await x.CreatedAsync(context), _logger);
            //}
            //else
            //{
            //    var context = new WorkflowDefinitionUpdatedContext(workflowDefinition);
            //    await _handlers.InvokeAsync(async x => await x.UpdatedAsync(context), _logger);
            //}

            return(Task.CompletedTask);
        }