2012-02-21 06:25:19 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Ninject;
|
|
|
|
using NLog;
|
2012-02-21 09:50:38 +03:00
|
|
|
using NzbDrone.Core.Datastore.PetaPoco;
|
2012-02-21 06:25:19 +03:00
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
using PetaPoco;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
|
|
|
public class SeasonProvider
|
|
|
|
{
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2012-02-28 08:50:56 +03:00
|
|
|
|
2012-02-21 06:25:19 +03:00
|
|
|
private readonly IDatabase _database;
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
public SeasonProvider(IDatabase database)
|
|
|
|
{
|
|
|
|
_database = database;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SeasonProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual IList<int> GetSeasons(int seriesId)
|
|
|
|
{
|
2012-02-28 08:50:56 +03:00
|
|
|
return _database.Fetch<Int32>("SELECT DISTINCT SeasonNumber FROM Seasons WHERE SeriesId = @seriesId", new { seriesId }).OrderBy(c => c).ToList();
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void SetIgnore(int seriesId, int seasonNumber, bool isIgnored)
|
|
|
|
{
|
2012-02-28 08:50:56 +03:00
|
|
|
logger.Trace("Setting ignore flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, isIgnored);
|
2012-02-21 06:25:19 +03: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 08:50:56 +03: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 06:25:19 +03: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 08:50:56 +03:00
|
|
|
return _database.Single<Season>("WHERE SeriesId = @seriesId AND SeasonNumber = @seasonNumber",
|
|
|
|
new { seriesId, seasonNumber }).Ignored;
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
2012-02-21 09:50:38 +03:00
|
|
|
|
|
|
|
public virtual List<Season> All(int seriesId)
|
|
|
|
{
|
2012-02-25 03:53:01 +03:00
|
|
|
var seasons = _database.Fetch<Season, Episode, EpisodeFile, Season>(
|
2012-02-28 08:50:56 +03:00
|
|
|
new EpisodeSeasonRelator().MapIt,
|
2012-02-21 09:50:38 +03:00
|
|
|
@"SELECT * FROM Seasons
|
|
|
|
INNER JOIN Episodes ON Episodes.SeriesId = Seasons.SeriesId
|
|
|
|
AND Episodes.SeasonNumber = Seasons.SeasonNumber
|
2012-02-25 03:53:01 +03:00
|
|
|
LEFT OUTER JOIN EpisodeFiles ON EpisodeFiles.EpisodeFileId = Episodes.EpisodeFileId
|
2012-02-21 09:50:38 +03:00
|
|
|
WHERE Seasons.SeriesId = @0", seriesId
|
|
|
|
);
|
|
|
|
|
|
|
|
return seasons;
|
|
|
|
}
|
2012-02-28 08:50:56 +03: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 06:25:19 +03:00
|
|
|
}
|
|
|
|
} |