2020-01-05 21:49:08 +00:00
|
|
|
using System.Collections.Generic;
|
2018-09-21 19:30:09 +00:00
|
|
|
using System.Linq;
|
2021-08-04 04:00:28 +00:00
|
|
|
using Dapper;
|
2013-02-19 06:56:02 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-09-14 06:36:07 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
namespace NzbDrone.Core.Tv
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
public interface ISeriesRepository : IBasicRepository<Series>
|
2013-02-19 06:56:02 +00:00
|
|
|
{
|
|
|
|
bool SeriesPathExists(string path);
|
2013-04-15 01:41:39 +00:00
|
|
|
Series FindByTitle(string cleanTitle);
|
2013-10-31 23:50:39 +00:00
|
|
|
Series FindByTitle(string cleanTitle, int year);
|
2020-01-05 21:49:08 +00:00
|
|
|
List<Series> FindByTitleInexact(string cleanTitle);
|
2013-03-02 18:25:39 +00:00
|
|
|
Series FindByTvdbId(int tvdbId);
|
2013-08-19 05:53:26 +00:00
|
|
|
Series FindByTvRageId(int tvRageId);
|
2018-09-21 19:30:09 +00:00
|
|
|
Series FindByPath(string path);
|
2022-11-25 03:15:20 +00:00
|
|
|
List<int> AllSeriesTvdbIds();
|
2021-08-04 04:00:28 +00:00
|
|
|
Dictionary<int, string> AllSeriesPaths();
|
2023-05-27 22:58:56 +00:00
|
|
|
Dictionary<int, List<int>> AllSeriesTags();
|
2013-02-19 06:56:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
public class SeriesRepository : BasicRepository<Series>, ISeriesRepository
|
2013-02-19 06:56:02 +00:00
|
|
|
{
|
2015-05-03 19:46:21 +00:00
|
|
|
public SeriesRepository(IMainDatabase database, IEventAggregator eventAggregator)
|
2013-09-14 06:36:07 +00:00
|
|
|
: base(database, eventAggregator)
|
2013-02-19 06:56:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SeriesPathExists(string path)
|
|
|
|
{
|
2021-08-04 04:00:28 +00:00
|
|
|
return Query(c => c.Path == path).Any();
|
2013-02-19 06:56:02 +00:00
|
|
|
}
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
public Series FindByTitle(string cleanTitle)
|
2013-02-19 06:56:02 +00:00
|
|
|
{
|
2014-02-22 08:53:29 +00:00
|
|
|
cleanTitle = cleanTitle.ToLowerInvariant();
|
|
|
|
|
2021-08-04 04:00:28 +00:00
|
|
|
var series = Query(s => s.CleanTitle == cleanTitle)
|
2020-03-19 02:30:02 +00:00
|
|
|
.ToList();
|
|
|
|
|
|
|
|
return ReturnSingleSeriesOrThrow(series);
|
2013-02-19 06:56:02 +00:00
|
|
|
}
|
2013-03-02 18:25:39 +00:00
|
|
|
|
2013-10-31 23:50:39 +00:00
|
|
|
public Series FindByTitle(string cleanTitle, int year)
|
|
|
|
{
|
2014-02-22 08:53:29 +00:00
|
|
|
cleanTitle = cleanTitle.ToLowerInvariant();
|
|
|
|
|
2021-08-04 04:00:28 +00:00
|
|
|
var series = Query(s => s.CleanTitle == cleanTitle && s.Year == year).ToList();
|
2020-03-19 02:30:02 +00:00
|
|
|
|
|
|
|
return ReturnSingleSeriesOrThrow(series);
|
2013-10-31 23:50:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 21:49:08 +00:00
|
|
|
public List<Series> FindByTitleInexact(string cleanTitle)
|
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
var builder = Builder().Where($"instr(@cleanTitle, \"Series\".\"CleanTitle\")", new { cleanTitle = cleanTitle });
|
|
|
|
|
|
|
|
if (_database.DatabaseType == DatabaseType.PostgreSQL)
|
|
|
|
{
|
|
|
|
builder = Builder().Where($"(strpos(@cleanTitle, \"Series\".\"CleanTitle\") > 0)", new { cleanTitle = cleanTitle });
|
|
|
|
}
|
2020-01-05 21:49:08 +00:00
|
|
|
|
2021-08-04 04:00:28 +00:00
|
|
|
return Query(builder).ToList();
|
2020-01-05 21:49:08 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 18:25:39 +00:00
|
|
|
public Series FindByTvdbId(int tvdbId)
|
|
|
|
{
|
2021-08-04 04:00:28 +00:00
|
|
|
return Query(s => s.TvdbId == tvdbId).SingleOrDefault();
|
2013-03-02 18:25:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 05:53:26 +00:00
|
|
|
public Series FindByTvRageId(int tvRageId)
|
|
|
|
{
|
2021-08-04 04:00:28 +00:00
|
|
|
return Query(s => s.TvRageId == tvRageId).SingleOrDefault();
|
2013-08-19 05:53:26 +00:00
|
|
|
}
|
2018-09-21 19:30:09 +00:00
|
|
|
|
|
|
|
public Series FindByPath(string path)
|
|
|
|
{
|
2021-08-04 04:00:28 +00:00
|
|
|
return Query(s => s.Path == path)
|
2018-09-21 19:30:09 +00:00
|
|
|
.FirstOrDefault();
|
|
|
|
}
|
2020-03-19 02:30:02 +00:00
|
|
|
|
2022-11-25 03:15:20 +00:00
|
|
|
public List<int> AllSeriesTvdbIds()
|
|
|
|
{
|
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
return conn.Query<int>("SELECT \"TvdbId\" FROM \"Series\"").ToList();
|
2022-11-25 03:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 04:00:28 +00:00
|
|
|
public Dictionary<int, string> AllSeriesPaths()
|
2020-10-18 21:59:26 +00:00
|
|
|
{
|
2021-08-04 04:00:28 +00:00
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
var strSql = "SELECT \"Id\" AS Key, \"Path\" AS Value FROM \"Series\"";
|
2021-08-04 04:00:28 +00:00
|
|
|
return conn.Query<KeyValuePair<int, string>>(strSql).ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
}
|
2020-10-18 21:59:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 22:58:56 +00:00
|
|
|
public Dictionary<int, List<int>> AllSeriesTags()
|
|
|
|
{
|
|
|
|
using (var conn = _database.OpenConnection())
|
|
|
|
{
|
2023-08-13 18:26:05 +00:00
|
|
|
var strSql = "SELECT \"Id\" AS Key, \"Tags\" AS Value FROM \"Series\" WHERE \"Tags\" IS NOT NULL";
|
2023-05-27 22:58:56 +00:00
|
|
|
return conn.Query<KeyValuePair<int, List<int>>>(strSql).ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 02:30:02 +00:00
|
|
|
private Series ReturnSingleSeriesOrThrow(List<Series> series)
|
|
|
|
{
|
|
|
|
if (series.Count == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (series.Count == 1)
|
|
|
|
{
|
|
|
|
return series.First();
|
|
|
|
}
|
|
|
|
|
2022-12-13 06:44:08 +00:00
|
|
|
throw new MultipleSeriesFoundException(series, "Expected one series, but found {0}. Matching series: {1}", series.Count, string.Join(", ", series));
|
2020-03-19 02:30:02 +00:00
|
|
|
}
|
2013-02-19 06:56:02 +00:00
|
|
|
}
|
2018-09-21 19:30:09 +00:00
|
|
|
}
|