Refactor Blacklist for Album Releases (#48)

* Refactor Blacklist for Album Releases
* Fix Test
pull/6/head
Qstick 8 years ago committed by GitHub
parent 87f13e5e9c
commit 337f74a184

@ -2,15 +2,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using NzbDrone.Api.REST; using NzbDrone.Api.REST;
using NzbDrone.Core.Qualities; using NzbDrone.Core.Qualities;
using NzbDrone.Api.Series; using NzbDrone.Api.Music;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
namespace NzbDrone.Api.Blacklist namespace NzbDrone.Api.Blacklist
{ {
public class BlacklistResource : RestResource public class BlacklistResource : RestResource
{ {
public int SeriesId { get; set; } public int ArtistId { get; set; }
public List<int> EpisodeIds { get; set; } public List<int> AlbumIds { get; set; }
public string SourceTitle { get; set; } public string SourceTitle { get; set; }
public QualityModel Quality { get; set; } public QualityModel Quality { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }
@ -18,7 +18,7 @@ namespace NzbDrone.Api.Blacklist
public string Indexer { get; set; } public string Indexer { get; set; }
public string Message { get; set; } public string Message { get; set; }
public SeriesResource Series { get; set; } public ArtistResource Artist { get; set; }
} }
public static class BlacklistResourceMapper public static class BlacklistResourceMapper
@ -31,8 +31,8 @@ namespace NzbDrone.Api.Blacklist
{ {
Id = model.Id, Id = model.Id,
SeriesId = model.SeriesId, ArtistId = model.ArtistId,
EpisodeIds = model.EpisodeIds, AlbumIds = model.AlbumIds,
SourceTitle = model.SourceTitle, SourceTitle = model.SourceTitle,
Quality = model.Quality, Quality = model.Quality,
Date = model.Date, Date = model.Date,
@ -40,7 +40,7 @@ namespace NzbDrone.Api.Blacklist
Indexer = model.Indexer, Indexer = model.Indexer,
Message = model.Message, Message = model.Message,
Series = model.Series.ToResource() Artist = model.Artist.ToResource()
}; };
} }
} }

@ -19,10 +19,10 @@ namespace NzbDrone.Core.Test.Blacklisting
{ {
_blacklist = new Blacklist _blacklist = new Blacklist
{ {
SeriesId = 12345, ArtistId = 12345,
EpisodeIds = new List<int> { 1 }, AlbumIds = new List<int> { 1 },
Quality = new QualityModel(Quality.FLAC), Quality = new QualityModel(Quality.FLAC),
SourceTitle = "series.title.s01e01", SourceTitle = "artist.name.album.title",
Date = DateTime.UtcNow Date = DateTime.UtcNow
}; };
} }
@ -35,11 +35,11 @@ namespace NzbDrone.Core.Test.Blacklisting
} }
[Test] [Test]
public void should_should_have_episode_ids() public void should_should_have_album_ids()
{ {
Subject.Insert(_blacklist); Subject.Insert(_blacklist);
Subject.All().First().EpisodeIds.Should().Contain(_blacklist.EpisodeIds); Subject.All().First().AlbumIds.Should().Contain(_blacklist.AlbumIds);
} }
[Test] [Test]
@ -47,7 +47,7 @@ namespace NzbDrone.Core.Test.Blacklisting
{ {
Subject.Insert(_blacklist); Subject.Insert(_blacklist);
Subject.BlacklistedByTitle(_blacklist.SeriesId, _blacklist.SourceTitle.ToUpperInvariant()).Should().HaveCount(1); Subject.BlacklistedByTitle(_blacklist.ArtistId, _blacklist.SourceTitle.ToUpperInvariant()).Should().HaveCount(1);
} }
} }
} }

