Fixed: Incorrectly looking up books by EditionId

pull/1064/head
ta264 4 years ago
parent 13d8554e7e
commit fe13823b43

@ -47,8 +47,11 @@ namespace NzbDrone.Core.Test.MediaFiles
var edition = Builder<Edition>.CreateNew() var edition = Builder<Edition>.CreateNew()
.With(e => e.Book = book) .With(e => e.Book = book)
.With(e => e.Monitored = true)
.Build(); .Build();
book.Editions = new List<Edition> { edition };
var rootFolder = Builder<RootFolder>.CreateNew() var rootFolder = Builder<RootFolder>.CreateNew()
.With(r => r.IsCalibreLibrary = false) .With(r => r.IsCalibreLibrary = false)
.Build(); .Build();
@ -85,6 +88,10 @@ namespace NzbDrone.Core.Test.MediaFiles
Mocker.GetMock<IRootFolderService>() Mocker.GetMock<IRootFolderService>()
.Setup(s => s.GetBestRootFolder(It.IsAny<string>())) .Setup(s => s.GetBestRootFolder(It.IsAny<string>()))
.Returns(rootFolder); .Returns(rootFolder);
Mocker.GetMock<IEditionService>()
.Setup(s => s.SetMonitored(edition))
.Returns(new List<Edition> { edition });
} }
[Test] [Test]

@ -165,6 +165,7 @@ namespace NzbDrone.Core.Test.MediaFiles
} }
} }
[Ignore("Doesn't make sense now we link to edition")]
[Test] [Test]
public void delete_files_by_book_should_work_if_join_fails() public void delete_files_by_book_should_work_if_join_fails()
{ {

@ -216,7 +216,7 @@ namespace NzbDrone.Core.Books
// Update book ids for trackfiles // Update book ids for trackfiles
var files = _mediaFileService.GetFilesByBook(local.Id); var files = _mediaFileService.GetFilesByBook(local.Id);
files.ForEach(x => x.EditionId = target.Id); files.ForEach(x => x.EditionId = target.Editions.Value.Single(e => e.Monitored).Id);
_mediaFileService.Update(files); _mediaFileService.Update(files);
// Update book ids for history // Update book ids for history

@ -157,7 +157,8 @@ namespace NzbDrone.Core.Datastore
new SqlBuilder() new SqlBuilder()
.Join<Author, AuthorMetadata>((a, m) => a.AuthorMetadataId == m.Id) .Join<Author, AuthorMetadata>((a, m) => a.AuthorMetadataId == m.Id)
.Join<Author, Book>((l, r) => l.AuthorMetadataId == r.AuthorMetadataId) .Join<Author, Book>((l, r) => l.AuthorMetadataId == r.AuthorMetadataId)
.Where<Book>(a => a.Id == f.EditionId)).SingleOrDefault(), .Join<Book, Edition>((l, r) => l.Id == r.BookId)
.Where<Edition>(a => a.Id == f.EditionId)).SingleOrDefault(),
t => t.Id > 0); t => t.Id > 0);
Mapper.Entity<QualityDefinition>("QualityDefinitions").RegisterModel() Mapper.Entity<QualityDefinition>("QualityDefinitions").RegisterModel()

