From fd5e8a5166ba00b6270f9930fc250d546b719f15 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sat, 21 Jun 2014 02:36:28 +0200 Subject: [PATCH] Added Series SortTitle Migration. (Left a slot for anime) --- src/NzbDrone.Api/Series/SeriesResource.cs | 1 + .../MetadataSourceTests/TraktProxyFixture.cs | 2 + .../Migration/053_add_series_sorttitle.cs | 53 +++++++++++++++++++ .../MetadataSource/TraktProxy.cs | 1 + src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + src/NzbDrone.Core/Tv/RefreshSeriesService.cs | 1 + src/NzbDrone.Core/Tv/Series.cs | 1 + src/NzbDrone.Core/Tv/SeriesService.cs | 1 + 8 files changed, 61 insertions(+) create mode 100644 src/NzbDrone.Core/Datastore/Migration/053_add_series_sorttitle.cs diff --git a/src/NzbDrone.Api/Series/SeriesResource.cs b/src/NzbDrone.Api/Series/SeriesResource.cs index 6fdff13df..95069ad20 100644 --- a/src/NzbDrone.Api/Series/SeriesResource.cs +++ b/src/NzbDrone.Api/Series/SeriesResource.cs @@ -15,6 +15,7 @@ namespace NzbDrone.Api.Series //View Only public String Title { get; set; } + public String SortTitle { get; set; } public List AlternativeTitles { get; set; } public Int32 SeasonCount diff --git a/src/NzbDrone.Core.Test/MetadataSourceTests/TraktProxyFixture.cs b/src/NzbDrone.Core.Test/MetadataSourceTests/TraktProxyFixture.cs index 41734daed..31a26f712 100644 --- a/src/NzbDrone.Core.Test/MetadataSourceTests/TraktProxyFixture.cs +++ b/src/NzbDrone.Core.Test/MetadataSourceTests/TraktProxyFixture.cs @@ -40,6 +40,7 @@ namespace NzbDrone.Core.Test.MetadataSourceTests [TestCase(75978)] [TestCase(83462)] + [TestCase(266189)] public void should_be_able_to_get_series_detail(int tvdbId) { var details = Subject.GetSeriesInfo(tvdbId); @@ -61,6 +62,7 @@ namespace NzbDrone.Core.Test.MetadataSourceTests series.Should().NotBeNull(); series.Title.Should().NotBeBlank(); series.CleanTitle.Should().Be(Parser.Parser.CleanSeriesTitle(series.Title)); + series.SortTitle.Should().Be(Parser.Parser.NormalizeEpisodeTitle(series.Title)); series.Overview.Should().NotBeBlank(); series.AirTime.Should().NotBeBlank(); series.FirstAired.Should().HaveValue(); diff --git a/src/NzbDrone.Core/Datastore/Migration/053_add_series_sorttitle.cs b/src/NzbDrone.Core/Datastore/Migration/053_add_series_sorttitle.cs new file mode 100644 index 000000000..ce004c87f --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/053_add_series_sorttitle.cs @@ -0,0 +1,53 @@ +using System; +using System.IO; +using System.Data; +using System.Linq; +using System.Collections.Generic; +using FluentMigrator; +using Newtonsoft.Json; +using NzbDrone.Common; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(53)] + public class add_series_sorttitle : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Create.Column("SortTitle").OnTable("Series").AsString().Nullable(); + + Execute.WithConnection(SetSortTitles); + } + + private void SetSortTitles(IDbConnection conn, IDbTransaction tran) + { + using (IDbCommand getSeriesCmd = conn.CreateCommand()) + { + getSeriesCmd.Transaction = tran; + getSeriesCmd.CommandText = @"SELECT Id, Title FROM Series"; + using (IDataReader seriesReader = getSeriesCmd.ExecuteReader()) + { + while (seriesReader.Read()) + { + var id = seriesReader.GetInt32(0); + var title = seriesReader.GetString(1); + + var sortTitle = Parser.Parser.NormalizeEpisodeTitle(title).ToLower(); + + using (IDbCommand updateCmd = conn.CreateCommand()) + { + updateCmd.Transaction = tran; + updateCmd.CommandText = "UPDATE Series SET SortTitle = ? WHERE Id = ?"; + updateCmd.AddParameter(sortTitle); + updateCmd.AddParameter(id); + + updateCmd.ExecuteNonQuery(); + } + } + } + } + } + } +} diff --git a/src/NzbDrone.Core/MetadataSource/TraktProxy.cs b/src/NzbDrone.Core/MetadataSource/TraktProxy.cs index 0236a4169..ee7c4b3a8 100644 --- a/src/NzbDrone.Core/MetadataSource/TraktProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/TraktProxy.cs @@ -105,6 +105,7 @@ namespace NzbDrone.Core.MetadataSource series.ImdbId = show.imdb_id; series.Title = show.title; series.CleanTitle = Parser.Parser.CleanSeriesTitle(show.title); + series.SortTitle = Parser.Parser.NormalizeEpisodeTitle(show.title).ToLower(); series.Year = GetYear(show.year, show.first_aired); series.FirstAired = FromIso(show.first_aired_iso); series.Overview = show.overview; diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 97f8093a6..b68285b8d 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -198,6 +198,7 @@ + diff --git a/src/NzbDrone.Core/Tv/RefreshSeriesService.cs b/src/NzbDrone.Core/Tv/RefreshSeriesService.cs index 5390d0502..a00e9e41b 100644 --- a/src/NzbDrone.Core/Tv/RefreshSeriesService.cs +++ b/src/NzbDrone.Core/Tv/RefreshSeriesService.cs @@ -58,6 +58,7 @@ namespace NzbDrone.Core.Tv series.Overview = seriesInfo.Overview; series.Status = seriesInfo.Status; series.CleanTitle = seriesInfo.CleanTitle; + series.SortTitle = seriesInfo.SortTitle; series.LastInfoSync = DateTime.UtcNow; series.Runtime = seriesInfo.Runtime; series.Images = seriesInfo.Images; diff --git a/src/NzbDrone.Core/Tv/Series.cs b/src/NzbDrone.Core/Tv/Series.cs index ace922cb1..2d81a305b 100644 --- a/src/NzbDrone.Core/Tv/Series.cs +++ b/src/NzbDrone.Core/Tv/Series.cs @@ -22,6 +22,7 @@ namespace NzbDrone.Core.Tv public string ImdbId { get; set; } public string Title { get; set; } public string CleanTitle { get; set; } + public string SortTitle { get; set; } public SeriesStatusType Status { get; set; } public string Overview { get; set; } public String AirTime { get; set; } diff --git a/src/NzbDrone.Core/Tv/SeriesService.cs b/src/NzbDrone.Core/Tv/SeriesService.cs index 9de113351..8a361175f 100644 --- a/src/NzbDrone.Core/Tv/SeriesService.cs +++ b/src/NzbDrone.Core/Tv/SeriesService.cs @@ -78,6 +78,7 @@ namespace NzbDrone.Core.Tv newSeries.Monitored = true; newSeries.CleanTitle = Parser.Parser.CleanSeriesTitle(newSeries.Title); + newSeries.SortTitle = Parser.Parser.NormalizeEpisodeTitle(newSeries.Title).ToLower(); _seriesRepository.Insert(newSeries); _eventAggregator.PublishEvent(new SeriesAddedEvent(GetSeries(newSeries.Id)));