refactor: add logs

This commit is contained in:
Gauthier Roebroeck 2020-08-14 09:58:42 +08:00
parent ee2e805cab
commit cbe714932b
2 changed files with 14 additions and 1 deletions

View file

@ -58,6 +58,7 @@ class LibraryScanner(
seriesLifecycle.sortBooks(createdSeries)
} else {
// if series already exists, update it
logger.debug { "Scanned series already exists. Scanned: $newSeries, Existing: $existingSeries" }
if (newSeries.fileLastModified.truncatedTo(ChronoUnit.MILLIS) != existingSeries.fileLastModified.truncatedTo(ChronoUnit.MILLIS)) {
logger.info { "Series changed on disk, updating: $existingSeries" }
@ -65,10 +66,13 @@ class LibraryScanner(
// update list of books with existing entities if they exist
val existingBooks = bookRepository.findBySeriesId(existingSeries.id)
logger.debug { "Existing books: $existingBooks" }
// update existing books
newBooks.forEach { newBook ->
logger.debug { "Trying to match scanned book by url: $newBook" }
existingBooks.find { it.url == newBook.url }?.let { existingBook ->
logger.debug { "Matched existing book: $existingBook" }
if (newBook.fileLastModified.truncatedTo(ChronoUnit.MILLIS) != existingBook.fileLastModified.truncatedTo(ChronoUnit.MILLIS)) {
logger.info { "Book changed on disk, update and reset media status: $existingBook" }
val updatedBook = existingBook.copy(
@ -87,11 +91,15 @@ class LibraryScanner(
val newBooksUrls = newBooks.map { it.url }
existingBooks
.filterNot { existingBook -> newBooksUrls.contains(existingBook.url) }
.let { books -> bookLifecycle.deleteMany(books.map { it.id }) }
.let { books ->
logger.info { "Deleting books not on disk anymore: $books" }
bookLifecycle.deleteMany(books.map { it.id })
}
// add new books
val existingBooksUrls = existingBooks.map { it.url }
val booksToAdd = newBooks.filterNot { newBook -> existingBooksUrls.contains(newBook.url) }
logger.info { "Adding new books: $booksToAdd" }
seriesLifecycle.addBooks(existingSeries, booksToAdd)
// sort all books

View file

@ -38,12 +38,17 @@ class SeriesLifecycle(
) {
fun sortBooks(series: Series) {
logger.debug { "Sorting books for $series" }
val books = bookRepository.findBySeriesId(series.id)
val metadatas = bookMetadataRepository.findByIds(books.map { it.id })
logger.debug { "Existing books: $books" }
logger.debug { "Existing metadata: $metadatas" }
val sorted = books
.sortedWith(compareBy(natSortComparator) { it.name })
.map { book -> book to metadatas.first { it.bookId == book.id } }
logger.debug { "Sorted books: $sorted" }
bookRepository.updateMany(
sorted.mapIndexed { index, (book, _) -> book.copy(number = index + 1) }