diff --git a/src/NzbDrone.Core.Test/MediaFiles/ImportApprovedTracksFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/ImportApprovedTracksFixture.cs index d1b0cf4cf..7fa95d711 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/ImportApprovedTracksFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/ImportApprovedTracksFixture.cs @@ -47,8 +47,11 @@ namespace NzbDrone.Core.Test.MediaFiles var edition = Builder.CreateNew() .With(e => e.Book = book) + .With(e => e.Monitored = true) .Build(); + book.Editions = new List { edition }; + var rootFolder = Builder.CreateNew() .With(r => r.IsCalibreLibrary = false) .Build(); @@ -85,6 +88,10 @@ namespace NzbDrone.Core.Test.MediaFiles Mocker.GetMock() .Setup(s => s.GetBestRootFolder(It.IsAny())) .Returns(rootFolder); + + Mocker.GetMock() + .Setup(s => s.SetMonitored(edition)) + .Returns(new List { edition }); } [Test] diff --git a/src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs index 3ec4e33e1..d7710446a 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs @@ -165,6 +165,7 @@ namespace NzbDrone.Core.Test.MediaFiles } } + [Ignore("Doesn't make sense now we link to edition")] [Test] public void delete_files_by_book_should_work_if_join_fails() { diff --git a/src/NzbDrone.Core/Books/Services/RefreshBookService.cs b/src/NzbDrone.Core/Books/Services/RefreshBookService.cs index d00963bf9..c7d21e5f6 100644 --- a/src/NzbDrone.Core/Books/Services/RefreshBookService.cs +++ b/src/NzbDrone.Core/Books/Services/RefreshBookService.cs @@ -216,7 +216,7 @@ namespace NzbDrone.Core.Books // Update book ids for trackfiles 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); // Update book ids for history diff --git a/src/NzbDrone.Core/Datastore/TableMapping.cs b/src/NzbDrone.Core/Datastore/TableMapping.cs index c1d7e7320..115e5adc8 100644 --- a/src/NzbDrone.Core/Datastore/TableMapping.cs +++ b/src/NzbDrone.Core/Datastore/TableMapping.cs @@ -157,7 +157,8 @@ namespace NzbDrone.Core.Datastore new SqlBuilder() .Join((a, m) => a.AuthorMetadataId == m.Id) .Join((l, r) => l.AuthorMetadataId == r.AuthorMetadataId) - .Where(a => a.Id == f.EditionId)).SingleOrDefault(), + .Join((l, r) => l.Id == r.BookId) + .Where(a => a.Id == f.EditionId)).SingleOrDefault(), t => t.Id > 0); Mapper.Entity("QualityDefinitions").RegisterModel() diff --git a/src/NzbDrone.Core/Extras/ExtraService.cs b/src/NzbDrone.Core/Extras/ExtraService.cs index 385de89a4..14da39e44 100644 --- a/src/NzbDrone.Core/Extras/ExtraService.cs +++ b/src/NzbDrone.Core/Extras/ExtraService.cs @@ -27,21 +27,21 @@ namespace NzbDrone.Core.Extras IHandle { private readonly IMediaFileService _mediaFileService; - private readonly IBookService _bookService; + private readonly IEditionService _editionService; private readonly IDiskProvider _diskProvider; private readonly IConfigService _configService; private readonly List _extraFileManagers; private readonly Logger _logger; public ExtraService(IMediaFileService mediaFileService, - IBookService bookService, + IEditionService editionService, IDiskProvider diskProvider, IConfigService configService, IEnumerable extraFileManagers, Logger logger) { _mediaFileService = mediaFileService; - _bookService = bookService; + _editionService = editionService; _diskProvider = diskProvider; _configService = configService; _extraFileManagers = extraFileManagers.OrderBy(e => e.Order).ToList(); @@ -143,11 +143,11 @@ namespace NzbDrone.Core.Extras public void Handle(TrackFolderCreatedEvent message) { var author = message.Author; - var book = _bookService.GetBook(message.BookFile.EditionId); + var edition = _editionService.GetEdition(message.BookFile.EditionId); foreach (var extraFileManager in _extraFileManagers) { - extraFileManager.CreateAfterBookImport(author, book, message.AuthorFolder, message.BookFolder); + extraFileManager.CreateAfterBookImport(author, edition.Book.Value, message.AuthorFolder, message.BookFolder); } } diff --git a/src/NzbDrone.Core/Extras/Files/ExtraFileManager.cs b/src/NzbDrone.Core/Extras/Files/ExtraFileManager.cs index 3866d81a2..ab0b60fb1 100644 --- a/src/NzbDrone.Core/Extras/Files/ExtraFileManager.cs +++ b/src/NzbDrone.Core/Extras/Files/ExtraFileManager.cs @@ -72,7 +72,7 @@ namespace NzbDrone.Core.Extras.Files return new TExtraFile { AuthorId = author.Id, - BookId = bookFile.EditionId, + BookId = bookFile.Edition.Value.BookId, BookFileId = bookFile.Id, RelativePath = author.Path.GetRelativePath(newFileName), Extension = extension diff --git a/src/NzbDrone.Core/Extras/Metadata/MetadataService.cs b/src/NzbDrone.Core/Extras/Metadata/MetadataService.cs index 6b1c74e8a..10cbbad95 100644 --- a/src/NzbDrone.Core/Extras/Metadata/MetadataService.cs +++ b/src/NzbDrone.Core/Extras/Metadata/MetadataService.cs @@ -144,7 +144,7 @@ namespace NzbDrone.Core.Extras.Metadata foreach (var filePath in distinctTrackFilePaths) { 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) .ToList(); @@ -287,7 +287,7 @@ namespace NzbDrone.Core.Extras.Metadata new MetadataFile { AuthorId = author.Id, - BookId = bookFile.EditionId, + BookId = bookFile.Edition.Value.BookId, BookFileId = bookFile.Id, Consumer = consumer.GetType().Name, Type = MetadataType.BookMetadata, diff --git a/src/NzbDrone.Core/MediaFiles/BookFileMovingService.cs b/src/NzbDrone.Core/MediaFiles/BookFileMovingService.cs index 0e5d4221d..62eef4472 100644 --- a/src/NzbDrone.Core/MediaFiles/BookFileMovingService.cs +++ b/src/NzbDrone.Core/MediaFiles/BookFileMovingService.cs @@ -24,7 +24,7 @@ namespace NzbDrone.Core.MediaFiles public class BookFileMovingService : IMoveBookFiles { - private readonly IBookService _bookService; + private readonly IEditionService _editionService; private readonly IUpdateBookFileService _updateBookFileService; private readonly IBuildFileNames _buildFileNames; private readonly IDiskTransferService _diskTransferService; @@ -35,7 +35,7 @@ namespace NzbDrone.Core.MediaFiles private readonly IConfigService _configService; private readonly Logger _logger; - public BookFileMovingService(IBookService bookService, + public BookFileMovingService(IEditionService editionService, IUpdateBookFileService updateBookFileService, IBuildFileNames buildFileNames, IDiskTransferService diskTransferService, @@ -46,7 +46,7 @@ namespace NzbDrone.Core.MediaFiles IConfigService configService, Logger logger) { - _bookService = bookService; + _editionService = editionService; _updateBookFileService = updateBookFileService; _buildFileNames = buildFileNames; _diskTransferService = diskTransferService; @@ -60,11 +60,11 @@ namespace NzbDrone.Core.MediaFiles public BookFile MoveBookFile(BookFile bookFile, Author author) { - var book = _bookService.GetBook(bookFile.EditionId); - var newFileName = _buildFileNames.BuildBookFileName(author, bookFile.Edition.Value, bookFile); - var filePath = _buildFileNames.BuildBookFilePath(author, bookFile.Edition.Value, newFileName, Path.GetExtension(bookFile.Path)); + var edition = _editionService.GetEdition(bookFile.EditionId); + var newFileName = _buildFileNames.BuildBookFileName(author, edition, bookFile); + 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); diff --git a/src/NzbDrone.Core/MediaFiles/BookImport/ImportApprovedBooks.cs b/src/NzbDrone.Core/MediaFiles/BookImport/ImportApprovedBooks.cs index 18acb5e6f..3ed3aff28 100644 --- a/src/NzbDrone.Core/MediaFiles/BookImport/ImportApprovedBooks.cs +++ b/src/NzbDrone.Core/MediaFiles/BookImport/ImportApprovedBooks.cs @@ -276,6 +276,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport foreach (var bookImport in bookImports) { var book = bookImport.First().ImportDecision.Item.Book; + var edition = book.Editions.Value.Single(x => x.Monitored); var author = bookImport.First().ImportDecision.Item.Author; 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( author, book, - allImportedTrackFiles.Where(s => s.EditionId == book.Id).ToList(), - allOldTrackFiles.Where(s => s.EditionId == book.Id).ToList(), + allImportedTrackFiles.Where(s => s.EditionId == edition.Id).ToList(), + allOldTrackFiles.Where(s => s.EditionId == edition.Id).ToList(), replaceExisting, downloadClientItem)); } diff --git a/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs b/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs index 041cd31ca..8295096f8 100644 --- a/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs +++ b/src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs @@ -86,12 +86,13 @@ namespace NzbDrone.Core.MediaFiles 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) { - var files = Query(x => x.EditionId == bookId); + var files = GetFilesByBook(bookId); files.ForEach(x => x.EditionId = 0); SetFields(files, f => f.EditionId); } diff --git a/src/NzbDrone.Core/Notifications/NotificationService.cs b/src/NzbDrone.Core/Notifications/NotificationService.cs index b49d55267..b01032a81 100644 --- a/src/NzbDrone.Core/Notifications/NotificationService.cs +++ b/src/NzbDrone.Core/Notifications/NotificationService.cs @@ -51,7 +51,7 @@ namespace NzbDrone.Core.Notifications private string GetBookDownloadMessage(Author author, Book book, List tracks) { - return string.Format("{0} - {1} ({2} Tracks Imported)", + return string.Format("{0} - {1} ({2} Files Imported)", author.Name, book.Title, tracks.Count); @@ -59,7 +59,7 @@ namespace NzbDrone.Core.Notifications 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); }