| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Linq; |
| | | 7 | | using Jellyfin.Data.Enums; |
| | | 8 | | using Jellyfin.Database.Implementations; |
| | | 9 | | using Jellyfin.Database.Implementations.Entities; |
| | | 10 | | using Jellyfin.Extensions; |
| | | 11 | | using MediaBrowser.Controller.Entities; |
| | | 12 | | using MediaBrowser.Controller.Persistence; |
| | | 13 | | using MediaBrowser.Model.Dto; |
| | | 14 | | using Microsoft.EntityFrameworkCore; |
| | | 15 | | |
| | | 16 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Provides item counting and played-status query operations. |
| | | 20 | | /// </summary> |
| | | 21 | | public class ItemCountService : IItemCountService |
| | | 22 | | { |
| | | 23 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 24 | | private readonly IItemTypeLookup _itemTypeLookup; |
| | | 25 | | private readonly IItemQueryHelpers _queryHelpers; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="ItemCountService"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 31 | | /// <param name="itemTypeLookup">The item type lookup.</param> |
| | | 32 | | /// <param name="queryHelpers">The shared query helpers.</param> |
| | | 33 | | public ItemCountService( |
| | | 34 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 35 | | IItemTypeLookup itemTypeLookup, |
| | | 36 | | IItemQueryHelpers queryHelpers) |
| | | 37 | | { |
| | 21 | 38 | | _dbProvider = dbProvider; |
| | 21 | 39 | | _itemTypeLookup = itemTypeLookup; |
| | 21 | 40 | | _queryHelpers = queryHelpers; |
| | 21 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc/> |
| | | 44 | | public int GetCount(InternalItemsQuery filter) |
| | | 45 | | { |
| | 0 | 46 | | ArgumentNullException.ThrowIfNull(filter); |
| | 0 | 47 | | _queryHelpers.PrepareFilterQuery(filter); |
| | | 48 | | |
| | 0 | 49 | | using var context = _dbProvider.CreateDbContext(); |
| | 0 | 50 | | var dbQuery = _queryHelpers.TranslateQuery(context.BaseItems.AsNoTracking(), context, filter); |
| | | 51 | | |
| | 0 | 52 | | return dbQuery.Count(); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public ItemCounts GetItemCounts(InternalItemsQuery filter) |
| | | 57 | | { |
| | 0 | 58 | | ArgumentNullException.ThrowIfNull(filter); |
| | 0 | 59 | | _queryHelpers.PrepareFilterQuery(filter); |
| | | 60 | | |
| | 0 | 61 | | using var context = _dbProvider.CreateDbContext(); |
| | 0 | 62 | | var dbQuery = _queryHelpers.TranslateQuery(context.BaseItems.AsNoTracking(), context, filter); |
| | | 63 | | |
| | 0 | 64 | | var counts = dbQuery |
| | 0 | 65 | | .GroupBy(x => x.Type) |
| | 0 | 66 | | .Select(x => new { x.Key, Count = x.Count() }) |
| | 0 | 67 | | .ToArray(); |
| | | 68 | | |
| | 0 | 69 | | var lookup = _itemTypeLookup.BaseItemKindNames; |
| | 0 | 70 | | var result = new ItemCounts |
| | 0 | 71 | | { |
| | 0 | 72 | | ItemCount = counts.Sum(c => c.Count) |
| | 0 | 73 | | }; |
| | 0 | 74 | | foreach (var count in counts) |
| | | 75 | | { |
| | 0 | 76 | | if (string.Equals(count.Key, lookup[BaseItemKind.MusicAlbum], StringComparison.Ordinal)) |
| | | 77 | | { |
| | 0 | 78 | | result.AlbumCount = count.Count; |
| | | 79 | | } |
| | 0 | 80 | | else if (string.Equals(count.Key, lookup[BaseItemKind.MusicArtist], StringComparison.Ordinal)) |
| | | 81 | | { |
| | 0 | 82 | | result.ArtistCount = count.Count; |
| | | 83 | | } |
| | 0 | 84 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Episode], StringComparison.Ordinal)) |
| | | 85 | | { |
| | 0 | 86 | | result.EpisodeCount = count.Count; |
| | | 87 | | } |
| | 0 | 88 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Movie], StringComparison.Ordinal)) |
| | | 89 | | { |
| | 0 | 90 | | result.MovieCount = count.Count; |
| | | 91 | | } |
| | 0 | 92 | | else if (string.Equals(count.Key, lookup[BaseItemKind.MusicVideo], StringComparison.Ordinal)) |
| | | 93 | | { |
| | 0 | 94 | | result.MusicVideoCount = count.Count; |
| | | 95 | | } |
| | 0 | 96 | | else if (string.Equals(count.Key, lookup[BaseItemKind.LiveTvProgram], StringComparison.Ordinal)) |
| | | 97 | | { |
| | 0 | 98 | | result.ProgramCount = count.Count; |
| | | 99 | | } |
| | 0 | 100 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Series], StringComparison.Ordinal)) |
| | | 101 | | { |
| | 0 | 102 | | result.SeriesCount = count.Count; |
| | | 103 | | } |
| | 0 | 104 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Audio], StringComparison.Ordinal)) |
| | | 105 | | { |
| | 0 | 106 | | result.SongCount = count.Count; |
| | | 107 | | } |
| | 0 | 108 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Trailer], StringComparison.Ordinal)) |
| | | 109 | | { |
| | 0 | 110 | | result.TrailerCount = count.Count; |
| | | 111 | | } |
| | 0 | 112 | | else if (string.Equals(count.Key, lookup[BaseItemKind.BoxSet], StringComparison.Ordinal)) |
| | | 113 | | { |
| | 0 | 114 | | result.BoxSetCount = count.Count; |
| | | 115 | | } |
| | 0 | 116 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Book], StringComparison.Ordinal)) |
| | | 117 | | { |
| | 0 | 118 | | result.BookCount = count.Count; |
| | | 119 | | } |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | return result; |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <inheritdoc /> |
| | | 126 | | public ItemCounts GetItemCountsForNameItem(BaseItemKind kind, Guid id, BaseItemKind[] relatedItemKinds, InternalItem |
| | | 127 | | { |
| | 0 | 128 | | using var context = _dbProvider.CreateDbContext(); |
| | | 129 | | |
| | 0 | 130 | | var item = context.BaseItems.AsNoTracking() |
| | 0 | 131 | | .Where(e => e.Id == id) |
| | 0 | 132 | | .Select(e => new { e.Name, e.CleanName }) |
| | 0 | 133 | | .FirstOrDefault(); |
| | | 134 | | |
| | 0 | 135 | | if (item is null) |
| | | 136 | | { |
| | 0 | 137 | | return new ItemCounts(); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | IQueryable<BaseItemEntity> baseQuery; |
| | | 141 | | switch (kind) |
| | | 142 | | { |
| | | 143 | | case BaseItemKind.Person: |
| | 0 | 144 | | baseQuery = context.PeopleBaseItemMap |
| | 0 | 145 | | .AsNoTracking() |
| | 0 | 146 | | .Where(m => m.People.Name == item.Name) |
| | 0 | 147 | | .Select(m => m.Item); |
| | 0 | 148 | | break; |
| | | 149 | | case BaseItemKind.MusicArtist: |
| | 0 | 150 | | baseQuery = context.ItemValuesMap |
| | 0 | 151 | | .AsNoTracking() |
| | 0 | 152 | | .Where(ivm => ivm.ItemValue.CleanValue == item.CleanName |
| | 0 | 153 | | && (ivm.ItemValue.Type == ItemValueType.Artist || ivm.ItemValue.Type == ItemValueType.AlbumArtis |
| | 0 | 154 | | .Select(ivm => ivm.Item); |
| | 0 | 155 | | break; |
| | | 156 | | case BaseItemKind.Genre: |
| | | 157 | | case BaseItemKind.MusicGenre: |
| | 0 | 158 | | baseQuery = context.ItemValuesMap |
| | 0 | 159 | | .AsNoTracking() |
| | 0 | 160 | | .Where(ivm => ivm.ItemValue.CleanValue == item.CleanName |
| | 0 | 161 | | && ivm.ItemValue.Type == ItemValueType.Genre) |
| | 0 | 162 | | .Select(ivm => ivm.Item); |
| | 0 | 163 | | break; |
| | | 164 | | case BaseItemKind.Studio: |
| | 0 | 165 | | baseQuery = context.ItemValuesMap |
| | 0 | 166 | | .AsNoTracking() |
| | 0 | 167 | | .Where(ivm => ivm.ItemValue.CleanValue == item.CleanName |
| | 0 | 168 | | && ivm.ItemValue.Type == ItemValueType.Studios) |
| | 0 | 169 | | .Select(ivm => ivm.Item); |
| | 0 | 170 | | break; |
| | | 171 | | case BaseItemKind.Year: |
| | 0 | 172 | | if (int.TryParse(item.Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year)) |
| | | 173 | | { |
| | 0 | 174 | | baseQuery = context.BaseItems |
| | 0 | 175 | | .AsNoTracking() |
| | 0 | 176 | | .Where(e => e.ProductionYear == year); |
| | | 177 | | } |
| | | 178 | | else |
| | | 179 | | { |
| | 0 | 180 | | return new ItemCounts(); |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | break; |
| | | 184 | | default: |
| | 0 | 185 | | return new ItemCounts(); |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | var typeNames = relatedItemKinds.Select(k => _itemTypeLookup.BaseItemKindNames[k]).ToArray(); |
| | 0 | 189 | | baseQuery = baseQuery.Where(e => typeNames.Contains(e.Type)); |
| | | 190 | | |
| | 0 | 191 | | baseQuery = _queryHelpers.ApplyAccessFiltering(context, baseQuery, accessFilter); |
| | | 192 | | |
| | 0 | 193 | | var counts = baseQuery |
| | 0 | 194 | | .GroupBy(x => x.Type) |
| | 0 | 195 | | .Select(x => new { x.Key, Count = x.Count() }) |
| | 0 | 196 | | .ToArray(); |
| | | 197 | | |
| | 0 | 198 | | var lookup = _itemTypeLookup.BaseItemKindNames; |
| | 0 | 199 | | var result = new ItemCounts(); |
| | 0 | 200 | | var totalCount = 0; |
| | | 201 | | |
| | 0 | 202 | | foreach (var count in counts) |
| | | 203 | | { |
| | 0 | 204 | | totalCount += count.Count; |
| | | 205 | | |
| | 0 | 206 | | if (string.Equals(count.Key, lookup[BaseItemKind.MusicAlbum], StringComparison.Ordinal)) |
| | | 207 | | { |
| | 0 | 208 | | result.AlbumCount = count.Count; |
| | | 209 | | } |
| | 0 | 210 | | else if (string.Equals(count.Key, lookup[BaseItemKind.MusicArtist], StringComparison.Ordinal)) |
| | | 211 | | { |
| | 0 | 212 | | result.ArtistCount = count.Count; |
| | | 213 | | } |
| | 0 | 214 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Episode], StringComparison.Ordinal)) |
| | | 215 | | { |
| | 0 | 216 | | result.EpisodeCount = count.Count; |
| | | 217 | | } |
| | 0 | 218 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Movie], StringComparison.Ordinal)) |
| | | 219 | | { |
| | 0 | 220 | | result.MovieCount = count.Count; |
| | | 221 | | } |
| | 0 | 222 | | else if (string.Equals(count.Key, lookup[BaseItemKind.MusicVideo], StringComparison.Ordinal)) |
| | | 223 | | { |
| | 0 | 224 | | result.MusicVideoCount = count.Count; |
| | | 225 | | } |
| | 0 | 226 | | else if (string.Equals(count.Key, lookup[BaseItemKind.LiveTvProgram], StringComparison.Ordinal)) |
| | | 227 | | { |
| | 0 | 228 | | result.ProgramCount = count.Count; |
| | | 229 | | } |
| | 0 | 230 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Series], StringComparison.Ordinal)) |
| | | 231 | | { |
| | 0 | 232 | | result.SeriesCount = count.Count; |
| | | 233 | | } |
| | 0 | 234 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Audio], StringComparison.Ordinal)) |
| | | 235 | | { |
| | 0 | 236 | | result.SongCount = count.Count; |
| | | 237 | | } |
| | 0 | 238 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Trailer], StringComparison.Ordinal)) |
| | | 239 | | { |
| | 0 | 240 | | result.TrailerCount = count.Count; |
| | | 241 | | } |
| | 0 | 242 | | else if (string.Equals(count.Key, lookup[BaseItemKind.BoxSet], StringComparison.Ordinal)) |
| | | 243 | | { |
| | 0 | 244 | | result.BoxSetCount = count.Count; |
| | | 245 | | } |
| | 0 | 246 | | else if (string.Equals(count.Key, lookup[BaseItemKind.Book], StringComparison.Ordinal)) |
| | | 247 | | { |
| | 0 | 248 | | result.BookCount = count.Count; |
| | | 249 | | } |
| | | 250 | | } |
| | | 251 | | |
| | 0 | 252 | | result.ItemCount = totalCount; |
| | | 253 | | |
| | 0 | 254 | | return result; |
| | 0 | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <inheritdoc/> |
| | | 258 | | public int GetPlayedCount(InternalItemsQuery filter, Guid ancestorId) |
| | | 259 | | { |
| | 0 | 260 | | ArgumentNullException.ThrowIfNull(filter.User); |
| | 0 | 261 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | | 262 | | |
| | 0 | 263 | | var baseQuery = _queryHelpers.BuildAccessFilteredDescendantsQuery(dbContext, filter, ancestorId); |
| | 0 | 264 | | return baseQuery.Count(b => b.UserData!.Any(u => u.UserId == filter.User.Id && u.Played)); |
| | 0 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <inheritdoc/> |
| | | 268 | | public int GetTotalCount(InternalItemsQuery filter, Guid ancestorId) |
| | | 269 | | { |
| | 0 | 270 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | | 271 | | |
| | 0 | 272 | | var baseQuery = _queryHelpers.BuildAccessFilteredDescendantsQuery(dbContext, filter, ancestorId); |
| | 0 | 273 | | return baseQuery.Count(); |
| | 0 | 274 | | } |
| | | 275 | | |
| | | 276 | | /// <inheritdoc/> |
| | | 277 | | public (int Played, int Total) GetPlayedAndTotalCount(InternalItemsQuery filter, Guid ancestorId) |
| | | 278 | | { |
| | 0 | 279 | | ArgumentNullException.ThrowIfNull(filter); |
| | 0 | 280 | | ArgumentNullException.ThrowIfNull(filter.User); |
| | 0 | 281 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | | 282 | | |
| | 0 | 283 | | var baseQuery = _queryHelpers.BuildAccessFilteredDescendantsQuery(dbContext, filter, ancestorId); |
| | 0 | 284 | | return GetPlayedAndTotalCountFromQuery(baseQuery, filter.User.Id); |
| | 0 | 285 | | } |
| | | 286 | | |
| | | 287 | | /// <inheritdoc/> |
| | | 288 | | public (int Played, int Total) GetPlayedAndTotalCountFromLinkedChildren(InternalItemsQuery filter, Guid parentId) |
| | | 289 | | { |
| | 0 | 290 | | ArgumentNullException.ThrowIfNull(filter); |
| | 0 | 291 | | ArgumentNullException.ThrowIfNull(filter.User); |
| | 0 | 292 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | | 293 | | |
| | 0 | 294 | | var allDescendantIds = DescendantQueryHelper.GetAllDescendantIds(dbContext, parentId); |
| | 0 | 295 | | var baseQuery = dbContext.BaseItems |
| | 0 | 296 | | .Where(b => allDescendantIds.Contains(b.Id) && !b.IsFolder && !b.IsVirtualItem); |
| | 0 | 297 | | baseQuery = _queryHelpers.ApplyAccessFiltering(dbContext, baseQuery, filter); |
| | | 298 | | |
| | 0 | 299 | | return GetPlayedAndTotalCountFromQuery(baseQuery, filter.User.Id); |
| | 0 | 300 | | } |
| | | 301 | | |
| | | 302 | | /// <inheritdoc/> |
| | | 303 | | public Dictionary<Guid, int> GetChildCountBatch(IReadOnlyList<Guid> parentIds, Guid? userId) |
| | | 304 | | { |
| | 0 | 305 | | ArgumentNullException.ThrowIfNull(parentIds); |
| | | 306 | | |
| | 0 | 307 | | if (parentIds.Count == 0) |
| | | 308 | | { |
| | 0 | 309 | | return new Dictionary<Guid, int>(); |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | | 313 | | |
| | 0 | 314 | | var parentIdsArray = parentIds.ToArray(); |
| | | 315 | | |
| | 0 | 316 | | var hierarchicalCounts = dbContext.BaseItems |
| | 0 | 317 | | .Where(b => b.ParentId.HasValue && parentIdsArray.Contains(b.ParentId.Value)) |
| | 0 | 318 | | .GroupBy(b => b.ParentId!.Value) |
| | 0 | 319 | | .Select(g => new { ParentId = g.Key, Count = g.Count() }) |
| | 0 | 320 | | .ToDictionary(x => x.ParentId, x => x.Count); |
| | | 321 | | |
| | 0 | 322 | | var linkedCounts = dbContext.LinkedChildren |
| | 0 | 323 | | .Where(lc => parentIdsArray.Contains(lc.ParentId)) |
| | 0 | 324 | | .GroupBy(lc => lc.ParentId) |
| | 0 | 325 | | .Select(g => new { ParentId = g.Key, Count = g.Count() }) |
| | 0 | 326 | | .ToDictionary(x => x.ParentId, x => x.Count); |
| | | 327 | | |
| | 0 | 328 | | var result = new Dictionary<Guid, int>(); |
| | 0 | 329 | | foreach (var parentId in parentIds) |
| | | 330 | | { |
| | 0 | 331 | | var hierarchicalCount = hierarchicalCounts.GetValueOrDefault(parentId, 0); |
| | 0 | 332 | | var linkedCount = linkedCounts.GetValueOrDefault(parentId, 0); |
| | | 333 | | |
| | 0 | 334 | | result[parentId] = linkedCount > 0 ? linkedCount : hierarchicalCount; |
| | | 335 | | } |
| | | 336 | | |
| | 0 | 337 | | return result; |
| | 0 | 338 | | } |
| | | 339 | | |
| | | 340 | | /// <inheritdoc/> |
| | | 341 | | public Dictionary<Guid, (int Played, int Total)> GetPlayedAndTotalCountBatch(IReadOnlyList<Guid> folderIds, User use |
| | | 342 | | { |
| | 0 | 343 | | ArgumentNullException.ThrowIfNull(folderIds); |
| | 0 | 344 | | ArgumentNullException.ThrowIfNull(user); |
| | | 345 | | |
| | 0 | 346 | | if (folderIds.Count == 0) |
| | | 347 | | { |
| | 0 | 348 | | return new Dictionary<Guid, (int Played, int Total)>(); |
| | | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | using var dbContext = _dbProvider.CreateDbContext(); |
| | 0 | 352 | | var folderIdsArray = folderIds.ToArray(); |
| | 0 | 353 | | var filter = new InternalItemsQuery(user); |
| | 0 | 354 | | var userId = user.Id; |
| | | 355 | | |
| | 0 | 356 | | var leafItems = dbContext.BaseItems |
| | 0 | 357 | | .Where(b => !b.IsFolder && !b.IsVirtualItem); |
| | 0 | 358 | | leafItems = _queryHelpers.ApplyAccessFiltering(dbContext, leafItems, filter); |
| | | 359 | | |
| | 0 | 360 | | var playedLeafItems = leafItems |
| | 0 | 361 | | .Select(b => new { b.Id, Played = b.UserData!.Any(ud => ud.UserId == userId && ud.Played) }); |
| | | 362 | | |
| | 0 | 363 | | var ancestorLeaves = dbContext.AncestorIds |
| | 0 | 364 | | .WhereOneOrMany(folderIdsArray, a => a.ParentItemId) |
| | 0 | 365 | | .Join( |
| | 0 | 366 | | playedLeafItems, |
| | 0 | 367 | | a => a.ItemId, |
| | 0 | 368 | | b => b.Id, |
| | 0 | 369 | | (a, b) => new { FolderId = a.ParentItemId, b.Id, b.Played }); |
| | | 370 | | |
| | 0 | 371 | | var linkedLeaves = dbContext.LinkedChildren |
| | 0 | 372 | | .WhereOneOrMany(folderIdsArray, lc => lc.ParentId) |
| | 0 | 373 | | .Join( |
| | 0 | 374 | | playedLeafItems, |
| | 0 | 375 | | lc => lc.ChildId, |
| | 0 | 376 | | b => b.Id, |
| | 0 | 377 | | (lc, b) => new { FolderId = lc.ParentId, b.Id, b.Played }); |
| | | 378 | | |
| | 0 | 379 | | var linkedFolderLeaves = dbContext.LinkedChildren |
| | 0 | 380 | | .WhereOneOrMany(folderIdsArray, lc => lc.ParentId) |
| | 0 | 381 | | .Join( |
| | 0 | 382 | | dbContext.BaseItems.Where(b => b.IsFolder), |
| | 0 | 383 | | lc => lc.ChildId, |
| | 0 | 384 | | b => b.Id, |
| | 0 | 385 | | (lc, b) => new { lc.ParentId, FolderChildId = b.Id }) |
| | 0 | 386 | | .Join( |
| | 0 | 387 | | dbContext.AncestorIds, |
| | 0 | 388 | | x => x.FolderChildId, |
| | 0 | 389 | | a => a.ParentItemId, |
| | 0 | 390 | | (x, a) => new { x.ParentId, DescendantId = a.ItemId }) |
| | 0 | 391 | | .Join( |
| | 0 | 392 | | playedLeafItems, |
| | 0 | 393 | | x => x.DescendantId, |
| | 0 | 394 | | b => b.Id, |
| | 0 | 395 | | (x, b) => new { FolderId = x.ParentId, b.Id, b.Played }); |
| | | 396 | | |
| | 0 | 397 | | var results = ancestorLeaves |
| | 0 | 398 | | .Union(linkedLeaves) |
| | 0 | 399 | | .Union(linkedFolderLeaves) |
| | 0 | 400 | | .GroupBy(x => x.FolderId) |
| | 0 | 401 | | .Select(g => new |
| | 0 | 402 | | { |
| | 0 | 403 | | FolderId = g.Key, |
| | 0 | 404 | | Total = g.Select(x => x.Id).Distinct().Count(), |
| | 0 | 405 | | Played = g.Where(x => x.Played).Select(x => x.Id).Distinct().Count() |
| | 0 | 406 | | }) |
| | 0 | 407 | | .ToDictionary(x => x.FolderId, x => (x.Played, x.Total)); |
| | | 408 | | |
| | 0 | 409 | | return results; |
| | 0 | 410 | | } |
| | | 411 | | |
| | | 412 | | private static (int Played, int Total) GetPlayedAndTotalCountFromQuery(IQueryable<BaseItemEntity> query, Guid userId |
| | | 413 | | { |
| | 0 | 414 | | var result = query |
| | 0 | 415 | | .Select(b => b.UserData!.Any(u => u.UserId == userId && u.Played)) |
| | 0 | 416 | | .GroupBy(_ => 1) |
| | 0 | 417 | | .OrderBy(g => g.Key) |
| | 0 | 418 | | .Select(g => new |
| | 0 | 419 | | { |
| | 0 | 420 | | Total = g.Count(), |
| | 0 | 421 | | Played = g.Count(isPlayed => isPlayed) |
| | 0 | 422 | | }) |
| | 0 | 423 | | .FirstOrDefault(); |
| | | 424 | | |
| | 0 | 425 | | return result is null ? (0, 0) : (result.Played, result.Total); |
| | | 426 | | } |
| | | 427 | | } |