Skip to content

Commit 63ed1ee

Browse files
AndyButlandCopilot
andauthored
Adds support for the "folders only" flag on retrieving siblings of a node. (#19861)
* Adds support for the "folders only" flag on retrieving siblings of a node. * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * Updated test code. * Removed double secondary ordering by node Id and ensured we include this clause for all sort orders. * Ensure that ordering by node Id is always added only once and last, and only if it's not already been included in the order by clause. --------- Co-authored-by: Copilot <[email protected]>
1 parent eb986d9 commit 63ed1ee

File tree

17 files changed

+203
-86
lines changed

17 files changed

+203
-86
lines changed

src/Umbraco.Cms.Api.Management/Controllers/DataType/Tree/SiblingsDataTypeTreeController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public SiblingsDataTypeTreeController(IEntityService entityService, IDataTypeSer
1515

1616
[HttpGet("siblings")]
1717
[ProducesResponseType(typeof(SubsetViewModel<DataTypeTreeItemResponseModel>), StatusCodes.Status200OK)]
18-
public Task<ActionResult<SubsetViewModel<DataTypeTreeItemResponseModel>>> Siblings(CancellationToken cancellationToken, Guid target, int before, int after)
19-
=> GetSiblings(target, before, after);
18+
public async Task<ActionResult<SubsetViewModel<DataTypeTreeItemResponseModel>>> Siblings(CancellationToken cancellationToken, Guid target, int before, int after, bool foldersOnly = false)
19+
{
20+
RenderFoldersOnly(foldersOnly);
21+
return await GetSiblings(target, before, after);
22+
}
2023
}

src/Umbraco.Cms.Api.Management/Controllers/Document/Tree/SiblingsDocumentTreeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public SiblingsDocumentTreeController(
3636
[HttpGet("siblings")]
3737
[MapToApiVersion("1.0")]
3838
[ProducesResponseType(typeof(SubsetViewModel<DocumentTreeItemResponseModel>), StatusCodes.Status200OK)]
39-
public Task<ActionResult<SubsetViewModel<DocumentTreeItemResponseModel>>> Siblings(CancellationToken cancellationToken, Guid target, int before, int after, Guid? dataTypeId = null)
39+
public async Task<ActionResult<SubsetViewModel<DocumentTreeItemResponseModel>>> Siblings(CancellationToken cancellationToken, Guid target, int before, int after, Guid? dataTypeId = null)
4040
{
4141
IgnoreUserStartNodesForDataType(dataTypeId);
42-
return GetSiblings(target, before, after);
42+
return await GetSiblings(target, before, after);
4343
}
4444
}

src/Umbraco.Cms.Api.Management/Controllers/DocumentBlueprint/Tree/SiblingsDocumentBlueprintTreeController.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ public SiblingsDocumentBlueprintTreeController(IEntityService entityService, IDo
1616

1717
[HttpGet("siblings")]
1818
[ProducesResponseType(typeof(SubsetViewModel<DocumentBlueprintTreeItemResponseModel>), StatusCodes.Status200OK)]
19-
public Task<ActionResult<SubsetViewModel<DocumentBlueprintTreeItemResponseModel>>> Siblings(
19+
public async Task<ActionResult<SubsetViewModel<DocumentBlueprintTreeItemResponseModel>>> Siblings(
2020
CancellationToken cancellationToken,
2121
Guid target,
2222
int before,
23-
int after) =>
24-
GetSiblings(target, before, after);
23+
int after,
24+
bool foldersOnly = false)
25+
{
26+
RenderFoldersOnly(foldersOnly);
27+
return await GetSiblings(target, before, after);
28+
}
2529
}

src/Umbraco.Cms.Api.Management/Controllers/DocumentType/Tree/SiblingsDocumentTypeTreeController.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ public SiblingsDocumentTypeTreeController(IEntityService entityService, IContent
1515

1616
[HttpGet("siblings")]
1717
[ProducesResponseType(typeof(SubsetViewModel<DocumentTypeTreeItemResponseModel>), StatusCodes.Status200OK)]
18-
public Task<ActionResult<SubsetViewModel<DocumentTypeTreeItemResponseModel>>> Siblings(
18+
public async Task<ActionResult<SubsetViewModel<DocumentTypeTreeItemResponseModel>>> Siblings(
1919
CancellationToken cancellationToken,
2020
Guid target,
2121
int before,
22-
int after) =>
23-
GetSiblings(target, before, after);
22+
int after,
23+
bool foldersOnly = false)
24+
{
25+
RenderFoldersOnly(foldersOnly);
26+
return await GetSiblings(target, before, after);
27+
}
2428
}

src/Umbraco.Cms.Api.Management/Controllers/Media/Tree/SiblingsMediaTreeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public SiblingsMediaTreeController(
2525

2626
[HttpGet("siblings")]
2727
[ProducesResponseType(typeof(SubsetViewModel<MediaTreeItemResponseModel>), StatusCodes.Status200OK)]
28-
public Task<ActionResult<SubsetViewModel<MediaTreeItemResponseModel>>> Siblings(CancellationToken cancellationToken, Guid target, int before, int after, Guid? dataTypeId = null)
28+
public async Task<ActionResult<SubsetViewModel<MediaTreeItemResponseModel>>> Siblings(CancellationToken cancellationToken, Guid target, int before, int after, Guid? dataTypeId = null)
2929
{
3030
IgnoreUserStartNodesForDataType(dataTypeId);
31-
return GetSiblings(target, before, after);
31+
return await GetSiblings(target, before, after);
3232
}
3333
}

src/Umbraco.Cms.Api.Management/Controllers/MediaType/Tree/SiblingsMediaTypeTreeController.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public SiblingsMediaTypeTreeController(IEntityService entityService, IMediaTypeS
1515

1616
[HttpGet("siblings")]
1717
[ProducesResponseType(typeof(SubsetViewModel<MediaTypeTreeItemResponseModel>), StatusCodes.Status200OK)]
18-
public Task<ActionResult<SubsetViewModel<MediaTypeTreeItemResponseModel>>> Siblings(CancellationToken cancellationToken, Guid target, int before, int after)
19-
=> GetSiblings(target, before, after);
18+
public async Task<ActionResult<SubsetViewModel<MediaTypeTreeItemResponseModel>>> Siblings(
19+
CancellationToken cancellationToken,
20+
Guid target,
21+
int before,
22+
int after,
23+
bool foldersOnly = false)
24+
{
25+
RenderFoldersOnly(foldersOnly);
26+
return await GetSiblings(target, before, after);
27+
}
2028
}

src/Umbraco.Cms.Api.Management/Controllers/Template/Tree/SiblingsTemplateTreeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public SiblingsTemplateTreeController(IEntityService entityService)
1515

1616
[HttpGet("siblings")]
1717
[ProducesResponseType(typeof(SubsetViewModel<NamedEntityTreeItemResponseModel>), StatusCodes.Status200OK)]
18-
public Task<ActionResult<SubsetViewModel<NamedEntityTreeItemResponseModel>>> Siblings(
18+
public async Task<ActionResult<SubsetViewModel<NamedEntityTreeItemResponseModel>>> Siblings(
1919
CancellationToken cancellationToken,
2020
Guid target,
2121
int before,
2222
int after) =>
23-
GetSiblings(target, before, after);
23+
await GetSiblings(target, before, after);
2424
}

src/Umbraco.Cms.Api.Management/Controllers/Tree/EntityTreeControllerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected virtual IEntitySlim[] GetSiblingEntities(Guid target, int before, int
127127
EntityService
128128
.GetSiblings(
129129
target,
130-
ItemObjectType,
130+
[ItemObjectType],
131131
before,
132132
after,
133133
out totalBefore,

src/Umbraco.Cms.Api.Management/Controllers/Tree/FolderTreeControllerBase.cs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ protected override IEntitySlim[] GetPagedChildEntities(Guid parentKey, int skip,
5151
take,
5252
out totalItems);
5353

54+
protected override IEntitySlim[] GetSiblingEntities(Guid target, int before, int after, out long totalBefore, out long totalAfter)
55+
{
56+
totalBefore = 0;
57+
totalAfter = 0;
58+
59+
UmbracoObjectTypes[] siblingObjectTypes = GetObjectTypes();
60+
61+
return EntityService.GetSiblings(
62+
target,
63+
siblingObjectTypes,
64+
before,
65+
after,
66+
out totalBefore,
67+
out totalAfter,
68+
ordering: ItemOrdering)
69+
.ToArray();
70+
}
71+
5472
protected override TItem MapTreeItemViewModel(Guid? parentKey, IEntitySlim entity)
5573
{
5674
TItem viewModel = base.MapTreeItemViewModel(parentKey, entity);
@@ -93,19 +111,19 @@ private IEntitySlim[] GetEntities(Guid? parentKey, int skip, int take, out long
93111
{
94112
totalItems = 0;
95113

96-
UmbracoObjectTypes[] childObjectTypes = _foldersOnly ? [FolderObjectType] : [FolderObjectType, ItemObjectType];
97-
98-
IEntitySlim[] itemEntities = EntityService.GetPagedChildren(
99-
parentKey,
100-
[FolderObjectType, ItemObjectType],
101-
childObjectTypes,
102-
skip,
103-
take,
104-
false,
105-
out totalItems,
106-
ordering: ItemOrdering)
107-
.ToArray();
108-
109-
return itemEntities;
114+
UmbracoObjectTypes[] childObjectTypes = GetObjectTypes();
115+
116+
return EntityService.GetPagedChildren(
117+
parentKey,
118+
[FolderObjectType, ItemObjectType],
119+
childObjectTypes,
120+
skip,
121+
take,
122+
false,
123+
out totalItems,
124+
ordering: ItemOrdering)
125+
.ToArray();
110126
}
127+
128+
private UmbracoObjectTypes[] GetObjectTypes() => _foldersOnly ? [FolderObjectType] : [FolderObjectType, ItemObjectType];
111129
}

src/Umbraco.Cms.Api.Management/Services/Entities/UserStartNodeEntitiesService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ out long totalAfter
190190
if (userStartNodePaths.Any(path => $"{targetParent?.Path},".StartsWith($"{path},")))
191191
{
192192
// The requested parent of the target is one of the user start nodes (or a descendant of one), all siblings are by definition allowed.
193-
siblings = _entityService.GetSiblings(targetKey, umbracoObjectType, before, after, out totalBefore, out totalAfter, ordering: ordering).ToArray();
193+
siblings = _entityService.GetSiblings(targetKey, [umbracoObjectType], before, after, out totalBefore, out totalAfter, ordering: ordering).ToArray();
194194
return ChildUserAccessEntities(siblings, userStartNodePaths);
195195
}
196196

@@ -206,7 +206,7 @@ out long totalAfter
206206

207207
// Even though we know the IDs of the allowed sibling entities to fetch, we still use a Query to yield correctly sorted children.
208208
IQuery<IUmbracoEntity> query = _scopeProvider.CreateQuery<IUmbracoEntity>().Where(x => allowedSiblingIds.Contains(x.Id));
209-
siblings = _entityService.GetSiblings(targetKey, umbracoObjectType, before, after, out totalBefore, out totalAfter, query, ordering).ToArray();
209+
siblings = _entityService.GetSiblings(targetKey, [umbracoObjectType], before, after, out totalBefore, out totalAfter, query, ordering).ToArray();
210210
return ChildUserAccessEntities(siblings, userStartNodePaths);
211211
}
212212

0 commit comments

Comments
 (0)