Esempio n. 1
0
        public async Task <RemoveArtifactsFromCollectionResult> RemoveArtifactsFromCollectionAsync(
            int collectionId, ItemsRemovalParams removalParams, int userId)
        {
            if (collectionId < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(collectionId));
            }

            if (userId < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(userId));
            }

            RemoveArtifactsFromCollectionResult result = null;

            Func <IDbTransaction, long, Task> action = async(transaction, transactionId) =>
            {
                var collection = await ValidateCollectionAsync(collectionId, userId, transaction);

                var searchArtifactsResult = await _searchEngineService.Search(
                    collection.Id, null, ScopeType.Contents, true, userId, transaction);

                var artifactsToRemove = removalParams.SelectionType == SelectionType.Selected ?
                                        searchArtifactsResult.ArtifactIds.Intersect(removalParams.ItemIds).ToList() :
                                        searchArtifactsResult.ArtifactIds.Except(removalParams.ItemIds).ToList();

                var accessibleArtifacts = await GetAccessibleArtifactsAsync(artifactsToRemove, userId, transaction);

                var accessibleArtifactIds = accessibleArtifacts.Select(a => a.HolderId);

                var removedCount = await _collectionsRepository.RemoveArtifactsFromCollectionAsync(
                    collection.Id, accessibleArtifactIds, userId, transaction);

                result = new RemoveArtifactsFromCollectionResult
                {
                    RemovedCount = removedCount,
                    Total        = removalParams.SelectionType == SelectionType.Selected ?
                                   removalParams.ItemIds.Count() :
                                   searchArtifactsResult.ArtifactIds.Except(removalParams.ItemIds).Count()
                };
            };

            await _sqlHelper.RunInTransactionAsync(ServiceConstants.RaptorMain, action);

            return(result);
        }