private Task <IResultList <IAssetFolderEntity> > GetAssetFoldersAsync(DomainId parentId, AssetFolderScope scope)
        {
            if (scope == AssetFolderScope.Path)
            {
                return(Task.FromResult(ResultList.CreateFrom <IAssetFolderEntity>(0)));
            }

            return(assetQuery.QueryAssetFoldersAsync(Context, parentId, HttpContext.RequestAborted));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetAssetFolders(string app, [FromQuery] Guid parentId)
        {
            var assetFolders = await assetQuery.QueryAssetFoldersAsync(Context, parentId);

            var response = Deferred.Response(() =>
            {
                return(AssetFoldersDto.FromAssets(assetFolders, Resources));
            });

            Response.Headers[HeaderNames.ETag] = assetFolders.ToEtag();

            return(Ok(response));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetAssetFolders(string app, [FromQuery] string parentId)
        {
            var(folders, path) = await AsyncHelper.WhenAll(
                assetQuery.QueryAssetFoldersAsync(Context, parentId),
                assetQuery.FindAssetFolderAsync(Context.App.Id, parentId));

            var response = Deferred.Response(() =>
            {
                return(AssetFoldersDto.FromAssets(folders, path, Resources));
            });

            Response.Headers[HeaderNames.ETag] = folders.ToEtag();

            return(Ok(response));
        }
Ejemplo n.º 4
0
        public async Task Should_cache_folders_on_same_level()
        {
            var folder11 = CreateFolder("level1_1");
            var folder12 = CreateFolder("level1_2");

            A.CallTo(() => assetQuery.QueryAssetFoldersAsync(requestContext, DomainId.Empty))
            .Returns(ResultList.CreateFrom(2, folder11, folder12));

            var folderId11 = await sut.ResolveOrCreateAsync(requestContext, commandBus, folder11.FolderName);

            var folderId12 = await sut.ResolveOrCreateAsync(requestContext, commandBus, folder12.FolderName);

            Assert.Equal(folder11.Id, folderId11);
            Assert.Equal(folder12.Id, folderId12);

            A.CallTo(() => assetQuery.QueryAssetFoldersAsync(requestContext, A <DomainId> ._))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => commandBus.PublishAsync(A <ICommand> ._))
            .MustNotHaveHappened();
        }
Ejemplo n.º 5
0
        public async Task <DomainId> ResolveOrCreateAsync(Context context, ICommandBus commandBus, string path)
        {
            Guard.NotNull(commandBus, nameof(commandBus));
            Guard.NotNull(path, nameof(path));

            path = path.Trim(TrimChars);

            var elements = path.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries);

            if (elements.Length == 0)
            {
                return(DomainId.Empty);
            }

            var currentId = DomainId.Empty;

            var i = elements.Length;

            for (; i > 0; i--)
            {
                var subPath = string.Join('/', elements.Take(i));

                if (localCache.TryGetValue(GetCacheKey(subPath), out var cached) && cached is DomainId id)
                {
                    currentId = id;
                    break;
                }
            }

            var creating = false;

            for (; i < elements.Length; i++)
            {
                var name = elements[i];

                var isResolved = false;

                if (!creating)
                {
                    var children = await assetQuery.QueryAssetFoldersAsync(context, currentId);

                    foreach (var child in children)
                    {
                        var childPath = string.Join('/', elements.Take(i).Union(Enumerable.Repeat(child.FolderName, 1)));

                        localCache.Add(GetCacheKey(childPath), child.Id);
                    }

                    foreach (var child in children)
                    {
                        if (child.FolderName == name)
                        {
                            currentId = child.Id;

                            isResolved = true;
                            break;
                        }
                    }
                }

                if (!isResolved)
                {
                    var command = new CreateAssetFolder {
                        ParentId = currentId, FolderName = name
                    };

                    await commandBus.PublishAsync(command);

                    currentId = command.AssetFolderId;
                    creating  = true;
                }

                var newPath = string.Join('/', elements.Take(i).Union(Enumerable.Repeat(name, 1)));

                localCache.Add(GetCacheKey(newPath), currentId);
            }

            return(currentId);
        }