1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Using LoadSubtype for Missing and Calendar

This commit is contained in:
Mark McDowall 2013-06-03 23:18:12 -07:00
parent 18441aca03
commit 8a1c6aa4d8
4 changed files with 14 additions and 58 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NzbDrone.Api.Episodes; using NzbDrone.Api.Episodes;
using NzbDrone.Api.Extensions;
using NzbDrone.Api.Mapping; using NzbDrone.Api.Mapping;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -11,13 +12,13 @@ namespace NzbDrone.Api.Calendar
public class CalendarModule : NzbDroneRestModule<EpisodeResource> public class CalendarModule : NzbDroneRestModule<EpisodeResource>
{ {
private readonly IEpisodeService _episodeService; private readonly IEpisodeService _episodeService;
private readonly SeriesService _seriesService; private readonly SeriesRepository _seriesRepository;
public CalendarModule(IEpisodeService episodeService, SeriesService seriesService) public CalendarModule(IEpisodeService episodeService, SeriesRepository seriesRepository)
: base("/calendar") : base("/calendar")
{ {
_episodeService = episodeService; _episodeService = episodeService;
_seriesService = seriesService; _seriesRepository = seriesRepository;
GetResourceAll = GetPaged; GetResourceAll = GetPaged;
} }
@ -33,13 +34,10 @@ private List<EpisodeResource> GetPaged()
if (queryStart.HasValue) start = DateTime.Parse(queryStart.Value); if (queryStart.HasValue) start = DateTime.Parse(queryStart.Value);
if (queryEnd.HasValue) end = DateTime.Parse(queryEnd.Value); if (queryEnd.HasValue) end = DateTime.Parse(queryEnd.Value);
var episodes = _episodeService.EpisodesBetweenDates(start, end); var resources = ToListResource(() => _episodeService.EpisodesBetweenDates(start, end))
var episodeResources = ToListResource(() => episodes); .LoadSubtype(e => e.SeriesId, _seriesRepository);
var series = _seriesService.GetSeriesInList(episodeResources.SelectDistinct(e => e.SeriesId)); return resources.ToList();
episodeResources.Join(series, episode => episode.SeriesId, s => s.Id, episode => episode.Series);
return episodeResources;
} }
} }
} }

View File

@ -1,5 +1,6 @@
using System.Linq; using System.Linq;
using NzbDrone.Api.Episodes; using NzbDrone.Api.Episodes;
using NzbDrone.Api.Extensions;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -9,13 +10,13 @@ namespace NzbDrone.Api.Missing
public class MissingModule : NzbDroneRestModule<EpisodeResource> public class MissingModule : NzbDroneRestModule<EpisodeResource>
{ {
private readonly IEpisodeService _episodeService; private readonly IEpisodeService _episodeService;
private readonly SeriesService _seriesService; private readonly SeriesRepository _seriesRepository;
public MissingModule(IEpisodeService episodeService, SeriesService seriesService) public MissingModule(IEpisodeService episodeService, SeriesRepository seriesRepository)
:base("missing") :base("missing")
{ {
_episodeService = episodeService; _episodeService = episodeService;
_seriesService = seriesService; _seriesRepository = seriesRepository;
GetResourcePaged = GetMissingEpisodes; GetResourcePaged = GetMissingEpisodes;
} }
@ -29,12 +30,10 @@ private PagingResource<EpisodeResource> GetMissingEpisodes(PagingResource<Episod
SortDirection = pagingResource.SortDirection SortDirection = pagingResource.SortDirection
}; };
var episodeResources = ApplyToPage(_episodeService.EpisodesWithoutFiles, pagingSpec); var resource = ApplyToPage(_episodeService.EpisodesWithoutFiles, pagingSpec);
resource.Records = resource.Records.LoadSubtype(e => e.SeriesId, _seriesRepository).ToList();
var series = _seriesService.GetSeriesInList(episodeResources.Records.SelectDistinct(e => e.SeriesId)); return resource;
episodeResources.Records.Join(series, episode => episode.SeriesId, s => s.Id, episode => episode.Series);
return episodeResources;
} }
} }
} }

View File

@ -1,40 +0,0 @@
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<TKey> SelectDistinct<TOuter, TKey>(this IEnumerable<TOuter> outer, Func<TOuter, TKey> outerKeySelector)
{
return outer.Select(outerKeySelector).Distinct();
}
public static IEnumerable<TOuter> Join<TOuter, TInner, TKey, TProperty>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Expression<Func<TOuter, TProperty>> 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;
}
}
}

View File

@ -84,7 +84,6 @@
<Compile Include="Composition\Container.cs" /> <Compile Include="Composition\Container.cs" />
<Compile Include="Composition\IContainer.cs" /> <Compile Include="Composition\IContainer.cs" />
<Compile Include="Composition\ContainerBuilderBase.cs" /> <Compile Include="Composition\ContainerBuilderBase.cs" />
<Compile Include="EnumerableExtensions.cs" />
<Compile Include="EnsureThat\Ensure.cs" /> <Compile Include="EnsureThat\Ensure.cs" />
<Compile Include="EnsureThat\EnsureBoolExtensions.cs" /> <Compile Include="EnsureThat\EnsureBoolExtensions.cs" />
<Compile Include="EnsureThat\EnsureCollectionExtensions.cs" /> <Compile Include="EnsureThat\EnsureCollectionExtensions.cs" />