Lidarr/src/NzbDrone.Core/History/HistoryRepository.cs

131 lines
4.7 KiB
C#
Raw Normal View History

using System;
2017-09-04 02:20:56 +00:00
using System.Collections.Generic;
2013-02-23 21:29:22 +00:00
using System.Linq;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Music;
using NzbDrone.Core.Qualities;
2013-02-23 21:29:22 +00:00
namespace NzbDrone.Core.History
{
public interface IHistoryRepository : IBasicRepository<History>
{
History MostRecentForAlbum(int albumId);
History MostRecentForDownloadId(string downloadId);
List<History> FindByDownloadId(string downloadId);
List<History> GetByArtist(int artistId, HistoryEventType? eventType);
List<History> GetByAlbum(int albumId, HistoryEventType? eventType);
List<History> FindDownloadHistory(int idArtistId, QualityModel quality);
2021-01-24 19:41:48 +00:00
void DeleteForArtists(List<int> artistIds);
List<History> Since(DateTime date, HistoryEventType? eventType);
2013-02-23 21:29:22 +00:00
}
public class HistoryRepository : BasicRepository<History>, IHistoryRepository
{
public HistoryRepository(IMainDatabase database, IEventAggregator eventAggregator)
: base(database, eventAggregator)
2013-02-23 21:29:22 +00:00
{
}
public History MostRecentForAlbum(int albumId)
{
2020-08-18 20:11:44 +00:00
return Query(h => h.AlbumId == albumId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
}
public History MostRecentForDownloadId(string downloadId)
{
2020-08-18 20:11:44 +00:00
return Query(h => h.DownloadId == downloadId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
}
public List<History> FindByDownloadId(string downloadId)
{
2020-08-18 20:11:44 +00:00
return _database.QueryJoined<History, Artist, Album>(
Builder()
.Join<History, Artist>((h, a) => h.ArtistId == a.Id)
.Join<History, Album>((h, a) => h.AlbumId == a.Id)
.Where<History>(h => h.DownloadId == downloadId),
(history, artist, album) =>
{
history.Artist = artist;
history.Album = album;
return history;
}).ToList();
}
public List<History> GetByArtist(int artistId, HistoryEventType? eventType)
{
2020-08-18 20:11:44 +00:00
var builder = Builder().Where<History>(h => h.ArtistId == artistId);
if (eventType.HasValue)
{
2020-08-18 20:11:44 +00:00
builder.Where<History>(h => h.EventType == eventType);
}
2020-08-18 20:11:44 +00:00
return Query(builder).OrderByDescending(h => h.Date).ToList();
}
public List<History> GetByAlbum(int albumId, HistoryEventType? eventType)
{
2020-08-18 20:11:44 +00:00
var builder = Builder()
.Join<History, Album>((h, a) => h.AlbumId == a.Id)
.Where<History>(h => h.AlbumId == albumId);
if (eventType.HasValue)
{
2020-08-18 20:11:44 +00:00
builder.Where<History>(h => h.EventType == eventType);
}
2020-08-18 20:11:44 +00:00
return _database.QueryJoined<History, Album>(
builder,
(history, album) =>
{
history.Album = album;
return history;
}).OrderByDescending(h => h.Date).ToList();
}
public List<History> FindDownloadHistory(int idArtistId, QualityModel quality)
{
2020-08-18 20:11:44 +00:00
var allowed = new[] { HistoryEventType.Grabbed, HistoryEventType.DownloadFailed, HistoryEventType.TrackFileImported };
return Query(h => h.ArtistId == idArtistId &&
h.Quality == quality &&
allowed.Contains(h.EventType));
}
2021-01-24 19:41:48 +00:00
public void DeleteForArtists(List<int> artistIds)
{
2021-01-24 19:41:48 +00:00
Delete(c => artistIds.Contains(c.ArtistId));
}
2020-08-18 20:11:44 +00:00
protected override SqlBuilder PagedBuilder() => new SqlBuilder()
.Join<History, Artist>((h, a) => h.ArtistId == a.Id)
.Join<History, Album>((h, a) => h.AlbumId == a.Id)
.LeftJoin<History, Track>((h, t) => h.TrackId == t.Id);
protected override IEnumerable<History> PagedQuery(SqlBuilder builder) =>
_database.QueryJoined<History, Artist, Album, Track>(builder, (history, artist, album, track) =>
{
history.Artist = artist;
history.Album = album;
history.Track = track;
return history;
});
public List<History> Since(DateTime date, HistoryEventType? eventType)
{
2020-08-18 20:11:44 +00:00
var builder = Builder().Where<History>(x => x.Date >= date);
if (eventType.HasValue)
{
2020-08-18 20:11:44 +00:00
builder.Where<History>(h => h.EventType == eventType);
}
2020-08-18 20:11:44 +00:00
return Query(builder).OrderBy(h => h.Date).ToList();
}
2013-02-23 21:29:22 +00:00
}
2017-09-04 02:20:56 +00:00
}