diff --git a/NzbDrone.Core.Test/FluentTest.cs b/NzbDrone.Core.Test/FluentTest.cs index 8a703d3fa..797bc5a4a 100644 --- a/NzbDrone.Core.Test/FluentTest.cs +++ b/NzbDrone.Core.Test/FluentTest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using FluentAssertions; using NUnit.Framework; @@ -164,5 +165,31 @@ namespace NzbDrone.Core.Test //Resolve result.Should().Be("http://www.nzbdrone.com"); } + + [Test] + public void MaxOrDefault_should_return_zero_when_collection_is_empty() + { + //Setup + + + //Act + var result = (new List()).MaxOrDefault(); + + //Resolve + result.Should().Be(0); + } + + [Test] + public void MaxOrDefault_should_return_max_when_collection_is_not_empty() + { + //Setup + var list = new List {6, 4, 5, 3, 8, 10}; + + //Act + var result = list.MaxOrDefault(); + + //Resolve + result.Should().Be(10); + } } } diff --git a/NzbDrone.Core/Fluent.cs b/NzbDrone.Core/Fluent.cs index fe611d249..f99c0d038 100644 --- a/NzbDrone.Core/Fluent.cs +++ b/NzbDrone.Core/Fluent.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -51,5 +52,15 @@ namespace NzbDrone.Core { return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - String.Join("", uri.Segments).Length - uri.Query.Length); } + + public static int MaxOrDefault(this IEnumerable ints) + { + var intList = ints.ToList(); + + if (intList.Count() == 0) + return 0; + + return intList.Max(); + } } } diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index ae522868f..597ed7746 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -138,9 +138,25 @@ namespace NzbDrone.Core.Providers SeriesId = parseResult.Series.SeriesId, AirDate = parseResult.AirDate.Value, Title = "TBD", - Overview = String.Empty, + Overview = String.Empty }; + var episodesInSeries = GetEpisodeBySeries(parseResult.Series.SeriesId); + + //Find the current season number + var maxSeasonNumber = episodesInSeries.Select(s => s.SeasonNumber).MaxOrDefault(); + + //Set the season number + episodeInfo.SeasonNumber = (maxSeasonNumber == 0) ? 1 : maxSeasonNumber; + + //Find the latest episode number + var maxEpisodeNumber = episodesInSeries + .Where(w => w.SeasonNumber == episodeInfo.SeasonNumber) + .Select(s => s.EpisodeNumber).MaxOrDefault(); + + //Set the episode number to max + 1 + episodeInfo.EpisodeNumber = maxEpisodeNumber + 1; + AddEpisode(episodeInfo); }