using Microsoft.EntityFrameworkCore; using Tiku.Application.Learning; using Tiku.Infrastructure.Persistence; namespace Tiku.Infrastructure.Learning; internal sealed class StudentLearningCatalogService( ILearningAccessPersistence access, IEffectiveLearningAccessService effectiveAccess) : IStudentLearningCatalogService { public async Task GetAsync( LearningActor actor, Guid businessLineId, CancellationToken cancellationToken = default) { var snapshot = await effectiveAccess.GetSnapshotAsync(actor, businessLineId, cancellationToken); var manifestIds = snapshot.ManifestVersionIds.ToArray(); var rows = await access.ProductManifestResources.AsNoTracking() .Where(item => item.TenantId == actor.TenantId && manifestIds.Contains(item.ProductAccessManifestVersionId)) .OrderBy(item => item.SortOrder) .ThenBy(item => item.Id) .ToArrayAsync(cancellationToken); var resources = rows .GroupBy(item => new { item.ResourceType, item.ResourceId }) .Select(group => { var first = group.OrderBy(item => item.SortOrder).ThenBy(item => item.Id).First(); return new StudentCatalogResourceItem( first.ResourceType, first.ResourceId, string.IsNullOrWhiteSpace(first.DisplayName) ? first.ResourceType.ToString() : first.DisplayName, first.SortOrder, group.Select(item => item.ProductAccessManifestVersionId).Distinct().Order().ToArray()); }) .OrderBy(item => item.SortOrder) .ThenBy(item => item.DisplayName) .ThenBy(item => item.ResourceId) .ToArray(); return new StudentLearningCatalogItem( businessLineId, snapshot.PrimaryProfileVersionId, snapshot.AlternateProfileVersionIds.Order().ToArray(), resources, snapshot.GrantVersion, snapshot.ContentVersion); } }