2013-05-03 00:25:51 +00:00
|
|
|
|
using System;
|
2012-02-21 03:25:19 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-04-17 02:46:07 +00:00
|
|
|
|
using Marr.Data.QGen;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-09-14 06:42:09 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-03-25 03:51:32 +00:00
|
|
|
|
|
2012-02-21 03:25:19 +00:00
|
|
|
|
|
2013-02-19 06:01:03 +00:00
|
|
|
|
namespace NzbDrone.Core.Tv
|
2012-02-21 03:25:19 +00:00
|
|
|
|
{
|
2013-02-20 02:05:15 +00:00
|
|
|
|
public interface IEpisodeRepository : IBasicRepository<Episode>
|
2012-02-21 03:25:19 +00:00
|
|
|
|
{
|
2013-05-29 01:28:47 +00:00
|
|
|
|
Episode Find(int seriesId, int season, int episodeNumber);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
Episode Get(int seriesId, DateTime date);
|
2013-05-29 01:28:47 +00:00
|
|
|
|
Episode Find(int seriesId, DateTime date);
|
2013-03-03 23:18:43 +00:00
|
|
|
|
List<Episode> GetEpisodes(int seriesId);
|
|
|
|
|
List<Episode> GetEpisodes(int seriesId, int seasonNumber);
|
|
|
|
|
List<Episode> GetEpisodeByFileId(int fileId);
|
2013-05-02 05:50:34 +00:00
|
|
|
|
PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec, bool includeSpecials);
|
2013-05-29 01:28:47 +00:00
|
|
|
|
Episode FindEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber);
|
2013-02-23 23:08:22 +00:00
|
|
|
|
List<Episode> EpisodesBetweenDates(DateTime startDate, DateTime endDate);
|
2013-07-09 01:22:02 +00:00
|
|
|
|
void SetMonitoredFlat(Episode episode, bool monitored);
|
2013-07-10 02:11:00 +00:00
|
|
|
|
void SetMonitoredBySeason(int seriesId, int seasonNumber, bool monitored);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
void SetFileId(int episodeId, int fileId);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
}
|
2012-02-21 03:25:19 +00:00
|
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
|
public class EpisodeRepository : BasicRepository<Episode>, IEpisodeRepository
|
|
|
|
|
{
|
2013-07-16 00:45:49 +00:00
|
|
|
|
private readonly IDatabase _database;
|
2013-05-02 05:50:34 +00:00
|
|
|
|
|
2013-09-14 06:36:07 +00:00
|
|
|
|
public EpisodeRepository(IDatabase database, IEventAggregator eventAggregator)
|
|
|
|
|
: base(database, eventAggregator)
|
2012-02-21 03:25:19 +00:00
|
|
|
|
{
|
2013-07-16 00:45:49 +00:00
|
|
|
|
_database = database;
|
2012-02-21 03:25:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-29 01:28:47 +00:00
|
|
|
|
public Episode Find(int seriesId, int season, int episodeNumber)
|
|
|
|
|
{
|
|
|
|
|
return Query.SingleOrDefault(s => s.SeriesId == seriesId && s.SeasonNumber == season && s.EpisodeNumber == episodeNumber);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
|
public Episode Get(int seriesId, DateTime date)
|
2012-02-21 03:25:19 +00:00
|
|
|
|
{
|
2013-07-26 03:25:24 +00:00
|
|
|
|
return Query.Single(s => s.SeriesId == seriesId && s.AirDate == date.ToString(Episode.AIR_DATE_FORMAT));
|
2012-02-21 03:25:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-29 01:28:47 +00:00
|
|
|
|
public Episode Find(int seriesId, DateTime date)
|
|
|
|
|
{
|
2013-07-26 03:25:24 +00:00
|
|
|
|
return Query.SingleOrDefault(s => s.SeriesId == seriesId && s.AirDate == date.ToString(Episode.AIR_DATE_FORMAT));
|
2013-05-29 01:28:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 23:18:43 +00:00
|
|
|
|
public List<Episode> GetEpisodes(int seriesId)
|
2012-02-21 03:25:19 +00:00
|
|
|
|
{
|
2013-03-27 03:44:52 +00:00
|
|
|
|
return Query.Where(s => s.SeriesId == seriesId).ToList();
|
2012-02-21 03:25:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 23:18:43 +00:00
|
|
|
|
public List<Episode> GetEpisodes(int seriesId, int seasonNumber)
|
2012-02-21 03:25:19 +00:00
|
|
|
|
{
|
2013-03-27 03:44:52 +00:00
|
|
|
|
return Query.Where(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber).ToList();
|
2012-02-21 03:25:19 +00:00
|
|
|
|
}
|
2012-02-21 06:50:38 +00:00
|
|
|
|
|
2013-03-03 23:18:43 +00:00
|
|
|
|
public List<Episode> GetEpisodeByFileId(int fileId)
|
2012-02-21 06:50:38 +00:00
|
|
|
|
{
|
2013-05-13 00:36:23 +00:00
|
|
|
|
return Query.Where(e => e.EpisodeFileId == fileId).ToList();
|
2012-02-21 06:50:38 +00:00
|
|
|
|
}
|
2012-02-28 05:50:56 +00:00
|
|
|
|
|
2013-05-02 05:50:34 +00:00
|
|
|
|
public PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec, bool includeSpecials)
|
2012-02-28 05:50:56 +00:00
|
|
|
|
{
|
2013-05-07 07:22:19 +00:00
|
|
|
|
var currentTime = DateTime.UtcNow;
|
|
|
|
|
var startingSeasonNumber = 1;
|
|
|
|
|
|
2013-05-02 05:50:34 +00:00
|
|
|
|
if (includeSpecials)
|
2012-02-28 05:50:56 +00:00
|
|
|
|
{
|
2013-05-07 07:22:19 +00:00
|
|
|
|
startingSeasonNumber = 0;
|
2012-02-28 05:50:56 +00:00
|
|
|
|
}
|
2013-05-13 00:36:23 +00:00
|
|
|
|
|
2013-07-22 04:39:26 +00:00
|
|
|
|
pagingSpec.Records = GetEpisodesWithoutFilesQuery(pagingSpec, currentTime, startingSeasonNumber).ToList();
|
|
|
|
|
pagingSpec.TotalRecords = GetEpisodesWithoutFilesQuery(pagingSpec, currentTime, startingSeasonNumber).GetRowCount();
|
2013-05-02 05:50:34 +00:00
|
|
|
|
|
|
|
|
|
return pagingSpec;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-29 01:28:47 +00:00
|
|
|
|
public Episode FindEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
|
{
|
2013-07-27 08:02:56 +00:00
|
|
|
|
return Query.SingleOrDefault(s => s.SeriesId == seriesId && s.SceneSeasonNumber == seasonNumber && s.SceneEpisodeNumber == episodeNumber);
|
2013-05-29 01:28:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 23:08:22 +00:00
|
|
|
|
|
|
|
|
|
public List<Episode> EpisodesBetweenDates(DateTime startDate, DateTime endDate)
|
|
|
|
|
{
|
2013-07-15 22:52:00 +00:00
|
|
|
|
return Query.Join<Episode, Series>(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id)
|
2013-07-24 05:28:06 +00:00
|
|
|
|
.Where<Episode>(e => e.AirDateUtc >= startDate)
|
|
|
|
|
.AndWhere(e => e.AirDateUtc <= endDate)
|
2013-07-15 22:52:00 +00:00
|
|
|
|
.AndWhere(e => e.Monitored)
|
|
|
|
|
.AndWhere(e => e.Series.Monitored)
|
|
|
|
|
.ToList();
|
2013-02-23 23:08:22 +00:00
|
|
|
|
}
|
2013-03-01 07:03:41 +00:00
|
|
|
|
|
2013-07-09 01:22:02 +00:00
|
|
|
|
public void SetMonitoredFlat(Episode episode, bool monitored)
|
2013-03-01 07:03:41 +00:00
|
|
|
|
{
|
2013-07-09 01:22:02 +00:00
|
|
|
|
episode.Monitored = monitored;
|
|
|
|
|
SetFields(episode, p => p.Monitored);
|
2013-03-01 07:03:41 +00:00
|
|
|
|
}
|
2013-04-15 01:41:39 +00:00
|
|
|
|
|
2013-07-10 02:11:00 +00:00
|
|
|
|
public void SetMonitoredBySeason(int seriesId, int seasonNumber, bool monitored)
|
|
|
|
|
{
|
2013-07-16 00:45:49 +00:00
|
|
|
|
var mapper = _database.GetDataMapper();
|
2013-07-10 02:11:00 +00:00
|
|
|
|
|
2013-07-16 00:45:49 +00:00
|
|
|
|
mapper.AddParameter("seriesId", seriesId);
|
|
|
|
|
mapper.AddParameter("seasonNumber", seasonNumber);
|
|
|
|
|
mapper.AddParameter("monitored", monitored);
|
2013-07-10 02:11:00 +00:00
|
|
|
|
|
2013-07-16 00:45:49 +00:00
|
|
|
|
const string sql = "UPDATE Episodes " +
|
|
|
|
|
"SET Monitored = @monitored " +
|
|
|
|
|
"WHERE SeriesId = @seriesId " +
|
|
|
|
|
"AND SeasonNumber = @seasonNumber";
|
|
|
|
|
|
|
|
|
|
mapper.ExecuteNonQuery(sql);
|
2013-07-10 02:11:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
|
public void SetFileId(int episodeId, int fileId)
|
|
|
|
|
{
|
|
|
|
|
SetFields(new Episode { Id = episodeId, EpisodeFileId = fileId }, episode => episode.EpisodeFileId);
|
|
|
|
|
}
|
2013-07-22 04:39:26 +00:00
|
|
|
|
|
|
|
|
|
private SortBuilder<Episode> GetEpisodesWithoutFilesQuery(PagingSpec<Episode> pagingSpec, DateTime currentTime, int startingSeasonNumber)
|
|
|
|
|
{
|
|
|
|
|
return Query.Join<Episode, Series>(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id)
|
|
|
|
|
.Where(e => e.EpisodeFileId == 0)
|
|
|
|
|
.AndWhere(e => e.SeasonNumber >= startingSeasonNumber)
|
2013-07-24 05:28:06 +00:00
|
|
|
|
.AndWhere(e => e.AirDateUtc <= currentTime)
|
2013-07-22 04:39:26 +00:00
|
|
|
|
.AndWhere(e => e.Monitored)
|
|
|
|
|
.AndWhere(e => e.Series.Monitored)
|
|
|
|
|
.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
|
|
|
|
|
.Skip(pagingSpec.PagingOffset())
|
|
|
|
|
.Take(pagingSpec.PageSize);
|
|
|
|
|
}
|
2012-02-21 03:25:19 +00:00
|
|
|
|
}
|
2013-05-03 00:25:51 +00:00
|
|
|
|
}
|