2012-02-21 04:25:19 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using PetaPoco;
|
|
|
|
|
|
2013-02-19 07:01:03 +01:00
|
|
|
|
namespace NzbDrone.Core.Tv
|
2012-02-21 04:25:19 +01:00
|
|
|
|
{
|
|
|
|
|
public class SeasonProvider
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2012-02-28 06:50:56 +01:00
|
|
|
|
|
2012-02-21 04:25:19 +01:00
|
|
|
|
private readonly IDatabase _database;
|
|
|
|
|
|
|
|
|
|
public SeasonProvider(IDatabase database)
|
|
|
|
|
{
|
|
|
|
|
_database = database;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SeasonProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual IList<int> GetSeasons(int seriesId)
|
|
|
|
|
{
|
2012-02-28 06:50:56 +01:00
|
|
|
|
return _database.Fetch<Int32>("SELECT DISTINCT SeasonNumber FROM Seasons WHERE SeriesId = @seriesId", new { seriesId }).OrderBy(c => c).ToList();
|
2012-02-21 04:25:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void SetIgnore(int seriesId, int seasonNumber, bool isIgnored)
|
|
|
|
|
{
|
2012-02-28 06:50:56 +01:00
|
|
|
|
logger.Trace("Setting ignore flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, isIgnored);
|
2012-02-21 04:25:19 +01:00
|
|
|
|
_database.Execute(@"UPDATE Seasons SET Ignored = @isIgnored
|
|
|
|
|
WHERE SeriesId = @seriesId AND SeasonNumber = @seasonNumber AND Ignored = @InversedIgnored",
|
|
|
|
|
new { isIgnored, seriesId, seasonNumber, InversedIgnored = !isIgnored });
|
|
|
|
|
|
2012-02-28 06:50:56 +01:00
|
|
|
|
|
|
|
|
|
logger.Trace("Setting ignore flag on all episodes of Series:{0} Season:{1} to {2}", seriesId, seasonNumber, isIgnored);
|
|
|
|
|
_database.Execute(@"UPDATE Episodes SET Ignored = @0
|
|
|
|
|
WHERE SeriesId = @1 AND SeasonNumber = @2 AND Ignored = @3",
|
|
|
|
|
isIgnored, seriesId, seasonNumber, !isIgnored);
|
|
|
|
|
|
2012-02-21 04:25:19 +01:00
|
|
|
|
logger.Info("Ignore flag for Series:{0} Season:{1} successfully set to {2}", seriesId, seasonNumber, isIgnored);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool IsIgnored(int seriesId, int seasonNumber)
|
|
|
|
|
{
|
2012-02-28 06:50:56 +01:00
|
|
|
|
return _database.Single<Season>("WHERE SeriesId = @seriesId AND SeasonNumber = @seasonNumber",
|
|
|
|
|
new { seriesId, seasonNumber }).Ignored;
|
2012-02-21 04:25:19 +01:00
|
|
|
|
}
|
2012-02-21 07:50:38 +01:00
|
|
|
|
|
|
|
|
|
public virtual List<Season> All(int seriesId)
|
|
|
|
|
{
|
2012-02-25 01:53:01 +01:00
|
|
|
|
var seasons = _database.Fetch<Season, Episode, EpisodeFile, Season>(
|
2012-02-28 06:50:56 +01:00
|
|
|
|
new EpisodeSeasonRelator().MapIt,
|
2012-02-21 07:50:38 +01:00
|
|
|
|
@"SELECT * FROM Seasons
|
|
|
|
|
INNER JOIN Episodes ON Episodes.SeriesId = Seasons.SeriesId
|
|
|
|
|
AND Episodes.SeasonNumber = Seasons.SeasonNumber
|
2012-02-25 01:53:01 +01:00
|
|
|
|
LEFT OUTER JOIN EpisodeFiles ON EpisodeFiles.EpisodeFileId = Episodes.EpisodeFileId
|
2012-02-21 07:50:38 +01:00
|
|
|
|
WHERE Seasons.SeriesId = @0", seriesId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return seasons;
|
|
|
|
|
}
|
2012-02-28 06:50:56 +01:00
|
|
|
|
|
|
|
|
|
public virtual void EnsureSeasons(int seriesId, IEnumerable<int> seasons)
|
|
|
|
|
{
|
|
|
|
|
var existingSeasons = GetSeasons(seriesId);
|
|
|
|
|
|
|
|
|
|
foreach (var season in seasons)
|
|
|
|
|
{
|
|
|
|
|
if (!existingSeasons.Contains(season))
|
|
|
|
|
{
|
|
|
|
|
var newSeason = new Season
|
|
|
|
|
{
|
|
|
|
|
SeriesId = seriesId,
|
|
|
|
|
SeasonNumber = season,
|
|
|
|
|
Ignored = season == 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_database.Insert(newSeason);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-21 04:25:19 +01:00
|
|
|
|
}
|
|
|
|
|
} |