New: Refresh books added to match existing files

Fixes #1146

(cherry picked from commit 40c99fb67587b5890d4d2a3a3af1d61034598e1f)
pull/1147/head
ta264 3 years ago
parent d8d09c2517
commit 4712fedb0e

@ -0,0 +1,21 @@
using System.Collections.Generic;
using NzbDrone.Core.Messaging.Commands;
namespace NzbDrone.Core.Books.Commands
{
public class BulkRefreshBookCommand : Command
{
public BulkRefreshBookCommand()
{
}
public BulkRefreshBookCommand(List<int> bookIds)
{
BookIds = bookIds;
}
public List<int> BookIds { get; set; }
public override bool SendUpdatesToClient => true;
}
}

@ -25,7 +25,8 @@ namespace NzbDrone.Core.Books
public class RefreshBookService : RefreshEntityServiceBase<Book, Edition>,
IRefreshBookService,
IExecute<RefreshBookCommand>
IExecute<RefreshBookCommand>,
IExecute<BulkRefreshBookCommand>
{
private readonly IBookService _bookService;
private readonly IAuthorService _authorService;
@ -374,6 +375,16 @@ namespace NzbDrone.Core.Books
return RefreshBookInfo(book, data.Books, data, false);
}
public void Execute(BulkRefreshBookCommand message)
{
var books = _bookService.GetBooks(message.BookIds);
foreach (var book in books)
{
RefreshBookInfo(book);
}
}
public void Execute(RefreshBookCommand message)
{
if (message.BookId.HasValue)

@ -79,6 +79,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport
var allImportedTrackFiles = new List<BookFile>();
var allOldTrackFiles = new List<BookFile>();
var addedAuthors = new List<Author>();
var addedBooks = new List<Book>();
var bookDecisions = decisions.Where(e => e.Item.Book != null && e.Approved)
.GroupBy(e => e.Item.Book.ForeignBookId).ToList();
@ -98,7 +99,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport
continue;
}
var book = EnsureBookAdded(decisionList);
var book = EnsureBookAdded(decisionList, addedBooks);
if (book == null)
{
@ -297,6 +298,15 @@ namespace NzbDrone.Core.MediaFiles.BookImport
_commandQueueManager.Push(new BulkRefreshAuthorCommand(addedAuthors.Select(x => x.Id).ToList(), true));
}
var addedAuthorMetadataIds = addedAuthors.Select(x => x.AuthorMetadataId).ToHashSet();
var booksToRefresh = addedBooks.Where(x => !addedAuthorMetadataIds.Contains(x.AuthorMetadataId)).ToList();
if (booksToRefresh.Any())
{
_logger.Debug($"Refreshing info for {booksToRefresh.Count} new books");
_commandQueueManager.Push(new BulkRefreshBookCommand(booksToRefresh.Select(x => x.Id).ToList()));
}
return importResults;
}
@ -363,7 +373,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport
return author;
}
private Book EnsureBookAdded(List<ImportDecision<LocalBook>> decisions)
private Book EnsureBookAdded(List<ImportDecision<LocalBook>> decisions, List<Book> addedBooks)
{
var book = decisions.First().Item.Book;
@ -385,6 +395,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport
book.Monitored = book.Author.Value.Monitored;
book.Added = DateTime.UtcNow;
_bookService.InsertMany(new List<Book> { book });
addedBooks.Add(book);
book.Editions.Value.ForEach(x => x.BookId = book.Id);
_editionService.InsertMany(book.Editions.Value);

Loading…
Cancel
Save