1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-29 02:57:15 +02:00
Sonarr/NzbDrone.Core/Tv/EpisodeRepository.cs

140 lines
5.7 KiB
C#
Raw Normal View History

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