@ -19,10 +19,10 @@ namespace NzbDrone.Core.Test.Blacklisting
{ {
_event = new DownloadFailedEvent _event = new DownloadFailedEvent
{ {
SeriesId = 12345, ArtistId = 12345,
EpisodeIds = new List<int> {1}, AlbumIds = new List<int> {1},
Quality = new QualityModel(Quality.MP3_320), Quality = new QualityModel(Quality.MP3_320),
SourceTitle = "series.title.s01e01", SourceTitle = "artist.name.album.title",
DownloadClient = "SabnzbdClient", DownloadClient = "SabnzbdClient",
DownloadId = "Sabnzbd_nzo_2dfh73k" DownloadId = "Sabnzbd_nzo_2dfh73k"
}; };
@ -40,7 +40,7 @@ namespace NzbDrone.Core.Test.Blacklisting
Subject.Handle(_event); Subject.Handle(_event);
Mocker.GetMock<IBlacklistRepository>() Mocker.GetMock<IBlacklistRepository>()
.Verify(v => v.Insert(It.Is<Blacklist>(b => b.EpisodeIds == _event.EpisodeIds)), Times.Once()); .Verify(v => v.Insert(It.Is<Blacklist>(b => b.AlbumIds == _event.AlbumIds)), Times.Once());
} }
[Test] [Test]
@ -52,7 +52,7 @@ namespace NzbDrone.Core.Test.Blacklisting
_event.Data.Remove("protocol"); _event.Data.Remove("protocol");
Mocker.GetMock<IBlacklistRepository>() Mocker.GetMock<IBlacklistRepository>()
.Verify(v => v.Insert(It.Is<Blacklist>(b => b.EpisodeIds == _event.EpisodeIds)), Times.Once()); .Verify(v => v.Insert(It.Is<Blacklist>(b => b.AlbumIds == _event.AlbumIds)), Times.Once());
} }
} }
} }

