Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions src/Umbraco.Web.BackOffice/Controllers/EntityController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Dynamic;

Check notice on line 1 in src/Umbraco.Web.BackOffice/Controllers/EntityController.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (v13/dev)

✅ Getting better: Lines of Code in a Single File

The lines of code decreases from 1119 to 1107, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in src/Umbraco.Web.BackOffice/Controllers/EntityController.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (v13/dev)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 5.49 to 5.41, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -847,73 +847,85 @@
return NotFound();
}

EntityBasic? MapEntityBasic(IEntitySlim source, string? culture)
{
EntityBasic? target = _umbracoMapper.Map<IEntitySlim, EntityBasic>(source, context =>
{
context.SetCulture(culture);
});

if (target is not null)
{
target.AdditionalData["hasChildren"] = source.HasChildren;
}

return target;
}

UmbracoObjectTypes? objectType = ConvertToObjectType(type);
if (objectType.HasValue)
{
IEnumerable<IEntitySlim> entities;

var startNodeIds = GetStartNodeIds(type);
var startNodePaths = GetStartNodePaths(type);

var ignoreUserStartNodes = IsDataTypeIgnoringUserStartNodes(dataTypeKey);

var culture = ClientCulture();

// root is special: we reduce it to start nodes if the user's start node is not the default, then we need to return their start nodes
if (id == Constants.System.Root && startNodeIds.Length > 0 &&
startNodeIds.Contains(Constants.System.Root) == false && !ignoreUserStartNodes)
startNodeIds.Contains(Constants.System.Root) == false &&
ignoreUserStartNodes == false)
{
return new PagedResult<EntityBasic>(0, 0, 0);
var startNodeEntities = _entityService.GetAll(objectType.Value, startNodeIds).ToList();
IEnumerable<IEntitySlim> pagedStartNodeEntities = startNodeEntities
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize);
return new PagedResult<EntityBasic>(startNodeEntities.Count, pageNumber, pageSize)
{
Items = pagedStartNodeEntities
.Select(source => MapEntityBasic(source, culture))
.WhereNotNull(),
};
}

//adding multiple conditions ,considering id,key & name as filter param
//for id as int
int.TryParse(filter, out int filterAsIntId);
//for key as Guid
Guid.TryParse(filter, out Guid filterAsGuid);
// else proceed as usual
entities = _entityService.GetPagedChildren(
id,
objectType.Value,
pageNumber - 1,
pageSize,
out long totalRecords,
filter.IsNullOrWhiteSpace()
? null
: _sqlContext.Query<IUmbracoEntity>().Where(x => x.Name!.Contains(filter)
|| x.Id == filterAsIntId
|| x.Key == filterAsGuid),
Ordering.By(orderBy, orderDirection));


if (totalRecords == 0)
{
return new PagedResult<EntityBasic>(0, 0, 0);
}

var culture = ClientCulture();
var pagedResult = new PagedResult<EntityBasic>(totalRecords, pageNumber, pageSize)
{
Items = entities
// Filtering out child nodes after getting a paged result is an active choice here, even though the pagination might get off.
// This has been the case with this functionality in Umbraco for a long time.
.Where(entity => ignoreUserStartNodes ||
(objectType == UmbracoObjectTypes.Document || objectType == UmbracoObjectTypes.Media) is false ||
(ContentPermissions.IsInBranchOfStartNode(entity.Path, startNodeIds, startNodePaths, out var hasPathAccess) &&
hasPathAccess))
.Select(source =>
{
EntityBasic? target = _umbracoMapper.Map<IEntitySlim, EntityBasic>(source, context =>
{
context.SetCulture(culture);
context.SetCulture(culture);
});

if (target is not null)
{
//TODO: Why is this here and not in the mapping?
target.AdditionalData["hasChildren"] = source.HasChildren;
}

return target;
}).WhereNotNull()
.Select(source => MapEntityBasic(source, culture))
.WhereNotNull(),

Check notice on line 928 in src/Umbraco.Web.BackOffice/Controllers/EntityController.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (v13/dev)

✅ Getting better: Complex Method

GetPagedChildren decreases in cyclomatic complexity from 23 to 22, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
};

return pagedResult;
Expand Down
Loading