@ -27,21 +27,21 @@ namespace NzbDrone.Core.Extras
IHandle<AuthorRenamedEvent> IHandle<AuthorRenamedEvent>
{ {
private readonly IMediaFileService _mediaFileService; private readonly IMediaFileService _mediaFileService;
private readonly IBookService _bookService; private readonly IEditionService _editionService;
private readonly IDiskProvider _diskProvider; private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly List<IManageExtraFiles> _extraFileManagers; private readonly List<IManageExtraFiles> _extraFileManagers;
private readonly Logger _logger; private readonly Logger _logger;
public ExtraService(IMediaFileService mediaFileService, public ExtraService(IMediaFileService mediaFileService,
IBookService bookService, IEditionService editionService,
IDiskProvider diskProvider, IDiskProvider diskProvider,
IConfigService configService, IConfigService configService,
IEnumerable<IManageExtraFiles> extraFileManagers, IEnumerable<IManageExtraFiles> extraFileManagers,
Logger logger) Logger logger)
{ {
_mediaFileService = mediaFileService; _mediaFileService = mediaFileService;
_bookService = bookService; _editionService = editionService;
_diskProvider = diskProvider; _diskProvider = diskProvider;
_configService = configService; _configService = configService;
_extraFileManagers = extraFileManagers.OrderBy(e => e.Order).ToList(); _extraFileManagers = extraFileManagers.OrderBy(e => e.Order).ToList();
@ -143,11 +143,11 @@ namespace NzbDrone.Core.Extras
public void Handle(TrackFolderCreatedEvent message) public void Handle(TrackFolderCreatedEvent message)
{ {
var author = message.Author; var author = message.Author;
var book = _bookService.GetBook(message.BookFile.EditionId); var edition = _editionService.GetEdition(message.BookFile.EditionId);
foreach (var extraFileManager in _extraFileManagers) foreach (var extraFileManager in _extraFileManagers)
{ {
extraFileManager.CreateAfterBookImport(author, book, message.AuthorFolder, message.BookFolder); extraFileManager.CreateAfterBookImport(author, edition.Book.Value, message.AuthorFolder, message.BookFolder);
} }
} }

@ -72,7 +72,7 @@ namespace NzbDrone.Core.Extras.Files
return new TExtraFile return new TExtraFile
{ {
AuthorId = author.Id, AuthorId = author.Id,
BookId = bookFile.EditionId, BookId = bookFile.Edition.Value.BookId,
BookFileId = bookFile.Id, BookFileId = bookFile.Id,
RelativePath = author.Path.GetRelativePath(newFileName), RelativePath = author.Path.GetRelativePath(newFileName),
Extension = extension Extension = extension

@ -144,7 +144,7 @@ namespace NzbDrone.Core.Extras.Metadata
foreach (var filePath in distinctTrackFilePaths) foreach (var filePath in distinctTrackFilePaths)
{ {
var metadataFilesForConsumer = GetMetadataFilesForConsumer(consumer, metadataFiles) var metadataFilesForConsumer = GetMetadataFilesForConsumer(consumer, metadataFiles)
.Where(m => m.BookId == filePath.EditionId) .Where(m => m.BookId == filePath.Edition.Value.BookId)
.Where(m => m.Type == MetadataType.BookImage || m.Type == MetadataType.BookMetadata) .Where(m => m.Type == MetadataType.BookImage || m.Type == MetadataType.BookMetadata)
.ToList(); .ToList();
@ -287,7 +287,7 @@ namespace NzbDrone.Core.Extras.Metadata
new MetadataFile new MetadataFile
{ {
AuthorId = author.Id, AuthorId = author.Id,
BookId = bookFile.EditionId, BookId = bookFile.Edition.Value.BookId,
BookFileId = bookFile.Id, BookFileId = bookFile.Id,
Consumer = consumer.GetType().Name, Consumer = consumer.GetType().Name,
Type = MetadataType.BookMetadata, Type = MetadataType.BookMetadata,

@ -24,7 +24,7 @@ namespace NzbDrone.Core.MediaFiles
public class BookFileMovingService : IMoveBookFiles public class BookFileMovingService : IMoveBookFiles
{ {
private readonly IBookService _bookService; private readonly IEditionService _editionService;
private readonly IUpdateBookFileService _updateBookFileService; private readonly IUpdateBookFileService _updateBookFileService;
private readonly IBuildFileNames _buildFileNames; private readonly IBuildFileNames _buildFileNames;
private readonly IDiskTransferService _diskTransferService; private readonly IDiskTransferService _diskTransferService;
@ -35,7 +35,7 @@ namespace NzbDrone.Core.MediaFiles
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly Logger _logger; private readonly Logger _logger;
public BookFileMovingService(IBookService bookService, public BookFileMovingService(IEditionService editionService,
IUpdateBookFileService updateBookFileService, IUpdateBookFileService updateBookFileService,
IBuildFileNames buildFileNames, IBuildFileNames buildFileNames,
IDiskTransferService diskTransferService, IDiskTransferService diskTransferService,
@ -46,7 +46,7 @@ namespace NzbDrone.Core.MediaFiles
IConfigService configService, IConfigService configService,
Logger logger) Logger logger)
{ {
_bookService = bookService; _editionService = editionService;
_updateBookFileService = updateBookFileService; _updateBookFileService = updateBookFileService;
_buildFileNames = buildFileNames; _buildFileNames = buildFileNames;
_diskTransferService = diskTransferService; _diskTransferService = diskTransferService;
@ -60,11 +60,11 @@ namespace NzbDrone.Core.MediaFiles
public BookFile MoveBookFile(BookFile bookFile, Author author) public BookFile MoveBookFile(BookFile bookFile, Author author)
{ {
var book = _bookService.GetBook(bookFile.EditionId); var edition = _editionService.GetEdition(bookFile.EditionId);
var newFileName = _buildFileNames.BuildBookFileName(author, bookFile.Edition.Value, bookFile); var newFileName = _buildFileNames.BuildBookFileName(author, edition, bookFile);
var filePath = _buildFileNames.BuildBookFilePath(author, bookFile.Edition.Value, newFileName, Path.GetExtension(bookFile.Path)); var filePath = _buildFileNames.BuildBookFilePath(author, edition, newFileName, Path.GetExtension(bookFile.Path));
EnsureBookFolder(bookFile, author, book, filePath); EnsureBookFolder(bookFile, author, edition.Book.Value, filePath);
_logger.Debug("Renaming book file: {0} to {1}", bookFile, filePath); _logger.Debug("Renaming book file: {0} to {1}", bookFile, filePath);

@ -276,6 +276,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport
foreach (var bookImport in bookImports) foreach (var bookImport in bookImports)
{ {
var book = bookImport.First().ImportDecision.Item.Book; var book = bookImport.First().ImportDecision.Item.Book;
var edition = book.Editions.Value.Single(x => x.Monitored);
var author = bookImport.First().ImportDecision.Item.Author; var author = bookImport.First().ImportDecision.Item.Author;
if (bookImport.Where(e => e.Errors.Count == 0).ToList().Count > 0 && author != null && book != null) if (bookImport.Where(e => e.Errors.Count == 0).ToList().Count > 0 && author != null && book != null)
@ -283,8 +284,8 @@ namespace NzbDrone.Core.MediaFiles.BookImport
_eventAggregator.PublishEvent(new BookImportedEvent( _eventAggregator.PublishEvent(new BookImportedEvent(
author, author,
book, book,
allImportedTrackFiles.Where(s => s.EditionId == book.Id).ToList(), allImportedTrackFiles.Where(s => s.EditionId == edition.Id).ToList(),
allOldTrackFiles.Where(s => s.EditionId == book.Id).ToList(), allOldTrackFiles.Where(s => s.EditionId == edition.Id).ToList(),
replaceExisting, replaceExisting,
downloadClientItem)); downloadClientItem));
} }

@ -86,12 +86,13 @@ namespace NzbDrone.Core.MediaFiles
public void DeleteFilesByBook(int bookId) public void DeleteFilesByBook(int bookId)
{ {
Delete(x => x.EditionId == bookId); var fileIds = GetFilesByBook(bookId).Select(x => x.Id).ToList();
Delete(x => fileIds.Contains(x.Id));
} }
public void UnlinkFilesByBook(int bookId) public void UnlinkFilesByBook(int bookId)
{ {
var files = Query(x => x.EditionId == bookId); var files = GetFilesByBook(bookId);
files.ForEach(x => x.EditionId = 0); files.ForEach(x => x.EditionId = 0);
SetFields(files, f => f.EditionId); SetFields(files, f => f.EditionId);
} }

@ -51,7 +51,7 @@ namespace NzbDrone.Core.Notifications
private string GetBookDownloadMessage(Author author, Book book, List<BookFile> tracks) private string GetBookDownloadMessage(Author author, Book book, List<BookFile> tracks)
{ {
return string.Format("{0} - {1} ({2} Tracks Imported)", return string.Format("{0} - {1} ({2} Files Imported)",
author.Name, author.Name,
book.Title, book.Title,
tracks.Count); tracks.Count);
@ -59,7 +59,7 @@ namespace NzbDrone.Core.Notifications
private string GetBookIncompleteImportMessage(string source) private string GetBookIncompleteImportMessage(string source)
{ {
return string.Format("Readarr failed to Import all tracks for {0}", return string.Format("Readarr failed to Import all files for {0}",
source); source);
} }

Loading…
Cancel
Save