Skip to content
Open
Show file tree
Hide file tree
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
63 changes: 63 additions & 0 deletions Dappi.SourceGenerator/CrudGenerator.cs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to implement this on our frontend side too.

When i go to the "Content Manager", I do not get the count or related models now

Image Image

Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ private dynamic GetDbSetForType(string typeName)

private IQueryable<{item.ClassName}> ApplyDynamicIncludes(IQueryable<{item.ClassName}> query)
{{
if (!HttpContext.Request.Query.ContainsKey(""include""))
{{
return query;
}}

var shouldApplyFullIncludes = HttpContext.Request.Query[""include""]
.SelectMany(includeValue => includeValue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check includeValue if null - this generates a warning

.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
.Any(includePath => includePath == ""*"");

if (shouldApplyFullIncludes)
{{
return ApplyFullIncludes(query);
}}

var includeTree = HttpContext.Items[IncludeQueryFilter.IncludeParamsKey] as IDictionary<string, IncludeNode>;
if (includeTree is null || includeTree.Count == 0)
{{
Expand All @@ -165,6 +180,54 @@ private dynamic GetDbSetForType(string typeName)
return query;
}}

private IQueryable<{item.ClassName}> ApplyFullIncludes(IQueryable<{item.ClassName}> query)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for formatting

{{
var rootEntityType = dbContext.Model.FindEntityType(typeof({item.ClassName}));
if (rootEntityType is null)
{{
return query;
}}

var includePaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var visitedTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
visitedTypes.Add(rootEntityType.Name);
var prefix = string.Empty;

CollectIncludePaths(rootEntityType, prefix, includePaths, visitedTypes);

foreach (var includePath in includePaths)
{{
query = query.Include(includePath);
}}

return query;
}}

private static void CollectIncludePaths(
Microsoft.EntityFrameworkCore.Metadata.IEntityType entityType,
string prefix,
HashSet<string> includePaths,
HashSet<string> visitedTypes)
{{
var relations = entityType.GetNavigations();
foreach (var relation in relations)
{{
var entityTypeName = relation.TargetEntityType.Name;
if (visitedTypes.Contains(entityTypeName))
{{
continue;
}}

var navigationPath = string.IsNullOrEmpty(prefix) ? relation.Name : string.Concat(prefix, ""."", relation.Name);

includePaths.Add(navigationPath);
visitedTypes.Add(entityTypeName);

CollectIncludePaths(relation.TargetEntityType, navigationPath, includePaths, visitedTypes);
visitedTypes.Remove(entityTypeName);
}}
}}

private static IQueryable<{item.ClassName}> ApplyIncludeRecursively(IQueryable<{item.ClassName}> query, string path, IncludeNode node)
{{
query = query.Include(path);
Expand Down

This file was deleted.

Loading