< Summary

Information
Class: TreeGrouping.Application.CategoryService.CategoryTreeService
Assembly: TreeGrouping.Application
File(s): /home/runner/work/Studio2Prod/Studio2Prod/TreeGrouping.Application/CategoryService/CategoryTreeService.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 63
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuildTree(...)100%11100%
BuildBranch()100%11100%
CategoryLinkToModel()100%1212100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/TreeGrouping.Application/CategoryService/CategoryTreeService.cs

#LineLine coverage
 1using ConsoleApp1;
 2using TreeGrouping.Application.DbService;
 3using TreeGrouping.Application.DbService.Models;
 4
 5namespace TreeGrouping.Application.CategoryService;
 6
 7public class CategoryTreeService
 8{
 9    public List<CategoryModel> BuildTree(List<CategoryModel> categories)
 10    {
 1511        var lookup = categories.ToLookup(c => c.ParentId);
 12
 13        List<CategoryModel> BuildBranch(int? parentId) =>
 1814            lookup[parentId]
 2715                .Select(c => new CategoryModel
 2716                {
 2717                    Id = c.Id,
 2718                    ParentId = c.ParentId,
 2719                    Name = c.Name,
 2720                    ParentName = c.ParentName,
 2721                    Children = c.Children.Any() ? c.Children : BuildBranch(c.Id),
 2722                    IsFiltred = c.IsFiltred,
 2723                    LinkId = c.LinkId,
 2724                    Links = c.Links,
 2725                    CatChar  = c.CatChar,
 2726                    Translation = c.Translation,
 2727                }).ToList();
 28
 529        return BuildBranch(null).Concat(BuildBranch(0)).ToList();
 30    }
 31
 32
 33    public async Task<List<CategoryModel>> CategoryLinkToModel(IDatabaseService dbService, List<CategoryModel> tree, Lis
 34    {
 2435        foreach (var link in links)
 36        {
 637            if (!Enum.TryParse<CategoryLinkType>(link.LinkTypeName, true, out var linkType))
 38                continue;
 39
 540            var linkTreeTask = await dbService.ExecuteStoredProcedureAsync(linkType.GetProcedureType(), link.LinkCategor
 1041            var linkCategory = linkTreeTask.FirstOrDefault(lc => lc.Id == link.LinkCategoryId);
 42
 1043            var treeItem = tree.FirstOrDefault(tr => tr.Id == link.CtCategoryId);
 544            if (treeItem != null && linkCategory != null)
 45            {
 446                bool linkExists = treeItem.Links.Any(l => l.Id == link.Id);
 347                if (!linkExists)
 48                {
 249                    treeItem.Links.Add(new Link
 250                    {
 251                        CategoryLinkType = linkType,
 252                        CategoryId = linkCategory.Id,
 253                        Id = link.Id
 254                    });
 55                }
 56
 357                treeItem.ParentName ??= link.LinkTypeName; // только если ещё не установлено
 58            }
 559        }
 60
 661        return tree;
 662    }
 63}