Fixed: Incorrectly looking up books by EditionId

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

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

@ -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()
{

@ -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

@ -157,7 +157,8 @@ namespace NzbDrone.Core.Datastore
new SqlBuilder()
.Join<Author, AuthorMetadata>((a, m) => a.AuthorMetadataId == m.Id)
.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);
Mapper.Entity<QualityDefinition>("QualityDefinitions").RegisterModel()

@ -27,21 +27,21 @@ namespace NzbDrone.Core.Extras
IHandle<AuthorRenamedEvent>
{
private readonly IMediaFileService _mediaFileService;
private readonly IBookService _bookService;
private readonly IEditionService _editionService;
private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService;
private readonly List<IManageExtraFiles> _extraFileManagers;
private readonly Logger _logger;
public ExtraService(IMediaFileService mediaFileService,
IBookService bookService,
IEditionService editionService,
IDiskProvider diskProvider,
IConfigService configService,
IEnumerable<IManageExtraFiles> 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);
}
}

@ -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

@ -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,

@ -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);

@ -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));
}

@ -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);
}

@ -51,7 +51,7 @@ namespace NzbDrone.Core.Notifications
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,
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);
}

Loading…
Cancel
Save