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 @@ public class SeriesResource : RestResource //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 @@ public void no_search_result() [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 @@ private void ValidateSeries(Series series) 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 @@ private static Series MapSeries(Show show) 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 @@ private void RefreshSeriesInfo(Series series) 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 @@ public Series() 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 @@ public Series AddSeries(Series newSeries) 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)));