From 71514af96be684dcbf432c5aca37712bbd773740 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 20 Nov 2013 16:08:12 -0500 Subject: [PATCH] render channels page --- MediaBrowser.Api/UserLibrary/ItemsService.cs | 55 +++++++++++++------ .../Sorting/AiredEpisodeOrderComparer.cs | 15 +++-- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/MediaBrowser.Api/UserLibrary/ItemsService.cs b/MediaBrowser.Api/UserLibrary/ItemsService.cs index 4239869a5..3936014c5 100644 --- a/MediaBrowser.Api/UserLibrary/ItemsService.cs +++ b/MediaBrowser.Api/UserLibrary/ItemsService.cs @@ -206,7 +206,7 @@ namespace MediaBrowser.Api.UserLibrary [ApiMember(Name = "AiredDuringSeason", Description = "Gets all episodes that aired during a season, including specials.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")] public int? AiredDuringSeason { get; set; } - + [ApiMember(Name = "MinPremiereDate", Description = "Optional. The minimum premiere date. Format = yyyyMMddHHmmss", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")] public string MinPremiereDate { get; set; } @@ -1012,21 +1012,7 @@ namespace MediaBrowser.Api.UserLibrary if (request.AiredDuringSeason.HasValue) { - var val = request.AiredDuringSeason.Value; - - items = items.Where(i => - { - var episode = i as Episode; - - if (episode != null) - { - var seasonNumber = episode.AirsAfterSeasonNumber ?? episode.AirsBeforeSeasonNumber ?? episode.ParentIndexNumber; - - return episode.PremiereDate.HasValue && seasonNumber.HasValue && seasonNumber.Value == val; - } - - return false; - }); + items = FilterByAiredDuringSeason(items, request.AiredDuringSeason.Value); } if (!string.IsNullOrEmpty(request.MinPremiereDate)) @@ -1046,6 +1032,43 @@ namespace MediaBrowser.Api.UserLibrary return items; } + private IEnumerable FilterByAiredDuringSeason(IEnumerable items, int seasonNumber) + { + var episodes = items.OfType().ToList(); + + // We can only enforce the air date requirement if the episodes have air dates + var enforceAirDate = episodes.Any(i => i.PremiereDate.HasValue); + + return episodes.Where(i => + { + var episode = i; + + if (episode != null) + { + var currentSeasonNumber = episode.AirsAfterSeasonNumber ?? episode.AirsBeforeSeasonNumber ?? episode.ParentIndexNumber; + + // If this produced nothing, try and get it from the parent folder + if (!currentSeasonNumber.HasValue) + { + var season = episode.Parent as Season; + if (season != null) + { + currentSeasonNumber = season.IndexNumber; + } + } + + if (enforceAirDate && !episode.PremiereDate.HasValue) + { + return false; + } + + return currentSeasonNumber.HasValue && currentSeasonNumber.Value == seasonNumber; + } + + return false; + }); + } + /// /// Determines whether the specified item has image. /// diff --git a/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs b/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs index cec9743ba..23334aa41 100644 --- a/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs @@ -1,8 +1,8 @@ -using System; -using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Sorting; using MediaBrowser.Model.Querying; +using System; namespace MediaBrowser.Server.Implementations.Sorting { @@ -16,11 +16,14 @@ namespace MediaBrowser.Server.Implementations.Sorting /// System.Int32. public int Compare(BaseItem x, BaseItem y) { - var val = DateTime.Compare(x.PremiereDate ?? DateTime.MinValue, y.PremiereDate ?? DateTime.MinValue); - - if (val != 0) + if (x.PremiereDate.HasValue && y.PremiereDate.HasValue) { - return val; + var val = DateTime.Compare(x.PremiereDate.Value, y.PremiereDate.Value); + + if (val != 0) + { + return val; + } } var episode1 = x as Episode;