| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Linq.Expressions; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using Jellyfin.Database.Implementations; |
| | | 8 | | using Jellyfin.Database.Implementations.Entities; |
| | | 9 | | using MediaBrowser.Controller.Entities; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Static class for methods which maps types of ordering to their respecting ordering functions. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class OrderMapper |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates Func to be executed later with a given BaseItemEntity input for sorting items on query. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="sortBy">Item property to sort by.</param> |
| | | 23 | | /// <param name="query">Context Query.</param> |
| | | 24 | | /// <param name="jellyfinDbContext">Context.</param> |
| | | 25 | | /// <returns>Func to be executed later for sorting query.</returns> |
| | | 26 | | public static Expression<Func<BaseItemEntity, object?>> MapOrderByField(ItemSortBy sortBy, InternalItemsQuery query, |
| | | 27 | | { |
| | 137 | 28 | | return (sortBy, query.User) switch |
| | 137 | 29 | | { |
| | 0 | 30 | | (ItemSortBy.AirTime, _) => e => e.SortName, // TODO |
| | 0 | 31 | | (ItemSortBy.Runtime, _) => e => e.RunTimeTicks, |
| | 47 | 32 | | (ItemSortBy.Random, _) => e => EF.Functions.Random(), |
| | 1 | 33 | | (ItemSortBy.DatePlayed, _) => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id))!.LastPla |
| | 0 | 34 | | (ItemSortBy.PlayCount, _) => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id))!.PlayCoun |
| | 0 | 35 | | (ItemSortBy.IsFavoriteOrLiked, _) => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id))!. |
| | 44 | 36 | | (ItemSortBy.IsFolder, _) => e => e.IsFolder, |
| | 0 | 37 | | (ItemSortBy.IsPlayed, _) => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id))!.Played, |
| | 0 | 38 | | (ItemSortBy.IsUnplayed, _) => e => !e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id))!.Played |
| | 0 | 39 | | (ItemSortBy.DateLastContentAdded, _) => e => e.DateLastMediaAdded, |
| | 0 | 40 | | (ItemSortBy.Artist, _) => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.Artist).Select(f = |
| | 0 | 41 | | (ItemSortBy.AlbumArtist, _) => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.AlbumArtist). |
| | 0 | 42 | | (ItemSortBy.Studio, _) => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.Studios).Select(f |
| | 0 | 43 | | (ItemSortBy.OfficialRating, _) => e => e.InheritedParentalRatingValue, |
| | 0 | 44 | | (ItemSortBy.SeriesSortName, _) => e => e.SeriesName, |
| | 0 | 45 | | (ItemSortBy.Album, _) => e => e.Album, |
| | 0 | 46 | | (ItemSortBy.DateCreated, _) => e => e.DateCreated, |
| | 1 | 47 | | (ItemSortBy.PremiereDate, _) => e => (e.PremiereDate ?? (e.ProductionYear.HasValue ? DateTime.MinValue.AddYe |
| | 0 | 48 | | (ItemSortBy.StartDate, _) => e => e.StartDate, |
| | 0 | 49 | | (ItemSortBy.Name, _) => e => e.CleanName, |
| | 0 | 50 | | (ItemSortBy.CommunityRating, _) => e => e.CommunityRating, |
| | 0 | 51 | | (ItemSortBy.ProductionYear, _) => e => e.ProductionYear, |
| | 0 | 52 | | (ItemSortBy.CriticRating, _) => e => e.CriticRating, |
| | 0 | 53 | | (ItemSortBy.VideoBitRate, _) => e => e.TotalBitrate, |
| | 0 | 54 | | (ItemSortBy.ParentIndexNumber, _) => e => e.ParentIndexNumber, |
| | 0 | 55 | | (ItemSortBy.IndexNumber, _) => e => e.IndexNumber, |
| | 0 | 56 | | (ItemSortBy.SeriesDatePlayed, not null) => e => |
| | 0 | 57 | | jellyfinDbContext.BaseItems |
| | 0 | 58 | | .Where(w => w.SeriesPresentationUniqueKey == e.PresentationUniqueKey) |
| | 0 | 59 | | .Join(jellyfinDbContext.UserData.Where(w => w.UserId == query.User.Id && w.Played), f => |
| | 0 | 60 | | .Max(f => f), |
| | 0 | 61 | | (ItemSortBy.SeriesDatePlayed, null) => e => jellyfinDbContext.BaseItems.Where(w => w.SeriesPresentationUniqu |
| | 0 | 62 | | .Join(jellyfinDbContext.UserData.Where(w => w.Played), f => f.Id, f => f.ItemId, (item, |
| | 0 | 63 | | .Max(f => f), |
| | 137 | 64 | | // ItemSortBy.SeriesDatePlayed => e => jellyfinDbContext.UserData |
| | 137 | 65 | | // .Where(u => u.Item!.SeriesPresentationUniqueKey == e.PresentationUniqueKey && u.Played) |
| | 137 | 66 | | // .Max(f => f.LastPlayedDate), |
| | 137 | 67 | | // ItemSortBy.AiredEpisodeOrder => "AiredEpisodeOrder", |
| | 44 | 68 | | _ => e => e.SortName |
| | 137 | 69 | | }; |
| | | 70 | | } |
| | | 71 | | } |