-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: data type service extensions
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/jcdcdev.Umbraco.Core/Extensions/DataTypeServiceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Umbraco.Cms.Core.Models; | ||
using Umbraco.Cms.Core.Services; | ||
using Umbraco.Extensions; | ||
|
||
namespace jcdcdev.Umbraco.Core.Extensions; | ||
|
||
public static class DataTypeServiceExtensions | ||
{ | ||
public static IEnumerable<EntityContainer> GetAllContainers(this IDataTypeService dataTypeService) => dataTypeService.GetContainers(Array.Empty<int>()); | ||
|
||
public static void DeleteAllEmptyContainers(this IDataTypeService dataTypeService) | ||
{ | ||
var dataTypes = dataTypeService.GetAll(); | ||
var lookup = dataTypes.GroupBy(x => x.ParentId).ToLookup(x => x.Key, x => x.Count()); | ||
var containers = dataTypeService.GetAllContainers(); | ||
|
||
foreach (var container in containers) | ||
{ | ||
var hasChildren = lookup.Contains(container.Id); | ||
|
||
if (hasChildren) | ||
{ | ||
continue; | ||
} | ||
|
||
dataTypeService.DeleteContainer(container.Id); | ||
} | ||
} | ||
|
||
public static EntityContainer GetOrCreateFolder( | ||
this IDataTypeService dataTypeService, | ||
string folder, | ||
int parentId = -1) | ||
{ | ||
var dts = dataTypeService | ||
.GetAllContainers() | ||
.FirstOrDefault(x => x.ParentId == parentId && x.Name.InvariantEquals(folder)); | ||
|
||
if (dts == null) | ||
{ | ||
dts = dataTypeService.CreateContainer(parentId, new Guid(), folder).Result?.Entity; | ||
} | ||
|
||
return dts ?? throw new Exception(); | ||
} | ||
} |