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