From a04a5e8669c8581a2a0dfa9d51b584cbcce5cf3b Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 20 Apr 2013 16:36:23 -0700 Subject: [PATCH] SeriesStats moved to its own Repo --- NzbDrone.Api/Series/SeriesModule.cs | 7 +++- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 3 +- .../SeriesStatisticsFixture.cs | 9 ++--- NzbDrone.Core/Datastore/TableMapping.cs | 1 + NzbDrone.Core/NzbDrone.Core.csproj | 4 +- .../{Tv => SeriesStats}/SeriesStatistics.cs | 18 ++++++--- .../SeriesStats/SeriesStatisticsRepository.cs | 38 +++++++++++++++++++ .../SeriesStats/SeriesStatisticsService.cs | 24 ++++++++++++ NzbDrone.Core/Tv/SeriesRepository.cs | 22 +---------- NzbDrone.Core/Tv/SeriesService.cs | 7 +--- 10 files changed, 92 insertions(+), 41 deletions(-) rename NzbDrone.Core.Test/{TvTests/SeriesRepositoryTests => SeriesStatsTests}/SeriesStatisticsFixture.cs (83%) rename NzbDrone.Core/{Tv => SeriesStats}/SeriesStatistics.cs (62%) create mode 100644 NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs create mode 100644 NzbDrone.Core/SeriesStats/SeriesStatisticsService.cs diff --git a/NzbDrone.Api/Series/SeriesModule.cs b/NzbDrone.Api/Series/SeriesModule.cs index 9b48aa990..c792940f4 100644 --- a/NzbDrone.Api/Series/SeriesModule.cs +++ b/NzbDrone.Api/Series/SeriesModule.cs @@ -6,6 +6,7 @@ using FluentValidation; using NzbDrone.Api.Extensions; using NzbDrone.Common; +using NzbDrone.Core.SeriesStats; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; using NzbDrone.Api.Validation; @@ -15,11 +16,13 @@ namespace NzbDrone.Api.Series public class SeriesModule : NzbDroneRestModule { private readonly ISeriesService _seriesService; + private readonly ISeriesStatisticsService _seriesStatisticsService; - public SeriesModule(ISeriesService seriesService) + public SeriesModule(ISeriesService seriesService, ISeriesStatisticsService seriesStatisticsService) : base("/Series") { _seriesService = seriesService; + _seriesStatisticsService = seriesStatisticsService; GetResourceAll = AllSeries; GetResourceById = GetSeries; @@ -39,7 +42,7 @@ public SeriesModule(ISeriesService seriesService) private List AllSeries() { var series = _seriesService.GetAllSeries().ToList(); - var seriesStats = _seriesService.SeriesStatistics(); + var seriesStats = _seriesStatisticsService.SeriesStatistics(); var seriesModels = Mapper.Map, List>(series); foreach (var s in seriesModels) diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index d7250b0f5..407f052c2 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -170,7 +170,7 @@ - + @@ -344,6 +344,7 @@ + diff --git a/NzbDrone.Core.Test/TvTests/SeriesRepositoryTests/SeriesStatisticsFixture.cs b/NzbDrone.Core.Test/SeriesStatsTests/SeriesStatisticsFixture.cs similarity index 83% rename from NzbDrone.Core.Test/TvTests/SeriesRepositoryTests/SeriesStatisticsFixture.cs rename to NzbDrone.Core.Test/SeriesStatsTests/SeriesStatisticsFixture.cs index f0418b75c..18c2a4d82 100644 --- a/NzbDrone.Core.Test/TvTests/SeriesRepositoryTests/SeriesStatisticsFixture.cs +++ b/NzbDrone.Core.Test/SeriesStatsTests/SeriesStatisticsFixture.cs @@ -1,17 +1,16 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Core.SeriesStats; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Tv; -namespace NzbDrone.Core.Test.TvTests.SeriesRepositoryTests +namespace NzbDrone.Core.Test.SeriesStatsTests { [TestFixture] - public class SeriesStatisticsFixture : DbTest + public class SeriesStatisticsFixture : DbTest { private Episode _episode; @@ -35,7 +34,7 @@ public void Setup() } [Test] - public void should_get_episodes() + public void should_get_stats_for_series() { var stats = Subject.SeriesStatistics(); diff --git a/NzbDrone.Core/Datastore/TableMapping.cs b/NzbDrone.Core/Datastore/TableMapping.cs index c11027d79..a43f17444 100644 --- a/NzbDrone.Core/Datastore/TableMapping.cs +++ b/NzbDrone.Core/Datastore/TableMapping.cs @@ -15,6 +15,7 @@ using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Qualities; using NzbDrone.Core.RootFolders; +using NzbDrone.Core.SeriesStats; using NzbDrone.Core.Tv; namespace NzbDrone.Core.Datastore diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 33d629786..d6cd4f9f1 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -349,6 +349,7 @@ + @@ -387,7 +388,8 @@ - + + diff --git a/NzbDrone.Core/Tv/SeriesStatistics.cs b/NzbDrone.Core/SeriesStats/SeriesStatistics.cs similarity index 62% rename from NzbDrone.Core/Tv/SeriesStatistics.cs rename to NzbDrone.Core/SeriesStats/SeriesStatistics.cs index a1d113026..a873f500e 100644 --- a/NzbDrone.Core/Tv/SeriesStatistics.cs +++ b/NzbDrone.Core/SeriesStats/SeriesStatistics.cs @@ -1,18 +1,26 @@ using System; using NzbDrone.Core.Datastore; -namespace NzbDrone.Core.Tv +namespace NzbDrone.Core.SeriesStats { public class SeriesStatistics : ResultSet { public int SeriesId { get; set; } public int NumberOfSeasons { get; set; } public string NextAiringString { get; set; } - public DateTime? NextAiring - { - get { return Convert.ToDateTime(NextAiringString); } - } public int EpisodeFileCount { get; set; } public int EpisodeCount { get; set; } + + public DateTime? NextAiring + { + get + { + DateTime nextAiring; + + if (!DateTime.TryParse(NextAiringString, out nextAiring)) return null; + + return nextAiring; + } + } } } diff --git a/NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs b/NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs new file mode 100644 index 000000000..2c2490f21 --- /dev/null +++ b/NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using Marr.Data; +using NzbDrone.Core.Datastore; + +namespace NzbDrone.Core.SeriesStats +{ + public interface ISeriesStatisticsRepository + { + List SeriesStatistics(); + } + + public class SeriesStatisticsRepository : ISeriesStatisticsRepository + { + private readonly IDataMapper _dataMapper; + + public SeriesStatisticsRepository(IDatabase database) + { + _dataMapper = database.DataMapper; + } + + public List SeriesStatistics() + { + _dataMapper.AddParameter("currentDate", DateTime.UtcNow); + + var queryText = @"SELECT + SeriesId, + SUM(CASE WHEN Ignored = 0 AND Airdate <= @currentDate THEN 1 ELSE 0 END) AS EpisodeCount, + SUM(CASE WHEN Ignored = 0 AND Episodes.EpisodeFileId > 0 AND AirDate <= @currentDate THEN 1 ELSE 0 END) as EpisodeFileCount, + MAX(Episodes.SeasonNumber) as NumberOfSeasons, + MIN(CASE WHEN AirDate < @currentDate THEN NULL ELSE AirDate END) as NextAiringString + FROM Episodes + GROUP BY SeriesId"; + + return _dataMapper.Query(queryText); + } + } +} diff --git a/NzbDrone.Core/SeriesStats/SeriesStatisticsService.cs b/NzbDrone.Core/SeriesStats/SeriesStatisticsService.cs new file mode 100644 index 000000000..a0c177a12 --- /dev/null +++ b/NzbDrone.Core/SeriesStats/SeriesStatisticsService.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace NzbDrone.Core.SeriesStats +{ + public interface ISeriesStatisticsService + { + List SeriesStatistics(); + } + + public class SeriesStatisticsService : ISeriesStatisticsService + { + private readonly SeriesStatisticsRepository _seriesStatisticsRepository; + + public SeriesStatisticsService(SeriesStatisticsRepository seriesStatisticsRepository) + { + _seriesStatisticsRepository = seriesStatisticsRepository; + } + + public List SeriesStatistics() + { + return _seriesStatisticsRepository.SeriesStatistics(); + } + } +} diff --git a/NzbDrone.Core/Tv/SeriesRepository.cs b/NzbDrone.Core/Tv/SeriesRepository.cs index 624ce030c..1ca5c2d56 100644 --- a/NzbDrone.Core/Tv/SeriesRepository.cs +++ b/NzbDrone.Core/Tv/SeriesRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using NzbDrone.Core.Datastore; +using NzbDrone.Core.SeriesStats; namespace NzbDrone.Core.Tv { @@ -13,17 +14,13 @@ public interface ISeriesRepository : IBasicRepository Series FindByTvdbId(int tvdbId); void SetSeriesType(int seriesId, SeriesTypes seriesTypes); void SetTvRageId(int seriesId, int tvRageId); - List SeriesStatistics(); } public class SeriesRepository : BasicRepository, ISeriesRepository { - private readonly IDatabase _database; - public SeriesRepository(IDatabase database) : base(database) { - _database = database; } public bool SeriesPathExists(string path) @@ -55,22 +52,5 @@ public void SetTvRageId(int seriesId, int tvRageId) { SetFields(new Series { Id = seriesId, TvRageId = tvRageId }, s => s.TvRageId); } - - public List SeriesStatistics() - { - _database.DataMapper.AddParameter("currentDate", DateTime.UtcNow); - - var queryText = @"SELECT - SeriesId, - SUM(CASE WHEN Airdate <= @currentDate THEN 1 ELSE 0 END) AS EpisodeCount, - SUM(CASE WHEN EpisodeFileId > 0 AND AirDate <= @currentDate THEN 1 ELSE 0 END) as EpisodeFileCount, - MAX(SeasonNumber) as NumberOfSeasons, - MIN(CASE WHEN AirDate < @currentDate THEN NULL ELSE AirDate END) as NextAiringString - FROM Episodes - WHERE Ignored = 0 - GROUP BY SeriesId"; - - return _database.DataMapper.Query(queryText); - } } } \ No newline at end of file diff --git a/NzbDrone.Core/Tv/SeriesService.cs b/NzbDrone.Core/Tv/SeriesService.cs index a5cd611d8..cfb65460d 100644 --- a/NzbDrone.Core/Tv/SeriesService.cs +++ b/NzbDrone.Core/Tv/SeriesService.cs @@ -14,6 +14,7 @@ using NzbDrone.Core.Organizer; using NzbDrone.Core.Parser; using NzbDrone.Core.RootFolders; +using NzbDrone.Core.SeriesStats; using NzbDrone.Core.Tv.Events; namespace NzbDrone.Core.Tv @@ -33,7 +34,6 @@ public interface ISeriesService void UpdateSeries(Series series); bool SeriesPathExists(string folder); List GetSeriesInList(IEnumerable seriesIds); - List SeriesStatistics(); } public class SeriesService : ISeriesService, IHandleAsync @@ -179,11 +179,6 @@ public List GetSeriesInList(IEnumerable seriesIds) return _seriesRepository.Get(seriesIds).ToList(); } - public List SeriesStatistics() - { - return _seriesRepository.SeriesStatistics(); - } - public void HandleAsync(SeriesAddedEvent message) { UpdateSeriesInfo(message.Series.Id);