2017-06-28 02:25:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Marr.Data.QGen;
|
2017-06-18 02:27:01 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2017-07-02 00:21:39 +00:00
|
|
|
|
using NzbDrone.Core.Datastore.Extensions;
|
2017-06-18 02:27:01 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Music
|
|
|
|
|
{
|
|
|
|
|
public interface IAlbumRepository : IBasicRepository<Album>
|
|
|
|
|
{
|
|
|
|
|
bool AlbumPathExists(string path);
|
|
|
|
|
List<Album> GetAlbums(int artistId);
|
|
|
|
|
Album FindByName(string cleanTitle);
|
|
|
|
|
Album FindById(string spotifyId);
|
2017-07-02 00:21:39 +00:00
|
|
|
|
PagingSpec<Album> AlbumsWithoutFiles(PagingSpec<Album> pagingSpec);
|
2017-06-28 02:25:51 +00:00
|
|
|
|
List<Album> AlbumsBetweenDates(DateTime startDate, DateTime endDate, bool includeUnmonitored);
|
2017-06-25 13:17:49 +00:00
|
|
|
|
void SetMonitoredFlat(Album album, bool monitored);
|
2017-06-18 02:27:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class AlbumRepository : BasicRepository<Album>, IAlbumRepository
|
|
|
|
|
{
|
|
|
|
|
public AlbumRepository(IMainDatabase database, IEventAggregator eventAggregator)
|
|
|
|
|
: base(database, eventAggregator)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool AlbumPathExists(string path)
|
|
|
|
|
{
|
|
|
|
|
return Query.Where(c => c.Path == path).Any();
|
|
|
|
|
}
|
|
|
|
|
public List<Album> GetAlbums(int artistId)
|
|
|
|
|
{
|
|
|
|
|
return Query.Where(s => s.ArtistId == artistId).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Album FindById(string foreignAlbumId)
|
|
|
|
|
{
|
|
|
|
|
return Query.Where(s => s.ForeignAlbumId == foreignAlbumId).SingleOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-02 00:21:39 +00:00
|
|
|
|
public PagingSpec<Album> AlbumsWithoutFiles(PagingSpec<Album> pagingSpec)
|
|
|
|
|
{
|
|
|
|
|
var currentTime = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
pagingSpec.TotalRecords = GetMissingAlbumsQuery(pagingSpec, currentTime).GetRowCount();
|
|
|
|
|
pagingSpec.Records = GetMissingAlbumsQuery(pagingSpec, currentTime).ToList();
|
|
|
|
|
|
|
|
|
|
return pagingSpec;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 02:25:51 +00:00
|
|
|
|
public List<Album> AlbumsBetweenDates(DateTime startDate, DateTime endDate, bool includeUnmonitored)
|
|
|
|
|
{
|
|
|
|
|
var query = Query.Join<Album, Artist>(JoinType.Inner, e => e.Artist, (e, s) => e.ArtistId == s.Id)
|
|
|
|
|
.Where<Album>(e => e.ReleaseDate >= startDate)
|
|
|
|
|
.AndWhere(e => e.ReleaseDate <= endDate);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!includeUnmonitored)
|
|
|
|
|
{
|
|
|
|
|
query.AndWhere(e => e.Monitored)
|
|
|
|
|
.AndWhere(e => e.Artist.Monitored);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-02 00:21:39 +00:00
|
|
|
|
private SortBuilder<Album> GetMissingAlbumsQuery(PagingSpec<Album> pagingSpec, DateTime currentTime)
|
|
|
|
|
{
|
|
|
|
|
return Query.Join<Album, Artist>(JoinType.Inner, e => e.Artist, (e, s) => e.ArtistId == s.Id)
|
|
|
|
|
.Where<Album>(pagingSpec.FilterExpression)
|
|
|
|
|
|
|
|
|
|
.AndWhere(BuildReleaseDateCutoffWhereClause(currentTime))
|
|
|
|
|
//.Where<Track>(t => t.TrackFileId == 0)
|
|
|
|
|
.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
|
|
|
|
|
.Skip(pagingSpec.PagingOffset())
|
|
|
|
|
.Take(pagingSpec.PageSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string BuildReleaseDateCutoffWhereClause(DateTime currentTime)
|
|
|
|
|
{
|
|
|
|
|
return string.Format("WHERE datetime(strftime('%s', [t0].[ReleaseDate]), 'unixepoch') <= '{0}'",
|
|
|
|
|
currentTime.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 13:17:49 +00:00
|
|
|
|
public void SetMonitoredFlat(Album album, bool monitored)
|
|
|
|
|
{
|
|
|
|
|
album.Monitored = monitored;
|
|
|
|
|
SetFields(album, p => p.Monitored);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 02:27:01 +00:00
|
|
|
|
public Album FindByName(string cleanTitle)
|
|
|
|
|
{
|
|
|
|
|
cleanTitle = cleanTitle.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
return Query.Where(s => s.CleanTitle == cleanTitle)
|
|
|
|
|
.SingleOrDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|