2012-02-21 06:25:19 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-05-02 08:50:34 +03:00
|
|
|
using System.ComponentModel;
|
2013-03-24 07:16:00 +03:00
|
|
|
using System.Data;
|
2012-02-21 06:25:19 +03:00
|
|
|
using System.Linq;
|
2013-05-02 08:50:34 +03:00
|
|
|
using Marr.Data;
|
2013-04-17 05:46:07 +03:00
|
|
|
using Marr.Data.QGen;
|
2013-02-20 05:05:15 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-04-15 04:41:39 +03:00
|
|
|
using NzbDrone.Core.Model;
|
2013-03-25 06:51:32 +03:00
|
|
|
|
2012-02-21 06:25:19 +03:00
|
|
|
|
2013-02-19 09:01:03 +03:00
|
|
|
namespace NzbDrone.Core.Tv
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
public interface IEpisodeRepository : IBasicRepository<Episode>
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-02-20 05:05:15 +03:00
|
|
|
Episode Get(int seriesId, int season, int episodeNumber);
|
|
|
|
Episode Get(int seriesId, DateTime date);
|
2013-03-04 02:18:43 +03:00
|
|
|
List<Episode> GetEpisodes(int seriesId);
|
|
|
|
List<Episode> GetEpisodes(int seriesId, int seasonNumber);
|
|
|
|
List<Episode> GetEpisodeByFileId(int fileId);
|
2013-05-02 08:50:34 +03:00
|
|
|
PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec, bool includeSpecials);
|
2013-02-20 05:05:15 +03:00
|
|
|
Episode GetEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber);
|
2013-03-04 02:18:43 +03:00
|
|
|
List<Episode> EpisodesWithFiles();
|
2013-02-24 02:08:22 +03:00
|
|
|
List<Episode> EpisodesBetweenDates(DateTime startDate, DateTime endDate);
|
2013-03-01 10:03:41 +03:00
|
|
|
void SetIgnoreFlat(Episode episode, bool ignoreFlag);
|
2013-04-15 04:41:39 +03:00
|
|
|
void SetPostDownloadStatus(int episodeId, PostDownloadStatusType status);
|
|
|
|
void SetFileId(int episodeId, int fileId);
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
2012-02-21 06:25:19 +03:00
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
public class EpisodeRepository : BasicRepository<Episode>, IEpisodeRepository
|
|
|
|
{
|
2013-05-02 08:50:34 +03:00
|
|
|
private readonly IDataMapper _dataMapper;
|
|
|
|
|
2013-03-25 06:51:32 +03:00
|
|
|
public EpisodeRepository(IDatabase database)
|
2013-03-24 07:16:00 +03:00
|
|
|
: base(database)
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-05-02 08:50:34 +03:00
|
|
|
_dataMapper = database.DataMapper;
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
public Episode Get(int seriesId, int season, int episodeNumber)
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-03-27 06:44:52 +03:00
|
|
|
return Query.Single(s => s.SeriesId == seriesId && s.SeasonNumber == season && s.EpisodeNumber == episodeNumber);
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
|
|
|
|
2013-02-20 05:05:15 +03:00
|
|
|
public Episode Get(int seriesId, DateTime date)
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-03-27 06:44:52 +03:00
|
|
|
return Query.Single(s => s.SeriesId == seriesId && s.AirDate.HasValue && s.AirDate.Value.Date == date.Date);
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
|
|
|
|
2013-03-04 02:18:43 +03:00
|
|
|
public List<Episode> GetEpisodes(int seriesId)
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-03-27 06:44:52 +03:00
|
|
|
return Query.Where(s => s.SeriesId == seriesId).ToList();
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
|
|
|
|
2013-03-04 02:18:43 +03:00
|
|
|
public List<Episode> GetEpisodes(int seriesId, int seasonNumber)
|
2012-02-21 06:25:19 +03:00
|
|
|
{
|
2013-03-27 06:44:52 +03:00
|
|
|
return Query.Where(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber).ToList();
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
2012-02-21 09:50:38 +03:00
|
|
|
|
2013-03-04 02:18:43 +03:00
|
|
|
public List<Episode> GetEpisodeByFileId(int fileId)
|
2012-02-21 09:50:38 +03:00
|
|
|
{
|
2013-03-27 06:44:52 +03:00
|
|
|
return Query.Where(s => s.EpisodeFile != null && s.EpisodeFile.Id == fileId).ToList();
|
2012-02-21 09:50:38 +03:00
|
|
|
}
|
2012-02-28 08:50:56 +03:00
|
|
|
|
2013-05-02 08:50:34 +03:00
|
|
|
public PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec, bool includeSpecials)
|
2012-02-28 08:50:56 +03:00
|
|
|
{
|
2013-05-02 08:50:34 +03:00
|
|
|
//TODO: Join in the series title so we can do sorting on it
|
|
|
|
if (!pagingSpec.SortKey.Equals("SeriesTitle", StringComparison.InvariantCultureIgnoreCase) &&
|
|
|
|
!pagingSpec.SortKey.Equals("AirDate", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid SortKey: " + pagingSpec.SortKey, "pagingSpec");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeSpecials)
|
2012-02-28 08:50:56 +03:00
|
|
|
{
|
2013-05-02 08:50:34 +03:00
|
|
|
throw new NotImplementedException("Including specials is not available");
|
2012-02-28 08:50:56 +03:00
|
|
|
}
|
2013-02-20 05:05:15 +03:00
|
|
|
|
2013-05-02 08:50:34 +03:00
|
|
|
var orderSql = String.Format("{0} {1}", pagingSpec.SortKey,
|
|
|
|
pagingSpec.SortDirection == ListSortDirection.Ascending ? "ASC" : "DESC");
|
|
|
|
|
|
|
|
var limitSql = String.Format("{0},{1}", (pagingSpec.Page - 1)*pagingSpec.PageSize, pagingSpec.PageSize);
|
|
|
|
|
2013-05-02 10:09:35 +03:00
|
|
|
_dataMapper.AddParameter("currentDate", DateTime.UtcNow);
|
|
|
|
|
2013-05-02 08:50:34 +03:00
|
|
|
var sql = String.Format(@"SELECT Episodes.*, Series.Title as SeriesTitle
|
|
|
|
FROM Episodes
|
|
|
|
INNER JOIN Series
|
|
|
|
ON Episodes.SeriesId = Series.Id
|
|
|
|
WHERE EpisodeFileId = 0
|
|
|
|
AND SeasonNumber > 0
|
2013-05-02 18:18:23 +03:00
|
|
|
AND AirDate <= @currentDate
|
2013-05-02 08:50:34 +03:00
|
|
|
ORDER BY {0}
|
|
|
|
LIMIT {1}",
|
|
|
|
orderSql, limitSql
|
|
|
|
);
|
|
|
|
|
|
|
|
pagingSpec.Records = _dataMapper.Query<Episode>(sql);
|
|
|
|
pagingSpec.TotalRecords = Count();
|
|
|
|
|
|
|
|
return pagingSpec;
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public Episode GetEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
{
|
2013-03-27 06:44:52 +03:00
|
|
|
return Query.Single(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber && s.SceneEpisodeNumber == episodeNumber);
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-03-04 02:18:43 +03:00
|
|
|
public List<Episode> EpisodesWithFiles()
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
2013-03-27 06:44:52 +03:00
|
|
|
return Query.Where(s => s.EpisodeFile != null).ToList();
|
2012-02-28 08:50:56 +03:00
|
|
|
}
|
2013-02-24 02:08:22 +03:00
|
|
|
|
|
|
|
public List<Episode> EpisodesBetweenDates(DateTime startDate, DateTime endDate)
|
|
|
|
{
|
2013-04-17 09:55:39 +03:00
|
|
|
return Query//.Join<Episode, Series>(JoinType.None, e => e.Series, (e, s) => e.SeriesId == s.Id);
|
|
|
|
.Where<Episode>(e => e.AirDate >= startDate && e.AirDate <= endDate).ToList();
|
2013-02-24 02:08:22 +03:00
|
|
|
}
|
2013-03-01 10:03:41 +03:00
|
|
|
|
|
|
|
public void SetIgnoreFlat(Episode episode, bool ignoreFlag)
|
|
|
|
{
|
|
|
|
episode.Ignored = ignoreFlag;
|
2013-03-27 09:16:55 +03:00
|
|
|
SetFields(episode, p => p.Ignored);
|
2013-03-01 10:03:41 +03:00
|
|
|
}
|
2013-04-15 04:41:39 +03:00
|
|
|
|
|
|
|
public void SetPostDownloadStatus(int episodeId, PostDownloadStatusType status)
|
|
|
|
{
|
|
|
|
SetFields(new Episode { Id = episodeId, PostDownloadStatus = status }, episode => episode.PostDownloadStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetFileId(int episodeId, int fileId)
|
|
|
|
{
|
|
|
|
SetFields(new Episode { Id = episodeId, EpisodeFileId = fileId }, episode => episode.EpisodeFileId);
|
|
|
|
}
|
2012-02-21 06:25:19 +03:00
|
|
|
}
|
|
|
|
} |