1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Core/Tv/EpisodeRepository.cs

134 lines
5.3 KiB
C#
Raw Normal View History

2013-05-03 03:25:51 +03:00
using System;
using System.Collections.Generic;
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-05-06 00:24:33 +03:00
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Model;
2013-03-25 06:51:32 +03:00
namespace NzbDrone.Core.Tv
{
public interface IEpisodeRepository : IBasicRepository<Episode>
{
Episode Get(int seriesId, int season, int episodeNumber);
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 GetEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber);
Episode FindEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber);
List<Episode> EpisodesWithFiles();
List<Episode> EpisodesBetweenDates(DateTime startDate, DateTime endDate);
void SetMonitoredFlat(Episode episode, bool monitored);
void SetFileId(int episodeId, int fileId);
}
public class EpisodeRepository : BasicRepository<Episode>, IEpisodeRepository
{
2013-05-02 08:50:34 +03:00
private readonly IDataMapper _dataMapper;
2013-05-06 00:24:33 +03:00
public EpisodeRepository(IDatabase database, IMessageAggregator messageAggregator)
: base(database, messageAggregator)
{
2013-05-02 08:50:34 +03:00
_dataMapper = database.DataMapper;
}
public Episode Get(int seriesId, int season, int episodeNumber)
{
2013-03-27 06:44:52 +03:00
return Query.Single(s => s.SeriesId == seriesId && s.SeasonNumber == season && s.EpisodeNumber == episodeNumber);
}
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-03-27 06:44:52 +03:00
return Query.Single(s => s.SeriesId == seriesId && s.AirDate.HasValue && s.AirDate.Value.Date == date.Date);
}
public Episode Find(int seriesId, DateTime date)
{
return Query.SingleOrDefault(s => s.SeriesId == seriesId && s.AirDate.HasValue && s.AirDate.Value.Date == date.Date);
}
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;
}
var pagingQuery = Query.Where(e => e.EpisodeFileId == 0)
.AndWhere(e => e.SeasonNumber >= startingSeasonNumber)
.AndWhere(e => e.AirDate <= currentTime)
.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
.Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize);
pagingSpec.Records = pagingQuery.ToList();
2013-05-07 05:32:43 +03:00
//TODO: Use the same query for count and records
pagingSpec.TotalRecords = Query.Count(e => e.EpisodeFileId == 0 && e.SeasonNumber >= startingSeasonNumber && e.AirDate <= currentTime);
2013-05-02 08:50:34 +03:00
return pagingSpec;
}
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);
}
public Episode FindEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber)
{
return Query.SingleOrDefault(s => s.SeriesId == seriesId && s.SeasonNumber == seasonNumber && s.SceneEpisodeNumber == episodeNumber);
}
public List<Episode> EpisodesWithFiles()
{
return Query.Where(s => s.EpisodeFileId != 0).ToList();
}
public List<Episode> EpisodesBetweenDates(DateTime startDate, DateTime endDate)
{
return Query.Where<Episode>(e => e.AirDate >= startDate)
2013-05-30 07:06:24 +03:00
.AndWhere(e => e.AirDate <= endDate).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
}
public void SetFileId(int episodeId, int fileId)
{
SetFields(new Episode { Id = episodeId, EpisodeFileId = fileId }, episode => episode.EpisodeFileId);
}
}
2013-05-03 03:25:51 +03:00
}