From 2ff1208947843c243988e2633f58fe5ab0b6b7d6 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 10 Sep 2014 18:11:35 -0700 Subject: [PATCH] Episode resource now uses series resource as the subtype Fixed: Episodes on the calendar have series images attached properly again --- .../Episodes/EpisodeModuleWithSignalR.cs | 12 +++++++++++- src/NzbDrone.Api/Episodes/EpisodeResource.cs | 5 ++--- src/NzbDrone.Api/Extensions/LazyExtensions.cs | 12 +++++++----- src/NzbDrone.Api/Series/SeriesModule.cs | 1 - src/NzbDrone.Api/Series/SeriesResource.cs | 1 - 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/NzbDrone.Api/Episodes/EpisodeModuleWithSignalR.cs b/src/NzbDrone.Api/Episodes/EpisodeModuleWithSignalR.cs index 430b34d01..41ee3d04e 100644 --- a/src/NzbDrone.Api/Episodes/EpisodeModuleWithSignalR.cs +++ b/src/NzbDrone.Api/Episodes/EpisodeModuleWithSignalR.cs @@ -1,7 +1,10 @@ using System; +using System.Collections.Generic; using System.IO; -using NzbDrone.Api.EpisodeFiles; +using System.Linq; +using NzbDrone.Api.Extensions; using NzbDrone.Api.Mapping; +using NzbDrone.Api.Series; using NzbDrone.Core.Datastore.Events; using NzbDrone.Core.DecisionEngine; using NzbDrone.Core.Download; @@ -72,6 +75,13 @@ namespace NzbDrone.Api.Episodes return resource; } + protected override List ToListResource(IEnumerable modelList) + { + var resources = base.ToListResource(modelList); + + return resources.LoadSubtype(e => e.SeriesId, _seriesService.GetSeries).ToList(); + } + public void Handle(EpisodeGrabbedEvent message) { foreach (var episode in message.Episode.Episodes) diff --git a/src/NzbDrone.Api/Episodes/EpisodeResource.cs b/src/NzbDrone.Api/Episodes/EpisodeResource.cs index 3e97e50b0..6338afaab 100644 --- a/src/NzbDrone.Api/Episodes/EpisodeResource.cs +++ b/src/NzbDrone.Api/Episodes/EpisodeResource.cs @@ -2,6 +2,7 @@ using Newtonsoft.Json; using NzbDrone.Api.EpisodeFiles; using NzbDrone.Api.REST; +using NzbDrone.Api.Series; namespace NzbDrone.Api.Episodes { @@ -27,9 +28,7 @@ namespace NzbDrone.Api.Episodes public DateTime? EndTime { get; set; } public DateTime? GrabDate { get; set; } public String SeriesTitle { get; set; } - - //I'd like to replace this with SeriesResource, but LoadSubType would need to be reworked to support that - public Core.Tv.Series Series { get; set; } + public SeriesResource Series { get; set; } //Hiding this so people don't think its usable (only used to set the initial state) [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] diff --git a/src/NzbDrone.Api/Extensions/LazyExtensions.cs b/src/NzbDrone.Api/Extensions/LazyExtensions.cs index 32852a26c..89228252b 100644 --- a/src/NzbDrone.Api/Extensions/LazyExtensions.cs +++ b/src/NzbDrone.Api/Extensions/LazyExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using NzbDrone.Api.Mapping; using NzbDrone.Api.REST; using NzbDrone.Common.Cache; using NzbDrone.Core.Datastore; @@ -12,8 +13,9 @@ namespace NzbDrone.Api.Extensions { private static readonly ICached SetterCache = new Cached(); - public static IEnumerable LoadSubtype(this IEnumerable parents, Func foreignKeySelector, IBasicRepository childRepository) - where TChild : ModelBase, new() + public static IEnumerable LoadSubtype(this IEnumerable parents, Func foreignKeySelector, Func, IEnumerable> sourceChildSelector) + where TSourceChild : ModelBase, new() + where TChild : RestResource, new() where TParent : RestResource { var parentList = parents.Where(p => foreignKeySelector(p) != 0).ToList(); @@ -24,13 +26,13 @@ namespace NzbDrone.Api.Extensions } var ids = parentList.Select(foreignKeySelector).Distinct(); - var childDictionary = childRepository.Get(ids).ToDictionary(child => child.Id, child => child); + var childDictionary = sourceChildSelector(ids).ToDictionary(child => child.Id, child => child); var childSetter = GetChildSetter(); foreach (var episode in parentList) { - childSetter.Invoke(episode, new object[] { childDictionary[foreignKeySelector(episode)] }); + childSetter.Invoke(episode, new object[] { childDictionary[foreignKeySelector(episode)].InjectTo() }); } return parents; @@ -38,7 +40,7 @@ namespace NzbDrone.Api.Extensions private static MethodInfo GetChildSetter() - where TChild : ModelBase + where TChild : RestResource where TParent : RestResource { var key = typeof(TChild).FullName + typeof(TParent).FullName; diff --git a/src/NzbDrone.Api/Series/SeriesModule.cs b/src/NzbDrone.Api/Series/SeriesModule.cs index 0427c5803..01711efd9 100644 --- a/src/NzbDrone.Api/Series/SeriesModule.cs +++ b/src/NzbDrone.Api/Series/SeriesModule.cs @@ -7,7 +7,6 @@ using NzbDrone.Core.Datastore.Events; using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.Events; -using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.SeriesStats; using NzbDrone.Core.Tv; diff --git a/src/NzbDrone.Api/Series/SeriesResource.cs b/src/NzbDrone.Api/Series/SeriesResource.cs index 596714983..cc348e919 100644 --- a/src/NzbDrone.Api/Series/SeriesResource.cs +++ b/src/NzbDrone.Api/Series/SeriesResource.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Newtonsoft.Json; using NzbDrone.Api.REST; using NzbDrone.Core.MediaCover; using NzbDrone.Core.Tv;