< Summary

Information
Class: Services.Authorization.Services.MenuService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/Authorization/Services/MenuService.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 60
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
.ctor(...)100%11100%
GetUserMenuAsync()100%1010100%
BuildUrl(...)100%22100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/Authorization/Services/MenuService.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonRepositories.Context;
 3using Services.Authorization.Dtos;
 4
 5namespace Services.Authorization.Services;
 6
 7public sealed class MenuService : IMenuService
 8{
 9    private readonly ApplicationDbContext _db;
 10    private readonly IPermissionService _permissions;
 11
 2212    public MenuService(ApplicationDbContext db, IPermissionService permissions)
 13    {
 2214        _db = db;
 2215        _permissions = permissions;
 2216    }
 17
 18    public async Task<List<MenuItemDto>> GetUserMenuAsync(string userId, CancellationToken ct = default)
 19    {
 2320        var bundle = await _permissions.GetUserPermissionsAsync(userId, ct);
 2221        var allowedPageIds = bundle.IsSuperAdmin
 2222            ? null
 3023            : bundle.Pages.Where(p => p.CanView).Select(p => p.PageId).ToHashSet();
 24
 2225        var pages = await _db.AppPages.AsNoTracking()
 2226            .Where(p => p.IsActive && p.IsMenuItem)
 2227            .OrderBy(p => p.SortOrder).ThenBy(p => p.DisplayName)
 2228            .ToListAsync(ct);
 29
 2230        if (allowedPageIds is not null)
 1131            pages = pages.Where(p => allowedPageIds.Contains(p.Id)).ToList();
 32
 8833        var dict = pages.ToDictionary(p => p.Id, p => new MenuItemDto
 5534        {
 5535            PageId = p.Id,
 5536            Code = p.Code,
 5537            DisplayName = p.DisplayName,
 5538            Icon = p.Icon,
 5539            SortOrder = p.SortOrder,
 5540            Url = BuildUrl(p.Area, p.Controller, p.Action)
 5541        });
 42
 2243        var roots = new List<MenuItemDto>();
 11044        foreach (var p in pages)
 45        {
 3346            var node = dict[p.Id];
 3347            if (p.ParentId.HasValue && dict.TryGetValue(p.ParentId.Value, out var parent))
 648                parent.Children.Add(node);
 49            else
 2750                roots.Add(node);
 51        }
 2252        return roots;
 2253    }
 54
 55    private static string BuildUrl(string? area, string controller, string action)
 56    {
 3357        var a = string.IsNullOrWhiteSpace(area) ? string.Empty : "/" + area.Trim('/');
 3358        return $"{a}/{controller}/{action}";
 59    }
 60}