From 047e53179df1b587d10c1a2a66f6172502da09e9 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 3 Jun 2013 20:59:24 -0700 Subject: [PATCH] Calendar and Missing join series in API --- NzbDrone.Api/Calendar/CalendarModule.cs | 13 ++++++-- NzbDrone.Api/Missing/MissingModule.cs | 15 ++++++++-- NzbDrone.Common/EnumerableExtensions.cs | 40 +++++++++++++++++++++++++ NzbDrone.Common/NzbDrone.Common.csproj | 1 + UI/Missing/MissingLayout.js | 1 + 5 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 NzbDrone.Common/EnumerableExtensions.cs diff --git a/NzbDrone.Api/Calendar/CalendarModule.cs b/NzbDrone.Api/Calendar/CalendarModule.cs index e4ed0f560..a03026cde 100644 --- a/NzbDrone.Api/Calendar/CalendarModule.cs +++ b/NzbDrone.Api/Calendar/CalendarModule.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Linq; using NzbDrone.Api.Episodes; using NzbDrone.Api.Mapping; +using NzbDrone.Common; using NzbDrone.Core.Tv; namespace NzbDrone.Api.Calendar @@ -9,11 +11,13 @@ namespace NzbDrone.Api.Calendar public class CalendarModule : NzbDroneRestModule { private readonly IEpisodeService _episodeService; + private readonly SeriesService _seriesService; - public CalendarModule(IEpisodeService episodeService) + public CalendarModule(IEpisodeService episodeService, SeriesService seriesService) : base("/calendar") { _episodeService = episodeService; + _seriesService = seriesService; GetResourceAll = GetPaged; } @@ -27,12 +31,15 @@ namespace NzbDrone.Api.Calendar var queryEnd = Request.Query.End; if (queryStart.HasValue) start = DateTime.Parse(queryStart.Value); - if (queryEnd.HasValue) end = DateTime.Parse(queryEnd.Value); var episodes = _episodeService.EpisodesBetweenDates(start, end); + var episodeResources = ToListResource(() => episodes); + + var series = _seriesService.GetSeriesInList(episodeResources.SelectDistinct(e => e.SeriesId)); + episodeResources.Join(series, episode => episode.SeriesId, s => s.Id, episode => episode.Series); - return ToListResource(() => episodes); + return episodeResources; } } } diff --git a/NzbDrone.Api/Missing/MissingModule.cs b/NzbDrone.Api/Missing/MissingModule.cs index 8527284c5..57402e547 100644 --- a/NzbDrone.Api/Missing/MissingModule.cs +++ b/NzbDrone.Api/Missing/MissingModule.cs @@ -1,4 +1,6 @@ -using NzbDrone.Api.Episodes; +using System.Linq; +using NzbDrone.Api.Episodes; +using NzbDrone.Common; using NzbDrone.Core.Datastore; using NzbDrone.Core.Tv; @@ -7,11 +9,13 @@ namespace NzbDrone.Api.Missing public class MissingModule : NzbDroneRestModule { private readonly IEpisodeService _episodeService; + private readonly SeriesService _seriesService; - public MissingModule(IEpisodeService episodeService) + public MissingModule(IEpisodeService episodeService, SeriesService seriesService) :base("missing") { _episodeService = episodeService; + _seriesService = seriesService; GetResourcePaged = GetMissingEpisodes; } @@ -25,7 +29,12 @@ namespace NzbDrone.Api.Missing SortDirection = pagingResource.SortDirection }; - return ApplyToPage(_episodeService.EpisodesWithoutFiles, pagingSpec); + var episodeResources = ApplyToPage(_episodeService.EpisodesWithoutFiles, pagingSpec); + + var series = _seriesService.GetSeriesInList(episodeResources.Records.SelectDistinct(e => e.SeriesId)); + episodeResources.Records.Join(series, episode => episode.SeriesId, s => s.Id, episode => episode.Series); + + return episodeResources; } } } \ No newline at end of file diff --git a/NzbDrone.Common/EnumerableExtensions.cs b/NzbDrone.Common/EnumerableExtensions.cs new file mode 100644 index 000000000..2f084fffc --- /dev/null +++ b/NzbDrone.Common/EnumerableExtensions.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; + +namespace NzbDrone.Common +{ + public static class EnumerableExtensions + { + public static IEnumerable SelectDistinct(this IEnumerable outer, Func outerKeySelector) + { + return outer.Select(outerKeySelector).Distinct(); + } + + public static IEnumerable Join(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Expression> outerProperty) + { + var outerList = outer.ToList(); + var innerList = inner.ToList(); + + foreach (var outerElement in outerList) + { + var outerKey = outerKeySelector(outerElement); + + foreach (var innerElement in innerList) + { + var innerKey = innerKeySelector(innerElement); + + if (innerKey.Equals(outerKey)) + { + var prop = (PropertyInfo)((MemberExpression)outerProperty.Body).Member; + prop.SetValue(outerElement, innerElement, null); + } + } + } + + return outerList; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 7410fc382..e7c49248c 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -84,6 +84,7 @@ + diff --git a/UI/Missing/MissingLayout.js b/UI/Missing/MissingLayout.js index 1f58b22f4..a955a0e06 100644 --- a/UI/Missing/MissingLayout.js +++ b/UI/Missing/MissingLayout.js @@ -20,6 +20,7 @@ define([ { name : 'series.Title', label : 'Series Title', + sortable : false, cell : Backgrid.TemplateBackedCell.extend({ template: 'Missing/SeriesTitleTemplate' }) }, {