Fixed: Refreshing / Deleting Series

pull/965/head
ta264 4 years ago
parent 652fdae7d9
commit fa25324463

@ -20,6 +20,9 @@ namespace NzbDrone.Core.Books
[MemberwiseEqualityIgnore]
public LazyLoaded<List<Book>> Books { get; set; }
// A placeholder used in refresh only
public string ForeignAuthorId { get; set; }
public override string ToString()
{
return string.Format("[{0}][{1}]", ForeignSeriesId.NullSafe(), Title.NullSafe());

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
@ -8,6 +8,7 @@ namespace NzbDrone.Core.Books
public interface ISeriesBookLinkRepository : IBasicRepository<SeriesBookLink>
{
List<SeriesBookLink> GetLinksBySeries(int seriesId);
List<SeriesBookLink> GetLinksBySeriesAndAuthor(int seriesId, string foreignAuthorId);
List<SeriesBookLink> GetLinksByBook(List<int> bookIds);
}
@ -23,6 +24,17 @@ namespace NzbDrone.Core.Books
return Query(x => x.SeriesId == seriesId);
}
public List<SeriesBookLink> GetLinksBySeriesAndAuthor(int seriesId, string foreignAuthorId)
{
return _database.Query<SeriesBookLink>(
Builder()
.Join<SeriesBookLink, Book>((l, b) => l.BookId == b.Id)
.Join<Book, AuthorMetadata>((b, a) => b.AuthorMetadataId == a.Id)
.Where<SeriesBookLink>(x => x.SeriesId == seriesId)
.Where<AuthorMetadata>(a => a.ForeignAuthorId == foreignAuthorId))
.ToList();
}
public List<SeriesBookLink> GetLinksByBook(List<int> bookIds)
{
return _database.QueryJoined<SeriesBookLink, Series>(

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Serializer;
namespace NzbDrone.Core.Books
{
@ -73,8 +72,16 @@ namespace NzbDrone.Core.Books
protected override void DeleteEntity(Series local, bool deleteFiles)
{
_logger.Trace($"Removing links for series {local} author {local.ForeignAuthorId}");
var children = GetLocalChildren(local, null);
_linkService.DeleteMany(children);
if (!_linkService.GetLinksBySeries(local.Id).Any())
{
_logger.Trace($"Series {local} has no links remaining, removing");
_seriesService.Delete(local.Id);
}
}
protected override List<SeriesBookLink> GetRemoteChildren(Series local, Series remote)
{
@ -83,7 +90,7 @@ namespace NzbDrone.Core.Books
protected override List<SeriesBookLink> GetLocalChildren(Series entity, List<SeriesBookLink> remoteChildren)
{
return _linkService.GetLinksBySeries(entity.Id);
return _linkService.GetLinksBySeriesAndAuthor(entity.Id, entity.ForeignAuthorId);
}
protected override Tuple<SeriesBookLink, List<SeriesBookLink>> GetMatchingExistingChildren(List<SeriesBookLink> existingChildren, SeriesBookLink remote)
@ -155,6 +162,7 @@ namespace NzbDrone.Core.Books
foreach (var item in all)
{
item.ForeignAuthorId = remoteData.ForeignAuthorId;
updated |= RefreshEntityInfo(item, remoteSeries, remoteData, true, forceUpdateFileTags, null);
}

@ -1,17 +1,21 @@
using System.Collections.Generic;
using NzbDrone.Core.Books.Events;
using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.Books
{
public interface ISeriesBookLinkService
{
List<SeriesBookLink> GetLinksBySeries(int seriesId);
List<SeriesBookLink> GetLinksBySeriesAndAuthor(int seriesId, string foreignAuthorId);
List<SeriesBookLink> GetLinksByBook(List<int> bookIds);
void InsertMany(List<SeriesBookLink> model);
void UpdateMany(List<SeriesBookLink> model);
void DeleteMany(List<SeriesBookLink> model);
}
public class SeriesBookLinkService : ISeriesBookLinkService
public class SeriesBookLinkService : ISeriesBookLinkService,
IHandle<BookDeletedEvent>
{
private readonly ISeriesBookLinkRepository _repo;
@ -25,6 +29,11 @@ namespace NzbDrone.Core.Books
return _repo.GetLinksBySeries(seriesId);
}
public List<SeriesBookLink> GetLinksBySeriesAndAuthor(int seriesId, string foreignAuthorId)
{
return _repo.GetLinksBySeriesAndAuthor(seriesId, foreignAuthorId);
}
public List<SeriesBookLink> GetLinksByBook(List<int> bookIds)
{
return _repo.GetLinksByBook(bookIds);
@ -44,5 +53,11 @@ namespace NzbDrone.Core.Books
{
_repo.DeleteMany(model);
}
public void Handle(BookDeletedEvent message)
{
var links = GetLinksByBook(new List<int> { message.Book.Id });
DeleteMany(links);
}
}
}

@ -105,6 +105,7 @@ namespace NzbDrone.Core.Datastore
.LazyLoad(a => a.Books, (db, a) => db.Query<Book>(new SqlBuilder().Where<Book>(rg => rg.AuthorMetadataId == a.Id)).ToList(), a => a.Id > 0);
Mapper.Entity<Series>("Series").RegisterModel()
.Ignore(s => s.ForeignAuthorId)
.LazyLoad(s => s.LinkItems,
(db, series) => db.Query<SeriesBookLink>(new SqlBuilder().Where<SeriesBookLink>(s => s.SeriesId == series.Id)).ToList(),
s => s.Id > 0)

@ -0,0 +1,28 @@
using Dapper;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedEditions : IHousekeepingTask
{
private readonly IMainDatabase _database;
public CleanupOrphanedEditions(IMainDatabase database)
{
_database = database;
}
public void Clean()
{
using (var mapper = _database.OpenConnection())
{
mapper.Execute(@"DELETE FROM Editions
WHERE Id IN (
SELECT Editions.Id FROM Editions
LEFT OUTER JOIN Books
ON Editions.BookId = Books.Id
WHERE Books.Id IS NULL)");
}
}
}
}

@ -0,0 +1,35 @@
using Dapper;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedSeriesBookLinks : IHousekeepingTask
{
private readonly IMainDatabase _database;
public CleanupOrphanedSeriesBookLinks(IMainDatabase database)
{
_database = database;
}
public void Clean()
{
using (var mapper = _database.OpenConnection())
{
mapper.Execute(@"DELETE FROM SeriesBookLinks
WHERE Id IN (
SELECT SeriesBookLinks.Id FROM SeriesBookLinks
LEFT OUTER JOIN Books
ON SeriesBookLinks.BookId = Books.Id
WHERE Books.Id IS NULL)");
mapper.Execute(@"DELETE FROM SeriesBookLinks
WHERE Id IN (
SELECT SeriesBookLinks.Id FROM SeriesBookLinks
LEFT OUTER JOIN Series
ON SeriesBookLinks.SeriesId = Series.Id
WHERE Series.Id IS NULL)");
}
}
}
}
Loading…
Cancel
Save