@ -5,7 +5,7 @@ using NzbDrone.Core.Blacklisting;
using NzbDrone.Core.Housekeeping.Housekeepers; using NzbDrone.Core.Housekeeping.Housekeepers;
using NzbDrone.Core.Qualities; using NzbDrone.Core.Qualities;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv; using NzbDrone.Core.Music;
using System.Collections.Generic; using System.Collections.Generic;
namespace NzbDrone.Core.Test.Housekeeping.Housekeepers namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
@ -17,7 +17,7 @@ namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
public void should_delete_orphaned_blacklist_items() public void should_delete_orphaned_blacklist_items()
{ {
var blacklist = Builder<Blacklist>.CreateNew() var blacklist = Builder<Blacklist>.CreateNew()
.With(h => h.EpisodeIds = new List<int>()) .With(h => h.AlbumIds = new List<int>())
.With(h => h.Quality = new QualityModel()) .With(h => h.Quality = new QualityModel())
.BuildNew(); .BuildNew();
@ -29,14 +29,14 @@ namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
[Test] [Test]
public void should_not_delete_unorphaned_blacklist_items() public void should_not_delete_unorphaned_blacklist_items()
{ {
var series = Builder<Series>.CreateNew().BuildNew(); var artist = Builder<Artist>.CreateNew().BuildNew();
Db.Insert(series); Db.Insert(artist);
var blacklist = Builder<Blacklist>.CreateNew() var blacklist = Builder<Blacklist>.CreateNew()
.With(h => h.EpisodeIds = new List<int>()) .With(h => h.AlbumIds = new List<int>())
.With(h => h.Quality = new QualityModel()) .With(h => h.Quality = new QualityModel())
.With(b => b.SeriesId = series.Id) .With(b => b.ArtistId = artist.Id)
.BuildNew(); .BuildNew();
Db.Insert(blacklist); Db.Insert(blacklist);

@ -3,15 +3,15 @@ using System.Collections.Generic;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.Qualities; using NzbDrone.Core.Qualities;
using NzbDrone.Core.Tv; using NzbDrone.Core.Music;
namespace NzbDrone.Core.Blacklisting namespace NzbDrone.Core.Blacklisting
{ {
public class Blacklist : ModelBase public class Blacklist : ModelBase
{ {
public int SeriesId { get; set; } public int ArtistId { get; set; }
public Series Series { get; set; } public Artist Artist { get; set; }
public List<int> EpisodeIds { get; set; } public List<int> AlbumIds { get; set; }
public string SourceTitle { get; set; } public string SourceTitle { get; set; }
public QualityModel Quality { get; set; } public QualityModel Quality { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }

@ -2,15 +2,15 @@
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
using Marr.Data.QGen; using Marr.Data.QGen;
using NzbDrone.Core.Tv; using NzbDrone.Core.Music;
namespace NzbDrone.Core.Blacklisting namespace NzbDrone.Core.Blacklisting
{ {
public interface IBlacklistRepository : IBasicRepository<Blacklist> public interface IBlacklistRepository : IBasicRepository<Blacklist>
{ {
List<Blacklist> BlacklistedByTitle(int seriesId, string sourceTitle); List<Blacklist> BlacklistedByTitle(int artistId, string sourceTitle);
List<Blacklist> BlacklistedByTorrentInfoHash(int seriesId, string torrentInfoHash); List<Blacklist> BlacklistedByTorrentInfoHash(int artistId, string torrentInfoHash);
List<Blacklist> BlacklistedBySeries(int seriesId); List<Blacklist> BlacklistedByArtist(int artistId);
} }
public class BlacklistRepository : BasicRepository<Blacklist>, IBlacklistRepository public class BlacklistRepository : BasicRepository<Blacklist>, IBlacklistRepository
@ -20,26 +20,26 @@ namespace NzbDrone.Core.Blacklisting
{ {
} }
public List<Blacklist> BlacklistedByTitle(int seriesId, string sourceTitle) public List<Blacklist> BlacklistedByTitle(int artistId, string sourceTitle)
{ {
return Query.Where(e => e.SeriesId == seriesId) return Query.Where(e => e.ArtistId == artistId)
.AndWhere(e => e.SourceTitle.Contains(sourceTitle)); .AndWhere(e => e.SourceTitle.Contains(sourceTitle));
} }
public List<Blacklist> BlacklistedByTorrentInfoHash(int seriesId, string torrentInfoHash) public List<Blacklist> BlacklistedByTorrentInfoHash(int artistId, string torrentInfoHash)
{ {
return Query.Where(e => e.SeriesId == seriesId) return Query.Where(e => e.ArtistId == artistId)
.AndWhere(e => e.TorrentInfoHash.Contains(torrentInfoHash)); .AndWhere(e => e.TorrentInfoHash.Contains(torrentInfoHash));
} }
public List<Blacklist> BlacklistedBySeries(int seriesId) public List<Blacklist> BlacklistedByArtist(int artistId)
{ {
return Query.Where(b => b.SeriesId == seriesId); return Query.Where(b => b.ArtistId == artistId);
} }
protected override SortBuilder<Blacklist> GetPagedQuery(QueryBuilder<Blacklist> query, PagingSpec<Blacklist> pagingSpec) protected override SortBuilder<Blacklist> GetPagedQuery(QueryBuilder<Blacklist> query, PagingSpec<Blacklist> pagingSpec)
{ {
var baseQuery = query.Join<Blacklist, Series>(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id); var baseQuery = query.Join<Blacklist, Artist>(JoinType.Inner, h => h.Artist, (h, s) => h.ArtistId == s.Id);
return base.GetPagedQuery(baseQuery, pagingSpec); return base.GetPagedQuery(baseQuery, pagingSpec);
} }

@ -7,13 +7,13 @@ using NzbDrone.Core.Indexers;
using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv.Events; using NzbDrone.Core.Music.Events;
namespace NzbDrone.Core.Blacklisting namespace NzbDrone.Core.Blacklisting
{ {
public interface IBlacklistService public interface IBlacklistService
{ {
bool Blacklisted(int seriesId, ReleaseInfo release); bool Blacklisted(int artistId, ReleaseInfo release);
PagingSpec<Blacklist> Paged(PagingSpec<Blacklist> pagingSpec); PagingSpec<Blacklist> Paged(PagingSpec<Blacklist> pagingSpec);
void Delete(int id); void Delete(int id);
} }
@ -21,7 +21,7 @@ namespace NzbDrone.Core.Blacklisting
IExecute<ClearBlacklistCommand>, IExecute<ClearBlacklistCommand>,
IHandle<DownloadFailedEvent>, IHandle<DownloadFailedEvent>,
IHandleAsync<SeriesDeletedEvent> IHandleAsync<ArtistDeletedEvent>
{ {
private readonly IBlacklistRepository _blacklistRepository; private readonly IBlacklistRepository _blacklistRepository;
@ -30,9 +30,9 @@ namespace NzbDrone.Core.Blacklisting
_blacklistRepository = blacklistRepository; _blacklistRepository = blacklistRepository;
} }
public bool Blacklisted(int seriesId, ReleaseInfo release) public bool Blacklisted(int artistId, ReleaseInfo release)
{ {
var blacklistedByTitle = _blacklistRepository.BlacklistedByTitle(seriesId, release.Title); var blacklistedByTitle = _blacklistRepository.BlacklistedByTitle(artistId, release.Title);
if (release.DownloadProtocol == DownloadProtocol.Torrent) if (release.DownloadProtocol == DownloadProtocol.Torrent)
{ {
@ -46,7 +46,7 @@ namespace NzbDrone.Core.Blacklisting
.Any(b => SameTorrent(b, torrentInfo)); .Any(b => SameTorrent(b, torrentInfo));
} }
var blacklistedByTorrentInfohash = _blacklistRepository.BlacklistedByTorrentInfoHash(seriesId, torrentInfo.InfoHash); var blacklistedByTorrentInfohash = _blacklistRepository.BlacklistedByTorrentInfoHash(artistId, torrentInfo.InfoHash);
return blacklistedByTorrentInfohash.Any(b => SameTorrent(b, torrentInfo)); return blacklistedByTorrentInfohash.Any(b => SameTorrent(b, torrentInfo));
} }
@ -128,8 +128,8 @@ namespace NzbDrone.Core.Blacklisting
{ {
var blacklist = new Blacklist var blacklist = new Blacklist
{ {
SeriesId = message.SeriesId, ArtistId = message.ArtistId,
EpisodeIds = message.EpisodeIds, AlbumIds = message.AlbumIds,
SourceTitle = message.SourceTitle, SourceTitle = message.SourceTitle,
Quality = message.Quality, Quality = message.Quality,
Date = DateTime.UtcNow, Date = DateTime.UtcNow,
@ -144,9 +144,9 @@ namespace NzbDrone.Core.Blacklisting
_blacklistRepository.Insert(blacklist); _blacklistRepository.Insert(blacklist);
} }
public void HandleAsync(SeriesDeletedEvent message) public void HandleAsync(ArtistDeletedEvent message)
{ {
var blacklisted = _blacklistRepository.BlacklistedBySeries(message.Series.Id); var blacklisted = _blacklistRepository.BlacklistedByArtist(message.Artist.Id);
_blacklistRepository.DeleteMany(blacklisted); _blacklistRepository.DeleteMany(blacklisted);
} }

@ -0,0 +1,24 @@
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(113)]
public class music_blacklist : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Alter.Table("Blacklist")
.AddColumn("ArtistId").AsInt32().WithDefaultValue(0)
.AddColumn("AlbumIds").AsString().WithDefaultValue("");
Delete.Column("SeriesId").FromTable("Blacklist");
Delete.Column("EpisodeIds").FromTable("Blacklist");
}
}
}

@ -18,9 +18,9 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
mapper.ExecuteNonQuery(@"DELETE FROM Blacklist mapper.ExecuteNonQuery(@"DELETE FROM Blacklist
WHERE Id IN ( WHERE Id IN (
SELECT Blacklist.Id FROM Blacklist SELECT Blacklist.Id FROM Blacklist
LEFT OUTER JOIN Series LEFT OUTER JOIN Artists
ON Blacklist.SeriesId = Series.Id ON Blacklist.ArtistId = Artists.Id
WHERE Series.Id IS NULL)"); WHERE Artists.Id IS NULL)");
} }
} }
} }

@ -295,6 +295,7 @@
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Datastore\Migration\105_rename_torrent_downloadstation.cs" /> <Compile Include="Datastore\Migration\105_rename_torrent_downloadstation.cs" />
<Compile Include="Datastore\Migration\113_music_blacklist.cs" />
<Compile Include="Datastore\Migration\112_music_history.cs" /> <Compile Include="Datastore\Migration\112_music_history.cs" />
<Compile Include="Datastore\Migration\111_setup_music.cs" /> <Compile Include="Datastore\Migration\111_setup_music.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationContext.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationContext.cs" />

@ -13,12 +13,12 @@ namespace NzbDrone.Integration.Test.ApiTests
[Ignore("Adding to blacklist not supported")] [Ignore("Adding to blacklist not supported")]
public void should_be_able_to_add_to_blacklist() public void should_be_able_to_add_to_blacklist()
{ {
_artist = EnsureArtist("266189", "The Blacklist"); _artist = EnsureArtist("8ac6cc32-8ddf-43b1-9ac4-4b04f9053176", "Alien Ant Farm");
Blacklist.Post(new Api.Blacklist.BlacklistResource Blacklist.Post(new Api.Blacklist.BlacklistResource
{ {
SeriesId = _artist.Id, ArtistId = _artist.Id,
SourceTitle = "Blacklist.S01E01.Brought.To.You.By-BoomBoxHD" SourceTitle = "Blacklist - Album 1 [2015 FLAC]"
}); });
} }

@ -26,7 +26,7 @@ var Collection = PageableCollection.extend({
}, },
sortMappings : { sortMappings : {
'series' : { sortKey : 'series.sortTitle' } 'artist' : { sortKey : 'artist.sortName' }
}, },
parseState : function(resp) { parseState : function(resp) {

@ -2,7 +2,7 @@ var vent = require('vent');
var Marionette = require('marionette'); var Marionette = require('marionette');
var Backgrid = require('backgrid'); var Backgrid = require('backgrid');
var BlacklistCollection = require('./BlacklistCollection'); var BlacklistCollection = require('./BlacklistCollection');
var SeriesTitleCell = require('../../Cells/SeriesTitleCell'); var ArtistTitleCell = require('../../Cells/ArtistTitleCell');
var QualityCell = require('../../Cells/QualityCell'); var QualityCell = require('../../Cells/QualityCell');
var RelativeDateCell = require('../../Cells/RelativeDateCell'); var RelativeDateCell = require('../../Cells/RelativeDateCell');
var BlacklistActionsCell = require('./BlacklistActionsCell'); var BlacklistActionsCell = require('./BlacklistActionsCell');
@ -21,9 +21,9 @@ module.exports = Marionette.Layout.extend({
columns : [ columns : [
{ {
name : 'series', name : 'artist',
label : 'Series', label : 'Artist',
cell : SeriesTitleCell cell : ArtistTitleCell
}, },
{ {
name : 'sourceTitle', name : 'sourceTitle',

@ -1,5 +1,5 @@
var Backbone = require('backbone'); var Backbone = require('backbone');
var SeriesCollection = require('../../Series/SeriesCollection'); var ArtistCollection = require('../../Artist/ArtistCollection');
module.exports = Backbone.Model.extend({ module.exports = Backbone.Model.extend({
@ -11,7 +11,7 @@ module.exports = Backbone.Model.extend({
}, },
parse : function(model) { parse : function(model) {
model.series = SeriesCollection.get(model.seriesId); model.artist = ArtistCollection.get(model.artistId);
return model; return model;
} }
}); });
Loading…
Cancel
Save